chore: refactored getting username

This commit is contained in:
David Allen 2025-04-23 18:31:37 -06:00
parent 1d9dbe7b7f
commit aecb56971d
Signed by: towk
GPG key ID: 0430CDBE22619155
2 changed files with 13 additions and 6 deletions

View file

@ -18,9 +18,9 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"os/user"
magellan "github.com/OpenCHAMI/magellan/internal" magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/util"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -31,8 +31,8 @@ const (
FORMAT_YAML = "yaml" FORMAT_YAML = "yaml"
) )
// CLI arguments as variables to not fiddle with error-prone strings
var ( var (
currentUser *user.User
accessToken string accessToken string
format string format string
timeout int timeout int
@ -79,7 +79,6 @@ func Execute() {
} }
func init() { func init() {
currentUser, _ = user.Current()
cobra.OnInitialize(InitializeConfig) cobra.OnInitialize(InitializeConfig)
rootCmd.PersistentFlags().IntVarP(&concurrency, "concurrency", "j", -1, "Set the number of concurrent processes") rootCmd.PersistentFlags().IntVarP(&concurrency, "concurrency", "j", -1, "Set the number of concurrent processes")
rootCmd.PersistentFlags().IntVarP(&timeout, "timeout", "t", 5, "Set the timeout for requests") rootCmd.PersistentFlags().IntVarP(&timeout, "timeout", "t", 5, "Set the timeout for requests")
@ -87,7 +86,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Set to enable/disable verbose output") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Set to enable/disable verbose output")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Set to enable/disable debug messages") rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Set to enable/disable debug messages")
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "Set the access token") rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "Set the access token")
rootCmd.PersistentFlags().StringVar(&cachePath, "cache", fmt.Sprintf("/tmp/%s/magellan/assets.db", currentUser.Username), "Set the scanning result cache path") rootCmd.PersistentFlags().StringVar(&cachePath, "cache", fmt.Sprintf("/tmp/%s/magellan/assets.db", util.GetCurrentUsername()), "Set the scanning result cache path")
// bind viper config flags with cobra // bind viper config flags with cobra
checkBindFlagError(viper.BindPFlag("concurrency", rootCmd.PersistentFlags().Lookup("concurrency"))) checkBindFlagError(viper.BindPFlag("concurrency", rootCmd.PersistentFlags().Lookup("concurrency")))
@ -123,13 +122,12 @@ func InitializeConfig() {
// TODO: This function should probably be moved to 'internal/config.go' // TODO: This function should probably be moved to 'internal/config.go'
// instead of in this file. // instead of in this file.
func SetDefaults() { func SetDefaults() {
currentUser, _ = user.Current()
viper.SetDefault("threads", 1) viper.SetDefault("threads", 1)
viper.SetDefault("timeout", 5) viper.SetDefault("timeout", 5)
viper.SetDefault("config", "") viper.SetDefault("config", "")
viper.SetDefault("verbose", false) viper.SetDefault("verbose", false)
viper.SetDefault("debug", false) viper.SetDefault("debug", false)
viper.SetDefault("cache", fmt.Sprintf("/tmp/%s/magellan/assets.db", currentUser.Username)) viper.SetDefault("cache", fmt.Sprintf("/tmp/%s/magellan/assets.db", util.GetCurrentUsername()))
viper.SetDefault("scan.hosts", []string{}) viper.SetDefault("scan.hosts", []string{})
viper.SetDefault("scan.ports", []int{}) viper.SetDefault("scan.ports", []int{})
viper.SetDefault("scan.subnets", []string{}) viper.SetDefault("scan.subnets", []string{})

View file

@ -2,6 +2,7 @@ package util
import ( import (
"fmt" "fmt"
"os/user"
"time" "time"
) )
@ -25,3 +26,11 @@ func CheckUntil(interval time.Duration, timeout time.Duration, predicate func()
} }
} }
} }
func GetCurrentUsername() string {
currentUser, _ := user.Current()
if currentUser != nil {
return currentUser.Username
}
return ""
}