From f7f7ddb9abbd15fa7b60bf963d1655e1c34c4d9f Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Mon, 23 Oct 2023 17:06:32 -0400 Subject: [PATCH 1/6] Add root cmd logic to allow non-auth execution --- cmd/root.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index deb5493c..dd1a445a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,8 +29,12 @@ import ( ) const ( - userAgent = "vultr-cli/" + version - perPageDefault int = 100 + userAgent = "vultr-cli/" + version + perPageDefault int = 100 + apiKeyError string = ` +Please export your VULTR API key as an environment variable or add 'api-key' to your config file, eg: +export VULTR_API_KEY='' + ` ) var cfgFile string @@ -47,7 +51,6 @@ var rootCmd = &cobra.Command{ // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) os.Exit(1) } } @@ -83,11 +86,13 @@ func init() { rootCmd.AddCommand(User()) rootCmd.AddCommand(VPC()) rootCmd.AddCommand(VPC2()) - cobra.OnInitialize(initConfig) + + ctx := initConfig() + rootCmd.SetContext(ctx) } // initConfig reads in config file and ENV variables if set. -func initConfig() { +func initConfig() context.Context { var token string configPath := viper.GetString("config") @@ -112,18 +117,23 @@ func initConfig() { token = os.Getenv("VULTR_API_KEY") } + ctx := context.Background() + if token == "" { - fmt.Println("Please export your VULTR API key as an environment variable or add `api-key` to your config file, eg:") - fmt.Println("export VULTR_API_KEY=''") - os.Exit(1) + client = govultr.NewClient(nil) + ctx = context.WithValue(ctx, "authenticated", false) + + } else { + config := &oauth2.Config{} + ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: token}) + client = govultr.NewClient(oauth2.NewClient(ctx, ts)) + ctx = context.WithValue(ctx, "authenticated", true) } - config := &oauth2.Config{} - ts := config.TokenSource(context.Background(), &oauth2.Token{AccessToken: token}) - client = govultr.NewClient(oauth2.NewClient(context.Background(), ts)) - client.SetRateLimit(1 * time.Second) client.SetUserAgent(userAgent) + + return ctx } func getPaging(cmd *cobra.Command) *govultr.ListOptions { From 965b9de3ecc08a4ec5c767d7fd16dca3863890c5 Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Mon, 23 Oct 2023 17:07:33 -0400 Subject: [PATCH 2/6] Add pre-execute logic to auth-required commands --- cmd/account.go | 6 ++++++ cmd/backups.go | 6 ++++++ cmd/bareMetal.go | 6 ++++++ cmd/billing.go | 6 ++++++ cmd/blockStorage.go | 6 ++++++ cmd/database.go | 6 ++++++ cmd/dns.go | 8 ++++++++ cmd/firewall.go | 8 ++++++++ cmd/instance.go | 6 ++++++ cmd/iso.go | 6 ++++++ cmd/kubernetes.go | 6 ++++++ cmd/loadBalancer.go | 6 ++++++ cmd/network.go | 6 ++++++ cmd/objectStorage.go | 6 ++++++ cmd/reservedIP.go | 6 ++++++ cmd/script.go | 6 ++++++ cmd/snapshot.go | 6 ++++++ cmd/sshKey.go | 6 ++++++ cmd/user.go | 6 ++++++ cmd/vpc.go | 6 ++++++ cmd/vpc2.go | 6 ++++++ 21 files changed, 130 insertions(+) diff --git a/cmd/account.go b/cmd/account.go index 14ae44c8..a91cda63 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -37,4 +37,10 @@ var accountCmd = &cobra.Command{ printer.Account(account) }, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } diff --git a/cmd/backups.go b/cmd/backups.go index 142298f5..77d88056 100644 --- a/cmd/backups.go +++ b/cmd/backups.go @@ -30,6 +30,12 @@ func Backups() *cobra.Command { Use: "backups", Aliases: []string{"b"}, Short: "Display backups", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } backupsCmd.AddCommand(backupsList, backupsGet) diff --git a/cmd/bareMetal.go b/cmd/bareMetal.go index b1db3fab..d640f224 100644 --- a/cmd/bareMetal.go +++ b/cmd/bareMetal.go @@ -62,6 +62,12 @@ func BareMetal() *cobra.Command { Aliases: []string{"bm"}, Long: bareMetalLong, Example: bareMetalExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } bareMetalCmd.AddCommand( diff --git a/cmd/billing.go b/cmd/billing.go index f68e674a..6cb8e450 100644 --- a/cmd/billing.go +++ b/cmd/billing.go @@ -94,6 +94,12 @@ func Billing() *cobra.Command { Short: "Display billing information", Long: billingLong, Example: billingExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } historyCmd := &cobra.Command{ diff --git a/cmd/blockStorage.go b/cmd/blockStorage.go index 8ec1846e..4d7bb3ea 100644 --- a/cmd/blockStorage.go +++ b/cmd/blockStorage.go @@ -112,6 +112,12 @@ func BlockStorageCmd() *cobra.Command { Aliases: []string{"bs"}, Short: "block storage commands", Long: `block-storage is used to interact with the block-storage api`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } bsCmd.AddCommand(bsAttach, bsCreate, bsDelete, bsDetach, bsLabelSet, bsList, bsGet, bsResize) diff --git a/cmd/database.go b/cmd/database.go index 4dc30f9e..ef784ca1 100644 --- a/cmd/database.go +++ b/cmd/database.go @@ -66,6 +66,12 @@ func Database() *cobra.Command { //nolint:funlen Short: "commands to interact with managed databases on vultr", Long: databaseLong, Example: databaseExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } databaseCmd.AddCommand(databaseList, databaseCreate, databaseInfo, databaseUpdate, databaseDelete) diff --git a/cmd/dns.go b/cmd/dns.go index f34866b1..8d184e83 100644 --- a/cmd/dns.go +++ b/cmd/dns.go @@ -15,6 +15,8 @@ package cmd import ( + "fmt" + "github.com/spf13/cobra" ) @@ -24,6 +26,12 @@ func DNS() *cobra.Command { Use: "dns", Short: "dns is used to access dns commands", Long: ``, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } dnsCmd.AddCommand(DNSDomain()) diff --git a/cmd/firewall.go b/cmd/firewall.go index 23899c32..ff3b125f 100644 --- a/cmd/firewall.go +++ b/cmd/firewall.go @@ -15,6 +15,8 @@ package cmd import ( + "fmt" + "github.com/spf13/cobra" ) @@ -25,6 +27,12 @@ func Firewall() *cobra.Command { Short: "firewall is used to access firewall commands", Long: ``, Aliases: []string{"fw"}, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } firewallCmd.AddCommand(FirewallGroup(), FirewallRule()) diff --git a/cmd/instance.go b/cmd/instance.go index c80c693d..994a30e8 100644 --- a/cmd/instance.go +++ b/cmd/instance.go @@ -95,6 +95,12 @@ func Instance() *cobra.Command { //nolint: funlen,gocyclo Short: "commands to interact with instances on vultr", Long: instanceLong, Example: instanceExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } instanceCmd.AddCommand( diff --git a/cmd/iso.go b/cmd/iso.go index c74c2789..5972fb1d 100644 --- a/cmd/iso.go +++ b/cmd/iso.go @@ -32,6 +32,12 @@ func ISO() *cobra.Command { Use: "iso", Short: "iso is used to access iso commands", Long: ``, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } isoCmd.AddCommand(isoCreate, isoDelete, isoPrivateGet, isoPrivateList, isoPublic) diff --git a/cmd/kubernetes.go b/cmd/kubernetes.go index 12c1eb26..86426c2a 100644 --- a/cmd/kubernetes.go +++ b/cmd/kubernetes.go @@ -229,6 +229,12 @@ func Kubernetes() *cobra.Command { //nolint: funlen Short: "kubernetes is used to access kubernetes commands", Long: kubernetesLong, Example: kubernetesExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } kubernetesCmd.AddCommand(k8Create, k8Get, k8List, k8GetConfig, k8Update, k8Delete, k8DeleteWithResources, k8GetVersions) diff --git a/cmd/loadBalancer.go b/cmd/loadBalancer.go index 74b28689..5b148b83 100644 --- a/cmd/loadBalancer.go +++ b/cmd/loadBalancer.go @@ -86,6 +86,12 @@ func LoadBalancer() *cobra.Command { //nolint: funlen Short: "load balancer commands", Long: lbLong, Example: lbExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } lbCmd.AddCommand(lbCreate, lbDelete, lbGet, lbList, lbUpdate) diff --git a/cmd/network.go b/cmd/network.go index 643dca34..5cfabcb8 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -40,6 +40,12 @@ func Network() *cobra.Command { Short: "network interacts with network actions", Long: netLong, Deprecated: "Use vpc instead.", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } networkCmd.AddCommand(networkGet, networkList, networkDelete, networkCreate) diff --git a/cmd/objectStorage.go b/cmd/objectStorage.go index 7fe9202d..80af1583 100644 --- a/cmd/objectStorage.go +++ b/cmd/objectStorage.go @@ -31,6 +31,12 @@ func ObjectStorageCmd() *cobra.Command { Aliases: []string{"objStorage"}, Short: "object storage commands", Long: `object-storage is used to interact with the object-storage api`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } objStorageCmd.AddCommand( diff --git a/cmd/reservedIP.go b/cmd/reservedIP.go index bfbfe675..22584fa3 100644 --- a/cmd/reservedIP.go +++ b/cmd/reservedIP.go @@ -113,6 +113,12 @@ func ReservedIP() *cobra.Command { Short: "reserved-ip lets you interact with reserved-ip ", Long: reservedIPLong, Example: reservedIPExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } reservedIPCmd.AddCommand( diff --git a/cmd/script.go b/cmd/script.go index 6d795973..0dfdf7ef 100644 --- a/cmd/script.go +++ b/cmd/script.go @@ -32,6 +32,12 @@ func Script() *cobra.Command { Aliases: []string{"ss"}, Short: "startup script commands", Long: `script is used to access startup script commands`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } cmd.AddCommand(scriptCreate, scriptGet, scriptDelete, scriptList, scriptUpdate) diff --git a/cmd/snapshot.go b/cmd/snapshot.go index 53361a27..c6a4aa70 100644 --- a/cmd/snapshot.go +++ b/cmd/snapshot.go @@ -32,6 +32,12 @@ func Snapshot() *cobra.Command { Aliases: []string{"sn"}, Short: "snapshot commands", Long: `snapshot is used to access snapshot commands`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } cmd.AddCommand(snapshotCreate, snapshotCreateFromURL, snapshotGet, snapshotDelete, snapshotList) diff --git a/cmd/sshKey.go b/cmd/sshKey.go index 7f43a2fe..8f1fbfda 100644 --- a/cmd/sshKey.go +++ b/cmd/sshKey.go @@ -32,6 +32,12 @@ func SSHKey() *cobra.Command { Aliases: []string{"ssh"}, Short: "ssh-key commands", Long: `ssh-key is used to access SSH key commands`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } cmd.AddCommand(sshCreate, sshDelete, sshGet, sshList, sshUpdate) diff --git a/cmd/user.go b/cmd/user.go index ce74e1ba..438a6ed5 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -32,6 +32,12 @@ func User() *cobra.Command { Aliases: []string{"u"}, Short: "user commands", Long: `user is used to access user commands`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } cmd.AddCommand(userCreate, userDelete, userGet, userList, userUpdate) diff --git a/cmd/vpc.go b/cmd/vpc.go index 72969729..f96aa097 100644 --- a/cmd/vpc.go +++ b/cmd/vpc.go @@ -82,6 +82,12 @@ func VPC() *cobra.Command { Short: "Interact with VPCs", Long: vpcLong, Example: vpcExample, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } vpcCmd.AddCommand(vpcGet, vpcList, vpcDelete, vpcCreate, vpcUpdate) diff --git a/cmd/vpc2.go b/cmd/vpc2.go index c2c32388..8eadfdba 100644 --- a/cmd/vpc2.go +++ b/cmd/vpc2.go @@ -62,6 +62,12 @@ func VPC2() *cobra.Command { Short: "commands to interact with vpc2 on vultr", Long: vpc2Long, Example: vpc2Example, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if auth := cmd.Context().Value("authenticated"); auth != true { + return fmt.Errorf(apiKeyError) + } + return nil + }, } vpc2Cmd.AddCommand(vpc2List, vpc2Create, vpc2Info, vpc2Update, vpc2Delete) From 2acd8be3e4a966f659dd7b05deba5ae6e67f73ab Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Mon, 23 Oct 2023 17:15:56 -0400 Subject: [PATCH 3/6] Switch to errors.New on pre-run checks --- cmd/account.go | 3 ++- cmd/backups.go | 2 +- cmd/bareMetal.go | 2 +- cmd/billing.go | 2 +- cmd/blockStorage.go | 2 +- cmd/database.go | 2 +- cmd/dns.go | 4 ++-- cmd/firewall.go | 4 ++-- cmd/instance.go | 2 +- cmd/iso.go | 2 +- cmd/kubernetes.go | 2 +- cmd/loadBalancer.go | 2 +- cmd/network.go | 2 +- cmd/objectStorage.go | 2 +- cmd/reservedIP.go | 2 +- cmd/script.go | 2 +- cmd/snapshot.go | 2 +- cmd/sshKey.go | 2 +- cmd/user.go | 2 +- cmd/vpc.go | 2 +- cmd/vpc2.go | 2 +- 21 files changed, 24 insertions(+), 23 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index a91cda63..bdbb01e6 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -16,6 +16,7 @@ package cmd import ( "context" + "errors" "fmt" "os" @@ -39,7 +40,7 @@ var accountCmd = &cobra.Command{ }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/backups.go b/cmd/backups.go index 77d88056..028c1b38 100644 --- a/cmd/backups.go +++ b/cmd/backups.go @@ -32,7 +32,7 @@ func Backups() *cobra.Command { Short: "Display backups", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/bareMetal.go b/cmd/bareMetal.go index d640f224..692f0c2f 100644 --- a/cmd/bareMetal.go +++ b/cmd/bareMetal.go @@ -64,7 +64,7 @@ func BareMetal() *cobra.Command { Example: bareMetalExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/billing.go b/cmd/billing.go index 6cb8e450..2132ebf6 100644 --- a/cmd/billing.go +++ b/cmd/billing.go @@ -96,7 +96,7 @@ func Billing() *cobra.Command { Example: billingExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/blockStorage.go b/cmd/blockStorage.go index 4d7bb3ea..cfb1c7c0 100644 --- a/cmd/blockStorage.go +++ b/cmd/blockStorage.go @@ -114,7 +114,7 @@ func BlockStorageCmd() *cobra.Command { Long: `block-storage is used to interact with the block-storage api`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/database.go b/cmd/database.go index ef784ca1..77c2b07a 100644 --- a/cmd/database.go +++ b/cmd/database.go @@ -68,7 +68,7 @@ func Database() *cobra.Command { //nolint:funlen Example: databaseExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/dns.go b/cmd/dns.go index 8d184e83..fd0202a4 100644 --- a/cmd/dns.go +++ b/cmd/dns.go @@ -15,7 +15,7 @@ package cmd import ( - "fmt" + "errors" "github.com/spf13/cobra" ) @@ -28,7 +28,7 @@ func DNS() *cobra.Command { Long: ``, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/firewall.go b/cmd/firewall.go index ff3b125f..3093917a 100644 --- a/cmd/firewall.go +++ b/cmd/firewall.go @@ -15,7 +15,7 @@ package cmd import ( - "fmt" + "errors" "github.com/spf13/cobra" ) @@ -29,7 +29,7 @@ func Firewall() *cobra.Command { Aliases: []string{"fw"}, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/instance.go b/cmd/instance.go index 994a30e8..c2d16c7f 100644 --- a/cmd/instance.go +++ b/cmd/instance.go @@ -97,7 +97,7 @@ func Instance() *cobra.Command { //nolint: funlen,gocyclo Example: instanceExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/iso.go b/cmd/iso.go index 5972fb1d..c27e7d30 100644 --- a/cmd/iso.go +++ b/cmd/iso.go @@ -34,7 +34,7 @@ func ISO() *cobra.Command { Long: ``, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/kubernetes.go b/cmd/kubernetes.go index 86426c2a..ac0066a7 100644 --- a/cmd/kubernetes.go +++ b/cmd/kubernetes.go @@ -231,7 +231,7 @@ func Kubernetes() *cobra.Command { //nolint: funlen Example: kubernetesExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/loadBalancer.go b/cmd/loadBalancer.go index 5b148b83..badd3823 100644 --- a/cmd/loadBalancer.go +++ b/cmd/loadBalancer.go @@ -88,7 +88,7 @@ func LoadBalancer() *cobra.Command { //nolint: funlen Example: lbExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/network.go b/cmd/network.go index 5cfabcb8..53acc261 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -42,7 +42,7 @@ func Network() *cobra.Command { Deprecated: "Use vpc instead.", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/objectStorage.go b/cmd/objectStorage.go index 80af1583..ef1abc17 100644 --- a/cmd/objectStorage.go +++ b/cmd/objectStorage.go @@ -33,7 +33,7 @@ func ObjectStorageCmd() *cobra.Command { Long: `object-storage is used to interact with the object-storage api`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/reservedIP.go b/cmd/reservedIP.go index 22584fa3..a7575d4b 100644 --- a/cmd/reservedIP.go +++ b/cmd/reservedIP.go @@ -115,7 +115,7 @@ func ReservedIP() *cobra.Command { Example: reservedIPExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/script.go b/cmd/script.go index 0dfdf7ef..835a7b3c 100644 --- a/cmd/script.go +++ b/cmd/script.go @@ -34,7 +34,7 @@ func Script() *cobra.Command { Long: `script is used to access startup script commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/snapshot.go b/cmd/snapshot.go index c6a4aa70..b951ffe7 100644 --- a/cmd/snapshot.go +++ b/cmd/snapshot.go @@ -34,7 +34,7 @@ func Snapshot() *cobra.Command { Long: `snapshot is used to access snapshot commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/sshKey.go b/cmd/sshKey.go index 8f1fbfda..edaa7ee3 100644 --- a/cmd/sshKey.go +++ b/cmd/sshKey.go @@ -34,7 +34,7 @@ func SSHKey() *cobra.Command { Long: `ssh-key is used to access SSH key commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/user.go b/cmd/user.go index 438a6ed5..2e130a15 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -34,7 +34,7 @@ func User() *cobra.Command { Long: `user is used to access user commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/vpc.go b/cmd/vpc.go index f96aa097..53b4f700 100644 --- a/cmd/vpc.go +++ b/cmd/vpc.go @@ -84,7 +84,7 @@ func VPC() *cobra.Command { Example: vpcExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, diff --git a/cmd/vpc2.go b/cmd/vpc2.go index 8eadfdba..af4eb9fb 100644 --- a/cmd/vpc2.go +++ b/cmd/vpc2.go @@ -64,7 +64,7 @@ func VPC2() *cobra.Command { Example: vpc2Example, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if auth := cmd.Context().Value("authenticated"); auth != true { - return fmt.Errorf(apiKeyError) + return errors.New(apiKeyError) } return nil }, From e99c8f524ce373785ae57e9ecb285a646512d0fa Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Tue, 24 Oct 2023 17:11:10 -0400 Subject: [PATCH 4/6] Use struct{} for context key --- cmd/account.go | 2 +- cmd/backups.go | 2 +- cmd/bareMetal.go | 2 +- cmd/billing.go | 2 +- cmd/blockStorage.go | 2 +- cmd/database.go | 2 +- cmd/dns.go | 2 +- cmd/firewall.go | 2 +- cmd/instance.go | 2 +- cmd/iso.go | 2 +- cmd/kubernetes.go | 2 +- cmd/loadBalancer.go | 2 +- cmd/network.go | 2 +- cmd/objectStorage.go | 2 +- cmd/reservedIP.go | 2 +- cmd/root.go | 6 ++++-- cmd/script.go | 2 +- cmd/snapshot.go | 2 +- cmd/sshKey.go | 2 +- cmd/user.go | 2 +- cmd/vpc.go | 2 +- cmd/vpc2.go | 2 +- 22 files changed, 25 insertions(+), 23 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index bdbb01e6..7ee2528a 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -39,7 +39,7 @@ var accountCmd = &cobra.Command{ printer.Account(account) }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/backups.go b/cmd/backups.go index 028c1b38..19211b83 100644 --- a/cmd/backups.go +++ b/cmd/backups.go @@ -31,7 +31,7 @@ func Backups() *cobra.Command { Aliases: []string{"b"}, Short: "Display backups", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/bareMetal.go b/cmd/bareMetal.go index 692f0c2f..d4aec418 100644 --- a/cmd/bareMetal.go +++ b/cmd/bareMetal.go @@ -63,7 +63,7 @@ func BareMetal() *cobra.Command { Long: bareMetalLong, Example: bareMetalExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/billing.go b/cmd/billing.go index 2132ebf6..ce9a6dfb 100644 --- a/cmd/billing.go +++ b/cmd/billing.go @@ -95,7 +95,7 @@ func Billing() *cobra.Command { Long: billingLong, Example: billingExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/blockStorage.go b/cmd/blockStorage.go index cfb1c7c0..ef8b3c58 100644 --- a/cmd/blockStorage.go +++ b/cmd/blockStorage.go @@ -113,7 +113,7 @@ func BlockStorageCmd() *cobra.Command { Short: "block storage commands", Long: `block-storage is used to interact with the block-storage api`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/database.go b/cmd/database.go index 77c2b07a..ab5929f5 100644 --- a/cmd/database.go +++ b/cmd/database.go @@ -67,7 +67,7 @@ func Database() *cobra.Command { //nolint:funlen Long: databaseLong, Example: databaseExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/dns.go b/cmd/dns.go index fd0202a4..322a003a 100644 --- a/cmd/dns.go +++ b/cmd/dns.go @@ -27,7 +27,7 @@ func DNS() *cobra.Command { Short: "dns is used to access dns commands", Long: ``, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/firewall.go b/cmd/firewall.go index 3093917a..2ccfb033 100644 --- a/cmd/firewall.go +++ b/cmd/firewall.go @@ -28,7 +28,7 @@ func Firewall() *cobra.Command { Long: ``, Aliases: []string{"fw"}, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/instance.go b/cmd/instance.go index c2d16c7f..c0bac200 100644 --- a/cmd/instance.go +++ b/cmd/instance.go @@ -96,7 +96,7 @@ func Instance() *cobra.Command { //nolint: funlen,gocyclo Long: instanceLong, Example: instanceExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/iso.go b/cmd/iso.go index c27e7d30..43d6628f 100644 --- a/cmd/iso.go +++ b/cmd/iso.go @@ -33,7 +33,7 @@ func ISO() *cobra.Command { Short: "iso is used to access iso commands", Long: ``, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/kubernetes.go b/cmd/kubernetes.go index ac0066a7..2ee57bc4 100644 --- a/cmd/kubernetes.go +++ b/cmd/kubernetes.go @@ -230,7 +230,7 @@ func Kubernetes() *cobra.Command { //nolint: funlen Long: kubernetesLong, Example: kubernetesExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/loadBalancer.go b/cmd/loadBalancer.go index badd3823..b8a85b0c 100644 --- a/cmd/loadBalancer.go +++ b/cmd/loadBalancer.go @@ -87,7 +87,7 @@ func LoadBalancer() *cobra.Command { //nolint: funlen Long: lbLong, Example: lbExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/network.go b/cmd/network.go index 53acc261..74cdb9d2 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -41,7 +41,7 @@ func Network() *cobra.Command { Long: netLong, Deprecated: "Use vpc instead.", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/objectStorage.go b/cmd/objectStorage.go index ef1abc17..d2d12cd4 100644 --- a/cmd/objectStorage.go +++ b/cmd/objectStorage.go @@ -32,7 +32,7 @@ func ObjectStorageCmd() *cobra.Command { Short: "object storage commands", Long: `object-storage is used to interact with the object-storage api`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/reservedIP.go b/cmd/reservedIP.go index a7575d4b..30d93102 100644 --- a/cmd/reservedIP.go +++ b/cmd/reservedIP.go @@ -114,7 +114,7 @@ func ReservedIP() *cobra.Command { Long: reservedIPLong, Example: reservedIPExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/root.go b/cmd/root.go index dd1a445a..75de4e56 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,6 +37,8 @@ export VULTR_API_KEY='' ` ) +type ctxAuthKey struct{} + var cfgFile string var client *govultr.Client @@ -121,13 +123,13 @@ func initConfig() context.Context { if token == "" { client = govultr.NewClient(nil) - ctx = context.WithValue(ctx, "authenticated", false) + ctx = context.WithValue(ctx, ctxAuthKey{}, false) } else { config := &oauth2.Config{} ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: token}) client = govultr.NewClient(oauth2.NewClient(ctx, ts)) - ctx = context.WithValue(ctx, "authenticated", true) + ctx = context.WithValue(ctx, ctxAuthKey{}, true) } client.SetRateLimit(1 * time.Second) diff --git a/cmd/script.go b/cmd/script.go index 835a7b3c..7e129c2e 100644 --- a/cmd/script.go +++ b/cmd/script.go @@ -33,7 +33,7 @@ func Script() *cobra.Command { Short: "startup script commands", Long: `script is used to access startup script commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/snapshot.go b/cmd/snapshot.go index b951ffe7..7c0124e1 100644 --- a/cmd/snapshot.go +++ b/cmd/snapshot.go @@ -33,7 +33,7 @@ func Snapshot() *cobra.Command { Short: "snapshot commands", Long: `snapshot is used to access snapshot commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/sshKey.go b/cmd/sshKey.go index edaa7ee3..96a9ec67 100644 --- a/cmd/sshKey.go +++ b/cmd/sshKey.go @@ -33,7 +33,7 @@ func SSHKey() *cobra.Command { Short: "ssh-key commands", Long: `ssh-key is used to access SSH key commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/user.go b/cmd/user.go index 2e130a15..baf0e092 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -33,7 +33,7 @@ func User() *cobra.Command { Short: "user commands", Long: `user is used to access user commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/vpc.go b/cmd/vpc.go index 53b4f700..6878d1bd 100644 --- a/cmd/vpc.go +++ b/cmd/vpc.go @@ -83,7 +83,7 @@ func VPC() *cobra.Command { Long: vpcLong, Example: vpcExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil diff --git a/cmd/vpc2.go b/cmd/vpc2.go index af4eb9fb..4f1cfe90 100644 --- a/cmd/vpc2.go +++ b/cmd/vpc2.go @@ -63,7 +63,7 @@ func VPC2() *cobra.Command { Long: vpc2Long, Example: vpc2Example, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if auth := cmd.Context().Value("authenticated"); auth != true { + if cmd.Context().Value(ctxAuthKey{}).(bool) == false { return errors.New(apiKeyError) } return nil From 23c358613a428a2e27e31e4a712552469b9ad7cd Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Tue, 24 Oct 2023 17:19:18 -0400 Subject: [PATCH 5/6] Fix bool comparison and whitespace linting --- cmd/account.go | 2 +- cmd/backups.go | 2 +- cmd/bareMetal.go | 2 +- cmd/billing.go | 2 +- cmd/blockStorage.go | 2 +- cmd/database.go | 2 +- cmd/dns.go | 2 +- cmd/firewall.go | 2 +- cmd/instance.go | 2 +- cmd/iso.go | 2 +- cmd/kubernetes.go | 2 +- cmd/loadBalancer.go | 2 +- cmd/network.go | 2 +- cmd/objectStorage.go | 2 +- cmd/reservedIP.go | 2 +- cmd/root.go | 8 ++++---- cmd/script.go | 2 +- cmd/snapshot.go | 2 +- cmd/sshKey.go | 2 +- cmd/user.go | 2 +- cmd/vpc.go | 2 +- cmd/vpc2.go | 2 +- 22 files changed, 25 insertions(+), 25 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index 7ee2528a..4e641721 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -39,7 +39,7 @@ var accountCmd = &cobra.Command{ printer.Account(account) }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/backups.go b/cmd/backups.go index 19211b83..6414bd6d 100644 --- a/cmd/backups.go +++ b/cmd/backups.go @@ -31,7 +31,7 @@ func Backups() *cobra.Command { Aliases: []string{"b"}, Short: "Display backups", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/bareMetal.go b/cmd/bareMetal.go index d4aec418..3477ed17 100644 --- a/cmd/bareMetal.go +++ b/cmd/bareMetal.go @@ -63,7 +63,7 @@ func BareMetal() *cobra.Command { Long: bareMetalLong, Example: bareMetalExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/billing.go b/cmd/billing.go index ce9a6dfb..28fde1f6 100644 --- a/cmd/billing.go +++ b/cmd/billing.go @@ -95,7 +95,7 @@ func Billing() *cobra.Command { Long: billingLong, Example: billingExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/blockStorage.go b/cmd/blockStorage.go index ef8b3c58..3393fddc 100644 --- a/cmd/blockStorage.go +++ b/cmd/blockStorage.go @@ -113,7 +113,7 @@ func BlockStorageCmd() *cobra.Command { Short: "block storage commands", Long: `block-storage is used to interact with the block-storage api`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/database.go b/cmd/database.go index ab5929f5..00e796f8 100644 --- a/cmd/database.go +++ b/cmd/database.go @@ -67,7 +67,7 @@ func Database() *cobra.Command { //nolint:funlen Long: databaseLong, Example: databaseExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/dns.go b/cmd/dns.go index 322a003a..913ab31b 100644 --- a/cmd/dns.go +++ b/cmd/dns.go @@ -27,7 +27,7 @@ func DNS() *cobra.Command { Short: "dns is used to access dns commands", Long: ``, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/firewall.go b/cmd/firewall.go index 2ccfb033..f312bae2 100644 --- a/cmd/firewall.go +++ b/cmd/firewall.go @@ -28,7 +28,7 @@ func Firewall() *cobra.Command { Long: ``, Aliases: []string{"fw"}, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/instance.go b/cmd/instance.go index c0bac200..34e6b0fb 100644 --- a/cmd/instance.go +++ b/cmd/instance.go @@ -96,7 +96,7 @@ func Instance() *cobra.Command { //nolint: funlen,gocyclo Long: instanceLong, Example: instanceExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/iso.go b/cmd/iso.go index 43d6628f..e5ce629b 100644 --- a/cmd/iso.go +++ b/cmd/iso.go @@ -33,7 +33,7 @@ func ISO() *cobra.Command { Short: "iso is used to access iso commands", Long: ``, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/kubernetes.go b/cmd/kubernetes.go index 2ee57bc4..4c60d0bc 100644 --- a/cmd/kubernetes.go +++ b/cmd/kubernetes.go @@ -230,7 +230,7 @@ func Kubernetes() *cobra.Command { //nolint: funlen Long: kubernetesLong, Example: kubernetesExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/loadBalancer.go b/cmd/loadBalancer.go index b8a85b0c..8fa90922 100644 --- a/cmd/loadBalancer.go +++ b/cmd/loadBalancer.go @@ -87,7 +87,7 @@ func LoadBalancer() *cobra.Command { //nolint: funlen Long: lbLong, Example: lbExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/network.go b/cmd/network.go index 74cdb9d2..234aaf84 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -41,7 +41,7 @@ func Network() *cobra.Command { Long: netLong, Deprecated: "Use vpc instead.", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/objectStorage.go b/cmd/objectStorage.go index d2d12cd4..fe6ec2ed 100644 --- a/cmd/objectStorage.go +++ b/cmd/objectStorage.go @@ -32,7 +32,7 @@ func ObjectStorageCmd() *cobra.Command { Short: "object storage commands", Long: `object-storage is used to interact with the object-storage api`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/reservedIP.go b/cmd/reservedIP.go index 30d93102..429d4f94 100644 --- a/cmd/reservedIP.go +++ b/cmd/reservedIP.go @@ -114,7 +114,7 @@ func ReservedIP() *cobra.Command { Long: reservedIPLong, Example: reservedIPExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/root.go b/cmd/root.go index 75de4e56..a6a445fd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,9 +29,10 @@ import ( ) const ( - userAgent = "vultr-cli/" + version - perPageDefault int = 100 - apiKeyError string = ` + userAgent = "vultr-cli/" + version + perPageDefault int = 100 + //nolint: gosec + apiKeyError string = ` Please export your VULTR API key as an environment variable or add 'api-key' to your config file, eg: export VULTR_API_KEY='' ` @@ -124,7 +125,6 @@ func initConfig() context.Context { if token == "" { client = govultr.NewClient(nil) ctx = context.WithValue(ctx, ctxAuthKey{}, false) - } else { config := &oauth2.Config{} ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: token}) diff --git a/cmd/script.go b/cmd/script.go index 7e129c2e..b37524eb 100644 --- a/cmd/script.go +++ b/cmd/script.go @@ -33,7 +33,7 @@ func Script() *cobra.Command { Short: "startup script commands", Long: `script is used to access startup script commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/snapshot.go b/cmd/snapshot.go index 7c0124e1..dcf4def3 100644 --- a/cmd/snapshot.go +++ b/cmd/snapshot.go @@ -33,7 +33,7 @@ func Snapshot() *cobra.Command { Short: "snapshot commands", Long: `snapshot is used to access snapshot commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/sshKey.go b/cmd/sshKey.go index 96a9ec67..ce1e07b9 100644 --- a/cmd/sshKey.go +++ b/cmd/sshKey.go @@ -33,7 +33,7 @@ func SSHKey() *cobra.Command { Short: "ssh-key commands", Long: `ssh-key is used to access SSH key commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/user.go b/cmd/user.go index baf0e092..261fee32 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -33,7 +33,7 @@ func User() *cobra.Command { Short: "user commands", Long: `user is used to access user commands`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/vpc.go b/cmd/vpc.go index 6878d1bd..2e3f197d 100644 --- a/cmd/vpc.go +++ b/cmd/vpc.go @@ -83,7 +83,7 @@ func VPC() *cobra.Command { Long: vpcLong, Example: vpcExample, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil diff --git a/cmd/vpc2.go b/cmd/vpc2.go index 4f1cfe90..c136baab 100644 --- a/cmd/vpc2.go +++ b/cmd/vpc2.go @@ -63,7 +63,7 @@ func VPC2() *cobra.Command { Long: vpc2Long, Example: vpc2Example, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Context().Value(ctxAuthKey{}).(bool) == false { + if !cmd.Context().Value(ctxAuthKey{}).(bool) { return errors.New(apiKeyError) } return nil From 7c5e5f099d80227a0e105cec2d3e85378e0801f9 Mon Sep 17 00:00:00 2001 From: Michael Riley Date: Tue, 24 Oct 2023 17:20:45 -0400 Subject: [PATCH 6/6] Add comment to ctxAuthKey type --- cmd/root.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/root.go b/cmd/root.go index a6a445fd..af82fdd3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -38,6 +38,7 @@ export VULTR_API_KEY='' ` ) +// ctxAuthKey is the context key for the authorized token check type ctxAuthKey struct{} var cfgFile string