Skip to content

Commit

Permalink
Add pre-execute logic to auth-required commands
Browse files Browse the repository at this point in the history
  • Loading branch information
optik-aper committed Oct 23, 2023
1 parent a7d5b8e commit 72a16c1
Show file tree
Hide file tree
Showing 21 changed files with 130 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
6 changes: 6 additions & 0 deletions cmd/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/bareMetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions cmd/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions cmd/blockStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions cmd/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -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())
Expand Down
8 changes: 8 additions & 0 deletions cmd/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions cmd/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/loadBalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/objectStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions cmd/reservedIP.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions cmd/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/sshKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/vpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 72a16c1

Please sign in to comment.