Skip to content

Commit

Permalink
init command wont switch workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalkaoz committed Mar 2, 2022
1 parent 053da66 commit a9d8452
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewApplyCommand(root *cobra.Command) {
Args: lib.ArgsValidator,

Run: func(cmd *cobra.Command, args []string) {
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1])
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1], true)

planFile := fmt.Sprintf("%s-%s.tfplan", time.Now().Format(time.RFC3339), args[0])

Expand Down
2 changes: 1 addition & 1 deletion cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewDestroyCommand(root *cobra.Command) {
Args: lib.ArgsValidator,

Run: func(cmd *cobra.Command, args []string) {
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1])
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1], true)

_ = tf.Destroy(ctx, buildDestroyOptions(files, args)...)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewImportCommand(root *cobra.Command) {
Example: "import prod path/to/stack aws_s3_bucket.example some_aws_bucket_name",
Args: importArgsValidator,
Run: func(cmd *cobra.Command, args []string) {
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1])
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1], true)

_ = tf.Import(ctx, args[2], args[3], buildImportOptions(files, args)...)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ These variables can be defined by your *.tfvars.json or through command options
Example: "init workspace path/to/stack --state-bucket=my_own_bucket_id --state-dynamo=my_dynamo_table --state-region=us-east-1 --state-account=4711 --state-name=my_state_entry_name",
Args: lib.ArgsValidator,
Run: func(cmd *cobra.Command, args []string) {
tf, ctx, _, mergedVars := lib.Executor(*cmd, args[0], args[1])
tf, ctx, _, mergedVars := lib.Executor(*cmd, args[0], args[1], false)

//init
_ = tf.Init(ctx, buildInitOptions(*cmd, mergedVars, args)...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewPlanCommand(root *cobra.Command) {
Args: lib.ArgsValidator,

Run: func(cmd *cobra.Command, args []string) {
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1])
tf, ctx, files, _ := lib.Executor(*cmd, args[0], args[1], true)

//plan
_, _ = tf.Plan(ctx, buildPlanOptions(files, args, "")...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewRemoveCommand(root *cobra.Command) {
Example: "remove prod path/to/stack aws_s3_bucket.example",
Args: removeArgsValidator,
Run: func(cmd *cobra.Command, args []string) {
tf, ctx, _, _ := lib.Executor(*cmd, args[0], args[1])
tf, ctx, _, _ := lib.Executor(*cmd, args[0], args[1], true)

_ = tf.StateRm(ctx, args[2])
},
Expand Down
27 changes: 0 additions & 27 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ func TestInitCommandWithoutRemoteState(t *testing.T) {
out := runCommand(t, args)
t.Log(out)

if !strings.Contains(out, "workspace new dev") {
t.Errorf("missing create workspace")
}
if !strings.Contains(out, "workspace select dev") {
t.Errorf("missing switch workspace")
}
if !strings.Contains(out, "version -json") {
t.Errorf("missing switch workspace")
}
if !strings.Contains(out, "init -force-copy -input=false -backend=false -get=true -upgrade=true") {
t.Errorf("invalid init command")
}
Expand All @@ -76,15 +67,6 @@ func TestInitCommandWithRemoteStateButNoLocking(t *testing.T) {
out := runCommand(t, args)
t.Log(out)

if !strings.Contains(out, "workspace new dev") {
t.Errorf("missing create workspace")
}
if !strings.Contains(out, "workspace select dev") {
t.Errorf("missing switch workspace")
}
if !strings.Contains(out, "version -json") {
t.Errorf("missing switch workspace")
}
if !strings.Contains(out, "init -force-copy -input=false -backend=true -get=true -upgrade=true -backend-config=region=eu-central-1 -backend-config=bucket=tf-state-terrarium-cli-eu-central-1-455201159890 -backend-config=key=stack.tfstate") {
t.Errorf("invalid init command")
}
Expand All @@ -95,15 +77,6 @@ func TestInitCommand(t *testing.T) {
out := runCommand(t, args)
t.Log(out)

if !strings.Contains(out, "workspace new dev") {
t.Errorf("missing create workspace")
}
if !strings.Contains(out, "workspace select dev") {
t.Errorf("missing switch workspace")
}
if !strings.Contains(out, "version -json") {
t.Errorf("missing switch workspace")
}
if !strings.Contains(out, "init -force-copy -input=false -backend=true -get=true -upgrade=true -backend-config=region=eu-central-1 -backend-config=bucket=tf-state-terrarium-cli-eu-central-1-455201159890 -backend-config=key=stack.tfstate -backend-config=dynamodb_table=terraform-lock-terrarium-cli-eu-central-1-455201159890") {
t.Errorf("invalid init command")
}
Expand Down
6 changes: 4 additions & 2 deletions lib/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Binary() string {
return path
}

func Executor(cmd cobra.Command, workspace string, path string) (*tfexec.Terraform, context.Context, []string, map[string]interface{}) {
func Executor(cmd cobra.Command, workspace string, path string, switchWorkspace bool) (*tfexec.Terraform, context.Context, []string, map[string]interface{}) {
binary, err := cmd.Parent().PersistentFlags().GetString("terraform")

if err != nil {
Expand All @@ -43,7 +43,9 @@ func Executor(cmd cobra.Command, workspace string, path string) (*tfexec.Terrafo
tf.SetStderr(cmd.ErrOrStderr())

ctx := context.Background()
Workspace(tf, ctx, cmd, workspace)
if switchWorkspace {
Workspace(tf, ctx, cmd, workspace)
}

files, vars := Vars(cmd, workspace, path)

Expand Down

0 comments on commit a9d8452

Please sign in to comment.