diff --git a/cli/accounts.go b/command/accounts.go similarity index 99% rename from cli/accounts.go rename to command/accounts.go index 1b3a698b..7227dbb8 100644 --- a/cli/accounts.go +++ b/command/accounts.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cli/alias.go b/command/alias.go similarity index 96% rename from cli/alias.go rename to command/alias.go index 54a17486..ad8ced7c 100644 --- a/cli/alias.go +++ b/command/alias.go @@ -1,4 +1,4 @@ -package main +package command import ( "github.com/spf13/cobra" diff --git a/cli/awsconfig.go b/command/awsconfig.go similarity index 99% rename from cli/awsconfig.go rename to command/awsconfig.go index a26bec4c..692784d4 100644 --- a/cli/awsconfig.go +++ b/command/awsconfig.go @@ -1,4 +1,4 @@ -package main +package command import ( "os" diff --git a/cli/awsconfig_test.go b/command/awsconfig_test.go similarity index 98% rename from cli/awsconfig_test.go rename to command/awsconfig_test.go index 2e5ac322..dadb8c33 100644 --- a/cli/awsconfig_test.go +++ b/command/awsconfig_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "bytes" diff --git a/cli/config.go b/command/config.go similarity index 99% rename from cli/config.go rename to command/config.go index 65f7106b..adab2ceb 100644 --- a/cli/config.go +++ b/command/config.go @@ -1,4 +1,4 @@ -package main +package command import ( "encoding/csv" diff --git a/cli/config_test.go b/command/config_test.go similarity index 99% rename from cli/config_test.go rename to command/config_test.go index 4e371ae9..0b3c77e6 100644 --- a/cli/config_test.go +++ b/command/config_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "encoding/json" diff --git a/cli/consts.go b/command/consts.go similarity index 97% rename from cli/consts.go rename to command/consts.go index 7c151184..7a1ce273 100644 --- a/cli/consts.go +++ b/command/consts.go @@ -1,4 +1,4 @@ -package main +package command // Vars for build time var ( diff --git a/cli/context.go b/command/context.go similarity index 97% rename from cli/context.go rename to command/context.go index 50f09056..2329c49c 100644 --- a/cli/context.go +++ b/command/context.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cli/credentials.go b/command/credentials.go similarity index 99% rename from cli/credentials.go rename to command/credentials.go index 8d86e9d0..2c2bef4a 100644 --- a/cli/credentials.go +++ b/command/credentials.go @@ -1,4 +1,4 @@ -package main +package command import ( "fmt" diff --git a/cli/credentials_test.go b/command/credentials_test.go similarity index 99% rename from cli/credentials_test.go rename to command/credentials_test.go index e7c84585..08855508 100644 --- a/cli/credentials_test.go +++ b/command/credentials_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "testing" diff --git a/cli/error.go b/command/error.go similarity index 96% rename from cli/error.go rename to command/error.go index eb214217..b3f888b6 100644 --- a/cli/error.go +++ b/command/error.go @@ -1,4 +1,4 @@ -package main +package command import ( "errors" @@ -168,3 +168,11 @@ func tryParseTimeToLiveError(err error) (error, bool) { return nil, false } + +func GetExitCode(err error) (int, bool) { + var codeError codeError + if errors.As(err, &codeError) { + return codeError.Code(), true + } + return 0, false +} diff --git a/cli/error_test.go b/command/error_test.go similarity index 98% rename from cli/error_test.go rename to command/error_test.go index 356eddde..224061aa 100644 --- a/cli/error_test.go +++ b/command/error_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "testing" diff --git a/cli/get.go b/command/get.go similarity index 99% rename from cli/get.go rename to command/get.go index 148b2de2..abbbe7e4 100644 --- a/cli/get.go +++ b/command/get.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cli/html.go b/command/html.go similarity index 99% rename from cli/html.go rename to command/html.go index cd8c753c..3380484e 100644 --- a/cli/html.go +++ b/command/html.go @@ -1,4 +1,4 @@ -package main +package command import ( "errors" diff --git a/cli/html_test.go b/command/html_test.go similarity index 99% rename from cli/html_test.go rename to command/html_test.go index 67276827..b1bfb12c 100644 --- a/cli/html_test.go +++ b/command/html_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "strings" diff --git a/cli/log.go b/command/log.go similarity index 98% rename from cli/log.go rename to command/log.go index d353f056..7742ae38 100644 --- a/cli/log.go +++ b/command/log.go @@ -1,4 +1,4 @@ -package main +package command import ( "net/http" diff --git a/cli/login.go b/command/login.go similarity index 99% rename from cli/login.go rename to command/login.go index 5f559e13..82bfb74f 100644 --- a/cli/login.go +++ b/command/login.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cli/login_test.go b/command/login_test.go similarity index 99% rename from cli/login_test.go rename to command/login_test.go index 28d02fa7..98db8d0a 100644 --- a/cli/login_test.go +++ b/command/login_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cli/oauth2.go b/command/oauth2.go similarity index 99% rename from cli/oauth2.go rename to command/oauth2.go index 3723a966..805bb270 100644 --- a/cli/oauth2.go +++ b/command/oauth2.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cli/oauth2_test.go b/command/oauth2_test.go similarity index 99% rename from cli/oauth2_test.go rename to command/oauth2_test.go index 09d91a02..02fefc89 100644 --- a/cli/oauth2_test.go +++ b/command/oauth2_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "net/http" diff --git a/cli/roles.go b/command/roles.go similarity index 98% rename from cli/roles.go rename to command/roles.go index cb406b9d..07e0145b 100644 --- a/cli/roles.go +++ b/command/roles.go @@ -1,4 +1,4 @@ -package main +package command import ( "github.com/spf13/cobra" diff --git a/cli/root.go b/command/root.go similarity index 93% rename from cli/root.go rename to command/root.go index 1ddb6d2f..e008d842 100644 --- a/cli/root.go +++ b/command/root.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" @@ -8,6 +8,7 @@ import ( "runtime" "time" + "github.com/coreos/go-oidc" "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" ) @@ -103,3 +104,10 @@ To get started run the following commands: SilenceErrors: true, SilenceUsage: true, } + +func Execute(ctx context.Context, args []string) error { + client := NewHTTPClient() + ctx = oidc.ClientContext(ctx, client) + rootCmd.SetArgs(args) + return rootCmd.ExecuteContext(ctx) +} diff --git a/cli/root_test.go b/command/root_test.go similarity index 99% rename from cli/root_test.go rename to command/root_test.go index 53110b34..3864e3d7 100644 --- a/cli/root_test.go +++ b/command/root_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "bytes" diff --git a/cli/saml.go b/command/saml.go similarity index 99% rename from cli/saml.go rename to command/saml.go index 575e980e..bca1d1ca 100644 --- a/cli/saml.go +++ b/command/saml.go @@ -1,4 +1,4 @@ -package main +package command import ( "strings" diff --git a/cli/saml_test.go b/command/saml_test.go similarity index 98% rename from cli/saml_test.go rename to command/saml_test.go index 904b8f9a..60151499 100644 --- a/cli/saml_test.go +++ b/command/saml_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "testing" diff --git a/cli/set.go b/command/set.go similarity index 98% rename from cli/set.go rename to command/set.go index 894370f8..d071b0c4 100644 --- a/cli/set.go +++ b/command/set.go @@ -1,4 +1,4 @@ -package main +package command import ( "fmt" diff --git a/cli/switch.go b/command/switch.go similarity index 99% rename from cli/switch.go rename to command/switch.go index 3915f13b..647bc8f7 100644 --- a/cli/switch.go +++ b/command/switch.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cli/unalias.go b/command/unalias.go similarity index 95% rename from cli/unalias.go rename to command/unalias.go index ab0a6a51..aef1ea05 100644 --- a/cli/unalias.go +++ b/command/unalias.go @@ -1,4 +1,4 @@ -package main +package command import ( "github.com/spf13/cobra" diff --git a/cli/main.go b/main.go similarity index 75% rename from cli/main.go rename to main.go index 09981da1..2dfa7880 100644 --- a/cli/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "log/slog" - "github.com/coreos/go-oidc" + "github.com/riotgames/key-conjurer/command" "github.com/spf13/cobra" ) @@ -42,23 +42,21 @@ func main() { if flag, ok := os.LookupEnv("KEYCONJURERFLAGS"); ok { args = append(args, strings.Split(flag, " ")...) } - rootCmd.SetArgs(args) - ctx := oidc.ClientContext(context.Background(), NewHTTPClient()) - err := rootCmd.ExecuteContext(ctx) + err := command.Execute(context.Background(), args) if IsWindowsPortAccessError(err) { fmt.Fprintf(os.Stderr, "Encountered an issue when opening the port for KeyConjurer: %s\n", err) fmt.Fprintln(os.Stderr, "Consider running `net stop hns` and then `net start hns`") - os.Exit(ExitCodeConnectivityError) + os.Exit(command.ExitCodeConnectivityError) } - var codeErr codeError - if errors.As(err, &codeErr) { - cobra.CheckErr(codeErr) - os.Exit(int(codeErr.Code())) - } else if err != nil { - // Probably a cobra error. + if err != nil { cobra.CheckErr(err) - os.Exit(ExitCodeUnknownError) + + errorCode, ok := command.GetExitCode(err) + if !ok { + errorCode = command.ExitCodeUnknownError + } + os.Exit(errorCode) } }