Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add API key flag to CLI command #1121

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions mgc/cli/cmd/apiKey_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import "github.com/spf13/cobra"

const (
apiKeyFlag = "api-key"
)

type APIKeyParameters struct {
Key string
}

func (a APIKeyParameters) GetAPIKey() string {
return a.Key
}

func addApiKeyFlag(cmd *cobra.Command) {
cmd.Root().PersistentFlags().String(
apiKeyFlag,
"",
"Use your API key to authenticate with the API",
)
}

func getApiKeyFlag(cmd *cobra.Command) string {
apiKey, err := cmd.Root().PersistentFlags().GetString(apiKeyFlag)
if err != nil {
return ""
}
return apiKey
}
37 changes: 36 additions & 1 deletion mgc/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"
"regexp"
"runtime"
"slices"
Expand All @@ -20,6 +21,7 @@ const (
loggerConfigKey = "logging"
defaultRegion = "br-se1"
defaultOutputFormat = "yaml"
apiKeyEnvVar = "MGC_API_KEY"
)

var argParser = &osArgParser{}
Expand Down Expand Up @@ -73,7 +75,7 @@ can generate a command line on-demand for Rest manipulation`,
addShowInternalFlag(rootCmd)
addShowHiddenFlag(rootCmd)
addRawOutputFlag(rootCmd)

addApiKeyFlag(rootCmd)
rootCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) { f.Hidden = true })

rootCmd.InitDefaultHelpFlag()
Expand All @@ -92,6 +94,11 @@ can generate a command line on-demand for Rest manipulation`,
if err == nil || len(args) == 0 {
break
}

if strings.HasPrefix(err.Error(), "flag needs an argument:") {
break
}

flag, found := strings.CutPrefix(err.Error(), "unknown flag: ")
if found && len(flag) > 0 {
skipTo := slices.IndexFunc(args, func(arg string) bool {
Expand Down Expand Up @@ -143,6 +150,8 @@ can generate a command line on-demand for Rest manipulation`,

setDefaultRegion(sdk)
setDefaultOutputFormat(sdk)
setApiKey(rootCmd, sdk)
setKeyPair(sdk)

err = rootCmd.Execute()
if err == nil && loadErr != nil {
Expand All @@ -153,6 +162,32 @@ can generate a command line on-demand for Rest manipulation`,
return err
}

func setKeyPair(sdk *mgcSdk.Sdk) {
objId := os.Getenv("MGC_OBJ_KEY_ID")
objKey := os.Getenv("MGC_OBJ_KEY_SECRET")

if objId != "" && objKey != "" {
sdk.Config().AddTempKeyPair("apikey",
objId,
objKey,
)
}
}

func setApiKey(rootCmd *cobra.Command, sdk *mgcSdk.Sdk) {
if key := getApiKeyFlag(rootCmd); key != "" {
apiKeyParameters := APIKeyParameters{
Key: key,
}
_ = sdk.Auth().SetAPIKey(apiKeyParameters)
} else if key := os.Getenv(apiKeyEnvVar); key != "" {
apiKeyParameters := APIKeyParameters{
Key: key,
}
_ = sdk.Auth().SetAPIKey(apiKeyParameters)
}
}

func getLastFlag(s string) string {
re := regexp.MustCompile(`-(\w)`)
matches := re.FindAllStringSubmatch(s, -1)
Expand Down
Loading