Added check to not overwrite config file

This commit is contained in:
David J. Allen 2024-02-22 08:37:41 -07:00
parent 3735421cf9
commit 08ff9ee9e5
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
2 changed files with 18 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
"davidallendj/oidc-auth/internal/util" "davidallendj/oidc-auth/internal/util"
"fmt"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -79,6 +80,11 @@ var configCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// create a new config at all args (paths) // create a new config at all args (paths)
for _, path := range args { 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) SaveDefaultConfig(path)
} }
}, },

View file

@ -4,6 +4,7 @@ import (
"encoding/base64" "encoding/base64"
"math/rand" "math/rand"
"net/url" "net/url"
"os"
"strings" "strings"
) )
@ -46,3 +47,14 @@ func EncodeURL(s string) string {
func EncodeBase64(s string) string { func EncodeBase64(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s)) 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
}