Skip to content

Commit

Permalink
don't load currentContext until it's actually needed
Browse files Browse the repository at this point in the history
  • Loading branch information
tshak committed Mar 12, 2020
1 parent 46394fd commit d3f6ea4
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 45 deletions.
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
22 changes: 12 additions & 10 deletions pkg/cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -49,15 +50,15 @@ 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)",
Short: "Creates a new app with a default app.yaml file",
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")
Expand All @@ -74,15 +75,15 @@ 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)",
Short: "Creates a new app",
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)
},
Expand All @@ -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)
Expand Down
22 changes: 9 additions & 13 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")

Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -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{},
})
}
2 changes: 1 addition & 1 deletion pkg/cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]

Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ 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{
Use: "delete (deploymentName) (stage)",
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]
Expand Down
12 changes: 7 additions & 5 deletions pkg/cmd/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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:]...)
Expand Down
12 changes: 7 additions & 5 deletions pkg/cmd/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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]
Expand All @@ -47,14 +48,15 @@ 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{
Use: "list (stage)",
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)

Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ 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
cmd := &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)
Expand Down
Loading

0 comments on commit d3f6ea4

Please sign in to comment.