diff --git a/cmd/config.go b/cmd/config.go index e89ce4a..c4d9519 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -2,6 +2,7 @@ package cmd import ( "davidallendj/oidc-auth/internal/util" + "fmt" "log" "os" "path/filepath" @@ -79,6 +80,11 @@ var configCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // create a new config at all args (paths) for _, path := range args { + // check and make sure something doesn't exist first + if exists, err := util.PathExists(path); exists || err != nil { + fmt.Printf("file or directory exists\n") + continue + } SaveDefaultConfig(path) } }, diff --git a/internal/util/util.go b/internal/util/util.go index 92b6eb7..9bf6de6 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "math/rand" "net/url" + "os" "strings" ) @@ -46,3 +47,14 @@ func EncodeURL(s string) string { func EncodeBase64(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } + +func PathExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +}