Skip to content

Commit

Permalink
Fix default value for credentials-file flag
Browse files Browse the repository at this point in the history
 - The tilde variable is something that happends in a Shell, therefore it won't
   work as the default value. We use os.UserHomeDir now
  • Loading branch information
martialblog committed Jan 9, 2023
1 parent 760a721 commit 82bbed7
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"github.com/NETWAYS/go-check"
"github.com/spf13/cobra"
"os"
"os/user"
"path/filepath"
"runtime"
)

var (
Expand Down Expand Up @@ -51,11 +54,40 @@ func init() {
})

p := rootCmd.PersistentFlags()
p.StringVarP(&CredentialsFile, "credentials-file", "C", "~/.aws/credentials", "Path to the credentials file")
// Default is empty and set later with the user's Home dir
// If we set the default before the help text will show the full user's home dir which we decided against
p.StringVarP(&CredentialsFile, "credentials-file", "C", "", "Path to the credentials file (default \"$HOME/.aws/credentials\")")

if CredentialsFile == "" {
CredentialsFile = filepath.Join(userHomeDir(), ".aws", "credentials")
}

p.StringVarP(&Region, "region", "R", "eu-central-1", "The AWS region to send requests to")
p.StringVarP(&Profile, "profile", "P", "default", "The AWS profile name, which represents a separate credential profile in the credential file")
p.IntVarP(&Timeout, "timeout", "t", Timeout, "Timeout for the check")

rootCmd.Flags().SortFlags = false
p.SortFlags = false
}

// Wrapper for os.UserHomeDir when getting config files for example
func userHomeDir() string {
var home string

if runtime.GOOS == "windows" { // Windows
home = os.Getenv("USERPROFILE")
} else { // Linux/macOS
home = os.Getenv("HOME")
}

if len(home) > 0 {
return home
}

currUser, _ := user.Current()
if currUser != nil {
home = currUser.HomeDir
}

return home
}

0 comments on commit 82bbed7

Please sign in to comment.