Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}