From d3f6ea45749910e54985000178866e6c1eafe326 Mon Sep 17 00:00:00 2001 From: tshak Date: Thu, 12 Mar 2020 19:00:15 +0100 Subject: [PATCH] don't load currentContext until it's actually needed --- go.sum | 7 +++++++ pkg/cmd/apps.go | 22 ++++++++++++---------- pkg/cmd/cmd.go | 22 +++++++++------------- pkg/cmd/cmd_test.go | 13 +++++++++++++ pkg/cmd/context.go | 2 +- pkg/cmd/deploy.go | 3 ++- pkg/cmd/deployments.go | 7 ++++--- pkg/cmd/namespaces.go | 12 +++++++----- pkg/cmd/rollout.go | 3 ++- pkg/cmd/secrets.go | 12 +++++++----- pkg/cmd/stages.go | 7 ++++--- pkg/cmd/status.go | 3 ++- pkg/cmd/util.go | 10 +++++++++- pkg/cmd/validate.go | 3 ++- 14 files changed, 81 insertions(+), 45 deletions(-) create mode 100644 pkg/cmd/cmd_test.go diff --git a/go.sum b/go.sum index b2d9ad6..d66d81c 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,7 @@ github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsC github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ= github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A= @@ -91,6 +92,7 @@ github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ= github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= @@ -121,6 +123,7 @@ github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGV github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ= github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -134,16 +137,20 @@ github.com/riser-platform/riser-server/pkg/sdk v0.0.32/go.mod h1:+0WkYBFj75mpTJE github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk= github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= +github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.4 h1:S0tLZ3VOKl2Te0hpq8+ke0eSJPfCnNTPiDlsfwi1/NE= github.com/spf13/cobra v0.0.4/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= diff --git a/pkg/cmd/apps.go b/pkg/cmd/apps.go index 970978d..ea0425e 100644 --- a/pkg/cmd/apps.go +++ b/pkg/cmd/apps.go @@ -16,24 +16,25 @@ import ( const AppConfigPath = "./app.yaml" -func newAppsCommand(currentContext *rc.Context) *cobra.Command { +func newAppsCommand(config *rc.RuntimeConfiguration) *cobra.Command { cmd := &cobra.Command{ Use: "apps", Short: "Commands for managing apps", } - cmd.AddCommand(newAppsListCommand(currentContext)) - cmd.AddCommand(newAppsNewCommand(currentContext)) - cmd.AddCommand(newAppsInitCommand(currentContext)) + cmd.AddCommand(newAppsListCommand(config)) + cmd.AddCommand(newAppsNewCommand(config)) + cmd.AddCommand(newAppsInitCommand(config)) return cmd } -func newAppsListCommand(currentContext *rc.Context) *cobra.Command { +func newAppsListCommand(config *rc.RuntimeConfiguration) *cobra.Command { return &cobra.Command{ Use: "list", Short: "Lists all apps", Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(config) riserClient := getRiserClient(currentContext) apps, err := riserClient.Apps.List() ui.ExitIfError(err) @@ -49,7 +50,7 @@ func newAppsListCommand(currentContext *rc.Context) *cobra.Command { } } -func newAppsInitCommand(currentContext *rc.Context) *cobra.Command { +func newAppsInitCommand(config *rc.RuntimeConfiguration) *cobra.Command { var namespace string cmd := &cobra.Command{ Use: "init (app name)", @@ -57,7 +58,7 @@ func newAppsInitCommand(currentContext *rc.Context) *cobra.Command { Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { appName := args[0] - app := createNewApp(currentContext, appName, namespace) + app := createNewApp(config, appName, namespace) file, err := os.OpenFile(AppConfigPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644) ui.ExitIfErrorMsg(err, "Error creating default app config") @@ -74,7 +75,7 @@ func newAppsInitCommand(currentContext *rc.Context) *cobra.Command { return cmd } -func newAppsNewCommand(currentContext *rc.Context) *cobra.Command { +func newAppsNewCommand(config *rc.RuntimeConfiguration) *cobra.Command { var namespace string cmd := &cobra.Command{ Use: "new (app name)", @@ -82,7 +83,7 @@ func newAppsNewCommand(currentContext *rc.Context) *cobra.Command { Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { appName := args[0] - app := createNewApp(currentContext, appName, namespace) + app := createNewApp(config, appName, namespace) fmt.Printf("App %s created. Please add the following id to your manifest: %s", app.Name, app.Id) }, @@ -93,7 +94,8 @@ func newAppsNewCommand(currentContext *rc.Context) *cobra.Command { return cmd } -func createNewApp(currentContext *rc.Context, appName, namespace string) *model.App { +func createNewApp(config *rc.RuntimeConfiguration, appName, namespace string) *model.App { + currentContext := safeCurrentContext(config) riserClient := getRiserClient(currentContext) app, err := riserClient.Apps.Create(&model.NewApp{Name: model.AppName(appName), Namespace: model.NamespaceName(namespace)}) ui.ExitIfError(err) diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 7eea17f..9d95d30 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -13,10 +13,6 @@ var verbose bool // Execute creates the root command and executes it func Execute(runtime *Runtime) { - currentContext, _ := runtime.Configuration.CurrentContext() - // TODO: Lazy load current context os that we don't have an error when using context commands which don't require the current context to be set - // ui.ExitIfErrorMsg(err, "Error loading current context") - cmd := &cobra.Command{ Use: os.Args[0], Short: "Riser platform", @@ -25,18 +21,18 @@ func Execute(runtime *Runtime) { }, } - cmd.AddCommand(newAppsCommand(currentContext)) + cmd.AddCommand(newAppsCommand(runtime.Configuration)) cmd.AddCommand(newContextCommand(runtime.Configuration)) cmd.AddCommand(newDemoCommand(runtime.Configuration, runtime.Assets)) - cmd.AddCommand(newDeployCommand(currentContext)) - cmd.AddCommand(newDeploymentsCommand(currentContext)) - cmd.AddCommand(newNamespacesCommand(currentContext)) + cmd.AddCommand(newDeployCommand(runtime.Configuration)) + cmd.AddCommand(newDeploymentsCommand(runtime.Configuration)) + cmd.AddCommand(newNamespacesCommand(runtime.Configuration)) cmd.AddCommand(newOpsCommand()) - cmd.AddCommand(newRolloutCommand(currentContext)) - cmd.AddCommand(newStagesCommand(currentContext)) - cmd.AddCommand(newSecretsCommand(currentContext)) - cmd.AddCommand(newStatusCommand(currentContext)) - cmd.AddCommand(newValidateCommand(currentContext)) + cmd.AddCommand(newRolloutCommand(runtime.Configuration)) + cmd.AddCommand(newStagesCommand(runtime.Configuration)) + cmd.AddCommand(newSecretsCommand(runtime.Configuration)) + cmd.AddCommand(newStatusCommand(runtime.Configuration)) + cmd.AddCommand(newValidateCommand(runtime.Configuration)) cmd.AddCommand(newVersionCmd(runtime.Version)) cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") diff --git a/pkg/cmd/cmd_test.go b/pkg/cmd/cmd_test.go new file mode 100644 index 0000000..6e69115 --- /dev/null +++ b/pkg/cmd/cmd_test.go @@ -0,0 +1,13 @@ +package cmd + +import ( + "riser/pkg/rc" + "testing" +) + +func Test_CreateCmd_DoesNotExitOnEmptyRC(t *testing.T) { + // This guards against someone attempting to access the currentContext while building up the commands + Execute(&Runtime{ + Configuration: &rc.RuntimeConfiguration{}, + }) +} diff --git a/pkg/cmd/context.go b/pkg/cmd/context.go index cd84b3e..a68f0cf 100644 --- a/pkg/cmd/context.go +++ b/pkg/cmd/context.go @@ -90,7 +90,7 @@ func newContextListCommand(config *rc.RuntimeConfiguration) *cobra.Command { Run: func(cmd *cobra.Command, args []string) { contexts := config.GetContexts() if len(contexts) == 0 { - logger.Log().Info("No contexts configured. Use \"riser context add\" to add a new context") + logger.Log().Info("No contexts configured. Use \"riser context save\" to add a new context") } else { for _, context := range contexts { logger.Log().Info(context.Name) diff --git a/pkg/cmd/deploy.go b/pkg/cmd/deploy.go index d3c431c..1a8524a 100644 --- a/pkg/cmd/deploy.go +++ b/pkg/cmd/deploy.go @@ -14,7 +14,7 @@ import ( "github.com/spf13/cobra" ) -func newDeployCommand(currentContext *rc.Context) *cobra.Command { +func newDeployCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { var appFilePath string var dryRun bool var deploymentName string @@ -24,6 +24,7 @@ func newDeployCommand(currentContext *rc.Context) *cobra.Command { Short: "Creates a new deployment or revision", Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) dockerTag := args[0] stage := args[1] diff --git a/pkg/cmd/deployments.go b/pkg/cmd/deployments.go index a1b5441..84c1cb3 100644 --- a/pkg/cmd/deployments.go +++ b/pkg/cmd/deployments.go @@ -9,19 +9,19 @@ import ( "github.com/spf13/cobra" ) -func newDeploymentsCommand(currentContext *rc.Context) *cobra.Command { +func newDeploymentsCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { cmd := &cobra.Command{ Use: "deployments", Short: "Commands for managing deployments", Long: "Commands for managing deployments. Use \"riser deploy\" to create a new deployment or revision.", } - cmd.AddCommand(newDeploymentsDeleteCommand(currentContext)) + cmd.AddCommand(newDeploymentsDeleteCommand(runtimeConfig)) return cmd } -func newDeploymentsDeleteCommand(currentContext *rc.Context) *cobra.Command { +func newDeploymentsDeleteCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { var namespace string noPrompt := false cmd := &cobra.Command{ @@ -29,6 +29,7 @@ func newDeploymentsDeleteCommand(currentContext *rc.Context) *cobra.Command { Short: "Permanentally deletes a deployment and all of its revisions in the specified stage", Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) deleteConfirmed := false deploymentName := args[0] stageName := args[1] diff --git a/pkg/cmd/namespaces.go b/pkg/cmd/namespaces.go index 0b91234..4bb5af1 100644 --- a/pkg/cmd/namespaces.go +++ b/pkg/cmd/namespaces.go @@ -10,23 +10,24 @@ import ( "github.com/spf13/cobra" ) -func newNamespacesCommand(currentContext *rc.Context) *cobra.Command { +func newNamespacesCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { cmd := &cobra.Command{ Use: "namespaces", Short: "Commands for managing namespaces", } - cmd.AddCommand(newNamespacesCreateCommand(currentContext)) - cmd.AddCommand(newNamespacesListCommand(currentContext)) + cmd.AddCommand(newNamespacesCreateCommand(runtimeConfig)) + cmd.AddCommand(newNamespacesListCommand(runtimeConfig)) return cmd } -func newNamespacesCreateCommand(currentContext *rc.Context) *cobra.Command { +func newNamespacesCreateCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { return &cobra.Command{ Use: "create (namespace name)", Short: "Create a new namespace", Args: cobra.ExactArgs(1), Run: func(_ *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) namespaceName := args[0] riserClient := getRiserClient(currentContext) err := riserClient.Namespaces.Create(namespaceName) @@ -36,11 +37,12 @@ func newNamespacesCreateCommand(currentContext *rc.Context) *cobra.Command { } } -func newNamespacesListCommand(currentContext *rc.Context) *cobra.Command { +func newNamespacesListCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { return &cobra.Command{ Use: "list", Short: "List all namespaces", Run: func(*cobra.Command, []string) { + currentContext := safeCurrentContext(runtimeConfig) riserClient := getRiserClient(currentContext) namespaces, err := riserClient.Namespaces.List() ui.ExitIfErrorMsg(err, "error listing namespaces") diff --git a/pkg/cmd/rollout.go b/pkg/cmd/rollout.go index 6986f9a..d875ce6 100644 --- a/pkg/cmd/rollout.go +++ b/pkg/cmd/rollout.go @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -func newRolloutCommand(currentContext *rc.Context) *cobra.Command { +func newRolloutCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { var deploymentName string var namespace string cmd := &cobra.Command{ @@ -18,6 +18,7 @@ func newRolloutCommand(currentContext *rc.Context) *cobra.Command { Args: cobra.MinimumNArgs(2), Example: " riser rollout prod rev-1:90 rev-2:10 // Canary routing 10% of traffic to a new revision \n riser rollout prod rev-2:100 // Route all traffic to rev 2", Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) stage := args[0] riserClient := getRiserClient(currentContext) err := riserClient.Rollouts.Save(deploymentName, namespace, stage, args[1:]...) diff --git a/pkg/cmd/secrets.go b/pkg/cmd/secrets.go index e038f2b..054f2bc 100644 --- a/pkg/cmd/secrets.go +++ b/pkg/cmd/secrets.go @@ -9,18 +9,18 @@ import ( "github.com/spf13/cobra" ) -func newSecretsCommand(currentContext *rc.Context) *cobra.Command { +func newSecretsCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { cmd := &cobra.Command{ Use: "secrets", Short: "Commands for secrets", } - cmd.AddCommand(newSecretsListCommand(currentContext)) - cmd.AddCommand(newSecretsSaveCommand(currentContext)) + cmd.AddCommand(newSecretsListCommand(runtimeConfig)) + cmd.AddCommand(newSecretsSaveCommand(runtimeConfig)) return cmd } -func newSecretsSaveCommand(currentContext *rc.Context) *cobra.Command { +func newSecretsSaveCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { var appName string var namespace string cmd := &cobra.Command{ @@ -29,6 +29,7 @@ func newSecretsSaveCommand(currentContext *rc.Context) *cobra.Command { Long: "Creates a new secret or updates an existing one. Secrets are stored seperately per app and stage.", Args: cobra.ExactArgs(3), Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) secretName := args[0] plainTextSecret := args[1] stageName := args[2] @@ -47,7 +48,7 @@ func newSecretsSaveCommand(currentContext *rc.Context) *cobra.Command { return cmd } -func newSecretsListCommand(currentContext *rc.Context) *cobra.Command { +func newSecretsListCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { var appName string var namespace string cmd := &cobra.Command{ @@ -55,6 +56,7 @@ func newSecretsListCommand(currentContext *rc.Context) *cobra.Command { Short: "Lists secrets configured for a given stage", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) stageName := args[0] riserClient := getRiserClient(currentContext) diff --git a/pkg/cmd/stages.go b/pkg/cmd/stages.go index 9c21556..46889b7 100644 --- a/pkg/cmd/stages.go +++ b/pkg/cmd/stages.go @@ -9,23 +9,24 @@ import ( "github.com/spf13/cobra" ) -func newStagesCommand(currentContext *rc.Context) *cobra.Command { +func newStagesCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { cmd := &cobra.Command{ Use: "stages", Short: "Commands for stages.", Long: "Commands for stages. A stage represents a single kubernetes cluster. Stages are commonly have names like \"dev\", \"test\", or \"prod\". Stages are created automatically after installing the riser controller in a cluster.", } - cmd.AddCommand(newStagesListCommand(currentContext)) + cmd.AddCommand(newStagesListCommand(runtimeConfig)) return cmd } -func newStagesListCommand(currentContext *rc.Context) *cobra.Command { +func newStagesListCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { return &cobra.Command{ Use: "list", Short: "Lists all available stages", Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) riserClient := getRiserClient(currentContext) stages, err := riserClient.Stages.List() ui.ExitIfError(err) diff --git a/pkg/cmd/status.go b/pkg/cmd/status.go index f245135..1dcd3c1 100644 --- a/pkg/cmd/status.go +++ b/pkg/cmd/status.go @@ -15,7 +15,7 @@ import ( "github.com/wzshiming/ctc" ) -func newStatusCommand(currentContext *rc.Context) *cobra.Command { +func newStatusCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { var appName string var namespace string showAllRevisions := false @@ -23,6 +23,7 @@ func newStatusCommand(currentContext *rc.Context) *cobra.Command { Use: "status", Short: "Gets the status for a deployment.", Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) riserClient := getRiserClient(currentContext) status, err := riserClient.Apps.GetStatus(appName, namespace) diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go index 2153db1..e544867 100644 --- a/pkg/cmd/util.go +++ b/pkg/cmd/util.go @@ -1,11 +1,19 @@ package cmd import ( - "github.com/riser-platform/riser-server/pkg/sdk" "riser/pkg/rc" "riser/pkg/ui" + + "github.com/riser-platform/riser-server/pkg/sdk" ) +// safeCurrentContext loads the CurrentContext and exits if there is any error +func safeCurrentContext(cfg *rc.RuntimeConfiguration) *rc.Context { + context, err := cfg.CurrentContext() + ui.ExitIfError(err) + return context +} + func getRiserClient(c *rc.Context) *sdk.Client { client, err := sdk.NewClient(c.ServerURL, c.Apikey) ui.ExitIfErrorMsg(err, "Error instantiating riser SDK") diff --git a/pkg/cmd/validate.go b/pkg/cmd/validate.go index 7e17a74..d37f162 100644 --- a/pkg/cmd/validate.go +++ b/pkg/cmd/validate.go @@ -10,12 +10,13 @@ import ( "github.com/spf13/cobra" ) -func newValidateCommand(currentContext *rc.Context) *cobra.Command { +func newValidateCommand(runtimeConfig *rc.RuntimeConfiguration) *cobra.Command { var appFilePath string cmd := &cobra.Command{ Use: "validate", Short: "Validates an app config", Run: func(cmd *cobra.Command, args []string) { + currentContext := safeCurrentContext(runtimeConfig) app, err := config.LoadApp(appFilePath) if err == nil { riserClient := getRiserClient(currentContext)