From 4d8876f64985e27a20f3e0e22eeb6030cfe70517 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Tue, 21 Jan 2020 11:36:00 +0100 Subject: [PATCH] add --overwrite flag for get-kubeconfig --- cli/cluster.go | 23 +++++++++++++++++------ cli/commands.go | 2 +- cli/shell.go | 2 +- main.go | 4 ++++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cli/cluster.go b/cli/cluster.go index 04e72cba..90081caa 100644 --- a/cli/cluster.go +++ b/cli/cluster.go @@ -167,7 +167,7 @@ func createKubeConfigFile(cluster string) error { return nil } -func getKubeConfig(cluster string) (string, error) { +func getKubeConfig(cluster string, overwrite bool) (string, error) { kubeConfigPath, err := getClusterKubeConfigPath(cluster) if err != nil { return "", err @@ -180,14 +180,25 @@ func getKubeConfig(cluster string) (string, error) { return "", fmt.Errorf("Cluster %s does not exist", cluster) } - // If kubeconfi.yaml has not been created, generate it now - if _, err := os.Stat(kubeConfigPath); err != nil { - if os.IsNotExist(err) { - if err = createKubeConfigFile(cluster); err != nil { + // Create or overwrite file no matter if it exists or not + if overwrite { + log.Debugf("Creating/Overwriting file %s...", kubeConfigPath) + if err = createKubeConfigFile(cluster); err != nil { + return "", err + } + } else { + // If kubeconfi.yaml has not been created, generate it now + if _, err := os.Stat(kubeConfigPath); err != nil { + if os.IsNotExist(err) { + log.Debugf("File %s does not exist. Creating it now...", kubeConfigPath) + if err = createKubeConfigFile(cluster); err != nil { + return "", err + } + } else { return "", err } } else { - return "", err + log.Debugf("File %s exists, leaving it as it is...", kubeConfigPath) } } diff --git a/cli/commands.go b/cli/commands.go index 59431fc3..a5844dfb 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -470,7 +470,7 @@ func GetKubeConfig(c *cli.Context) error { } for _, cluster := range clusters { - kubeConfigPath, err := getKubeConfig(cluster.name) + kubeConfigPath, err := getKubeConfig(cluster.name, c.Bool("overwrite")) if err != nil { if !c.Bool("all") { return err diff --git a/cli/shell.go b/cli/shell.go index b991681b..d7d5d110 100644 --- a/cli/shell.go +++ b/cli/shell.go @@ -51,7 +51,7 @@ func subShell(cluster, shell, command string) error { } // get kubeconfig for selected cluster - kubeConfigPath, err := getKubeConfig(cluster) + kubeConfigPath, err := getKubeConfig(cluster, true) if err != nil { return err } diff --git a/main.go b/main.go index 95c3750e..1aaa1605 100644 --- a/main.go +++ b/main.go @@ -273,6 +273,10 @@ func main() { Name: "all, a", Usage: "Get kubeconfig for all clusters (this ignores the --name/-n flag)", }, + cli.BoolFlag{ + Name: "overwrite, o", + Usage: "Overwrite any existing file with the same name", + }, }, Action: run.GetKubeConfig, },