Skip to content

Commit

Permalink
add create kubeconfig file
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamRussak committed Jun 6, 2024
1 parent 13fbf30 commit fa8a5a4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
)

var (
// cfgFile represents the path to the configuration file.
cfgFile string
uiSize int
macNotify bool
Expand Down
43 changes: 43 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ func printWarning(out io.Writer, name string) {
ct.ResetColor()
}

func FailOnError(err error, message string) {
if err != nil {
printWarning(os.Stdout, fmt.Sprintf("%s: %s\n", message, err.Error()))
os.Exit(1)
}
}

func appendConfig(c1, c2 *clientcmdapi.Config) *clientcmdapi.Config {
config := clientcmdapi.NewConfig()
_ = mergo.Merge(config, c1)
Expand All @@ -420,6 +427,9 @@ func CheckAndTransformFilePath(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
path = filepath.Join(homeDir(), path[2:])
}
if !Exists(path) {
CreateDirectory(path)
}
// read files info
_, err := os.Stat(path)
if err != nil {
Expand Down Expand Up @@ -481,3 +491,36 @@ func validateContextTemplate(contextTemplate []string) error {
}
return nil
}

// checkes if a path exists
func Exists(path string) bool {
printYellow(os.Stdout, "Start Checking if Path Exist")
_, err := os.Stat(path)
if err == nil {
printYellow(os.Stdout, "Path Exist")
return true
}
if os.IsNotExist(err) {
printYellow(os.Stdout, "Path Dose NOT Exist")
return false
}
return false
}

func CreateDirectory(path string) {
dir := filepath.Dir(path)
var create string
if Exists(dir) {
printYellow(os.Stdout, dir+" Path Exist")
} else {
printYellow(os.Stdout, "Createing Directory: "+filepath.Dir(path))
if !filepath.IsAbs(dir) {
printYellow(os.Stdout, dir+" Path is Not Absolute")
create = "./" + dir
} else {
create = dir
}
err := os.MkdirAll(create, 0777)
FailOnError(err, "Failed to Create Directory")
}
}

0 comments on commit fa8a5a4

Please sign in to comment.