Skip to content

Commit

Permalink
Fix flag initialization and expand home directory for kubeconfig path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Mar 19, 2024
1 parent 1483210 commit 23ddbc7
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 139 deletions.
2 changes: 0 additions & 2 deletions cmd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ func newAuditCmd(c *config) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return c.auditList()
},
PreRun: bindPFlags,
}
auditDescribeCmd := &cobra.Command{
Use: "describe <rqid>",
Short: "describe an audit trace",
RunE: func(cmd *cobra.Command, args []string) error {
return c.auditDescribe(args)
},
PreRun: bindPFlags,
}

auditDescribeCmd.Flags().String("phase", "response", "phase of the audit trace. One of [request, response, single, error, opened, closed]")
Expand Down
10 changes: 0 additions & 10 deletions cmd/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func newBillingCmd(c *config) *cobra.Command {
}
return c.projectsBilling()
},
PreRun: bindPFlags,
}
containerBillingCmd := &cobra.Command{
Use: "container",
Expand All @@ -69,7 +68,6 @@ export CLOUDCTL_COSTS_MEMORY_GI_HOUR=0.01 # costs per memory hour
}
return c.containerUsage()
},
PreRun: bindPFlags,
}
clusterBillingCmd := &cobra.Command{
Use: "cluster",
Expand All @@ -88,7 +86,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.clusterUsage()
},
PreRun: bindPFlags,
}
ipBillingCmd := &cobra.Command{
Use: "ip",
Expand All @@ -107,7 +104,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.ipUsage()
},
PreRun: bindPFlags,
}
machineBillingCmd := &cobra.Command{
Use: "machine",
Expand All @@ -126,7 +122,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.machineUsage()
},
PreRun: bindPFlags,
}
productOptionBillingCmd := &cobra.Command{
Use: "product-option",
Expand All @@ -145,7 +140,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.productOptionUsage()
},
PreRun: bindPFlags,
}
networkTrafficBillingCmd := &cobra.Command{
Use: "network-traffic",
Expand All @@ -166,7 +160,6 @@ export CLOUDCTL_COSTS_TOTAL_NETWORK_TRAFFIC_GI=0.01 # costs per gi
}
return c.networkTrafficUsage()
},
PreRun: bindPFlags,
}
s3BillingCmd := &cobra.Command{
Use: "s3",
Expand All @@ -185,7 +178,6 @@ export CLOUDCTL_COSTS_STORAGE_GI_HOUR=0.01 # costs per storage hour
}
return c.s3Usage()
},
PreRun: bindPFlags,
}
volumeBillingCmd := &cobra.Command{
Use: "volume",
Expand All @@ -204,7 +196,6 @@ export CLOUDCTL_COSTS_STORAGE_GI_HOUR=0.01 # costs per capacity hour
}
return c.volumeUsage()
},
PreRun: bindPFlags,
}
postgresBillingCmd := &cobra.Command{
Use: "postgres",
Expand All @@ -225,7 +216,6 @@ export CLOUDCTL_COSTS_STORAGE_GI_HOUR=0.01 # Costs per capacity hour
}
return c.postgresUsage()
},
PreRun: bindPFlags,
}

billingCmd.AddCommand(projectBillingCmd)
Expand Down
19 changes: 0 additions & 19 deletions cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func newClusterCmd(c *config) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return c.clusterCreate()
},
PreRun: bindPFlags,
}

clusterListCmd := &cobra.Command{
Expand All @@ -63,7 +62,6 @@ func newClusterCmd(c *config) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return c.clusterList()
},
PreRun: bindPFlags,
}
clusterDeleteCmd := &cobra.Command{
Use: "delete <clusterid>",
Expand All @@ -73,7 +71,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterDelete(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterDescribeCmd := &cobra.Command{
Use: "describe <clusterid>",
Expand All @@ -82,7 +79,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterDescribe(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterKubeconfigCmd := &cobra.Command{
Use: "kubeconfig <clusterid>",
Expand All @@ -91,7 +87,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterKubeconfig(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}

clusterReconcileCmd := &cobra.Command{
Expand All @@ -101,7 +96,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.reconcileCluster(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterUpdateCmd := &cobra.Command{
Use: "update <clusterid>",
Expand All @@ -110,15 +104,13 @@ func newClusterCmd(c *config) *cobra.Command {
return c.updateCluster(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterInputsCmd := &cobra.Command{
Use: "inputs",
Short: "get possible cluster inputs like k8s versions, etc.",
RunE: func(cmd *cobra.Command, args []string) error {
return c.clusterInputs()
},
PreRun: bindPFlags,
}
clusterMachineCmd := &cobra.Command{
Use: "machine",
Expand All @@ -133,7 +125,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMachines(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterIssuesCmd := &cobra.Command{
Use: "issues [<clusterid>]",
Expand All @@ -143,7 +134,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterIssues(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterMonitoringSecretCmd := &cobra.Command{
Use: "monitoring-secret <clusterid>",
Expand All @@ -152,7 +142,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMonitoringSecret(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterMachineSSHCmd := &cobra.Command{
Use: "ssh <clusterid>",
Expand All @@ -161,7 +150,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMachineSSH(args, false)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterMachineConsoleCmd := &cobra.Command{
Use: "console <clusterid>",
Expand All @@ -170,7 +158,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMachineSSH(args, true)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterMachineResetCmd := &cobra.Command{
Use: "reset <clusterid>",
Expand All @@ -179,7 +166,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMachineReset(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterMachineCycleCmd := &cobra.Command{
Use: "cycle <clusterid>",
Expand All @@ -188,7 +174,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMachineCycle(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterMachineReinstallCmd := &cobra.Command{
Use: "reinstall <clusterid>",
Expand All @@ -197,7 +182,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMachineReinstall(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterMachinePackagesCmd := &cobra.Command{
Use: "packages <clusterid>",
Expand All @@ -206,7 +190,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterMachinePackages(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterLogsCmd := &cobra.Command{
Use: "logs <clusterid>",
Expand All @@ -215,7 +198,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterLogs(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}
clusterDNSManifestCmd := &cobra.Command{
Use: "dns-manifest <clusterid>",
Expand All @@ -224,7 +206,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterDNSManifest(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}

clusterCreateCmd.Flags().String("name", "", "name of the cluster, max 10 characters. [required]")
Expand Down
5 changes: 0 additions & 5 deletions cmd/cluster_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func newClusterAuditCmd(c *config) *cobra.Command {
}
return fmt.Errorf("no command specified")
},
PreRun: bindPFlags,
}
modeCmd := &cobra.Command{
Use: "mode --cluster-id=<clusterid>",
Expand All @@ -51,7 +50,6 @@ func newClusterAuditCmd(c *config) *cobra.Command {
"blocking\tBlock API server responses on processing each individual event.",
"blocking-strict\tSame as blocking, but when there is a failure during audit logging at the RequestReceived stage, the whole request to the kube-apiserver fails. This is the default.",
},
PreRun: bindPFlags,
}
policyCmd := &cobra.Command{
Use: "policy --cluster-id=<clusterid>",
Expand All @@ -60,15 +58,13 @@ func newClusterAuditCmd(c *config) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return w.auditPolicy()
},
PreRun: bindPFlags,
}
splunkCmd := &cobra.Command{
Use: "splunk --cluster-id=<clusterid>",
Short: "configure splunk as an audit backend, if enabled without any specific configuration, the provider's default configuration will be used",
RunE: func(cmd *cobra.Command, args []string) error {
return w.splunk()
},
PreRun: bindPFlags,
}
clusterForwardingCmd := &cobra.Command{
Use: "cluster-forwarding --cluster-id=<clusterid>",
Expand All @@ -77,7 +73,6 @@ func newClusterAuditCmd(c *config) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return w.clusterForwarding()
},
PreRun: bindPFlags,
}

clusterAuditCmd.Flags().Bool("disabled", false, "disables the entire audit functionality, enable again with --disabled=false, requires --yes-i-really-mean-it flag")
Expand Down
2 changes: 0 additions & 2 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ contexts:
}
return nil
},
PreRun: bindPFlags,
}

contextShortCmd := &cobra.Command{
Expand All @@ -51,7 +50,6 @@ contexts:
RunE: func(cmd *cobra.Command, args []string) error {
return contextShort()
},
PreRun: bindPFlags,
}

contextCmd.AddCommand(contextShortCmd)
Expand Down
1 change: 0 additions & 1 deletion cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func newDashboardCmd(c *config) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runDashboard(c.cloud)
},
PreRun: bindPFlags,
}

tabs := dashboardTabs(nil, nil, nil)
Expand Down
1 change: 0 additions & 1 deletion cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func newHealthCmd(c *config) *cobra.Command {

return output.New().Print(resp.Payload.Services)
},
PreRun: bindPFlags,
}
return healthCmd
}
14 changes: 14 additions & 0 deletions cmd/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -184,3 +185,16 @@ func ClientNoAuth() runtime.ClientAuthInfoWriterFunc {
noAuth := func(_ runtime.ClientRequest, _ strfmt.Registry) error { return nil }
return runtime.ClientAuthInfoWriterFunc(noAuth)
}

func ExpandHomeDir(path string) (string, error) {
if !strings.HasPrefix(path, "~/") {
return path, nil
}

homedir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("unable to expand home dir: %w", err)
}

return filepath.Join(homedir, strings.TrimLeft(path, "~/")), nil
}
45 changes: 45 additions & 0 deletions cmd/helper/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package helper

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestExpandHomeDir(t *testing.T) {
homedir, err := os.UserHomeDir()
require.NoError(t, err)

tests := []struct {
name string
path string
want string
wantErr bool
}{
{
name: "no home dir",
path: "/my/absolute/path",
want: "/my/absolute/path",
wantErr: false,
},
{
name: "with home dir",
path: "~/file",
want: homedir + "/file",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ExpandHomeDir(tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("ExpandHomeDir() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ExpandHomeDir() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 23ddbc7

Please sign in to comment.