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 authored Mar 20, 2024
1 parent 1f24965 commit 25aea4f
Show file tree
Hide file tree
Showing 20 changed files with 114 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 @@ -28,15 +28,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 @@ -50,7 +50,6 @@ func newBillingCmd(c *config) *cobra.Command {
}
return c.projectsBilling()
},
PreRun: bindPFlags,
}
containerBillingCmd := &cobra.Command{
Use: "container",
Expand All @@ -70,7 +69,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 @@ -89,7 +87,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.clusterUsage()
},
PreRun: bindPFlags,
}
ipBillingCmd := &cobra.Command{
Use: "ip",
Expand All @@ -108,7 +105,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.ipUsage()
},
PreRun: bindPFlags,
}
machineBillingCmd := &cobra.Command{
Use: "machine",
Expand All @@ -127,7 +123,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.machineUsage()
},
PreRun: bindPFlags,
}
productOptionBillingCmd := &cobra.Command{
Use: "product-option",
Expand All @@ -146,7 +141,6 @@ export CLOUDCTL_COSTS_HOUR=0.01 # costs per hour
}
return c.productOptionUsage()
},
PreRun: bindPFlags,
}
networkTrafficBillingCmd := &cobra.Command{
Use: "network-traffic",
Expand All @@ -167,7 +161,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 @@ -186,7 +179,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 @@ -205,7 +197,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 @@ -226,7 +217,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 @@ -54,7 +54,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 @@ -64,7 +63,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 @@ -74,7 +72,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 @@ -83,7 +80,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 @@ -92,7 +88,6 @@ func newClusterCmd(c *config) *cobra.Command {
return c.clusterKubeconfig(args)
},
ValidArgsFunction: c.comp.ClusterListCompletion,
PreRun: bindPFlags,
}

clusterReconcileCmd := &cobra.Command{
Expand All @@ -102,7 +97,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 @@ -111,15 +105,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 @@ -134,7 +126,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 @@ -144,7 +135,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 @@ -153,7 +143,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 @@ -162,7 +151,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 @@ -171,7 +159,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 @@ -180,7 +167,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 @@ -189,7 +175,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 @@ -198,7 +183,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 @@ -207,7 +191,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 @@ -216,7 +199,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 @@ -225,7 +207,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 @@ -47,7 +47,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 @@ -32,7 +32,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 25aea4f

Please sign in to comment.