Skip to content

Commit

Permalink
implement DIR positional arg (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe authored Aug 17, 2020
1 parent 63af3ce commit cf7aa80
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
10 changes: 10 additions & 0 deletions tfexec/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type destroyConfig struct {
backup string
dir string
lock bool

// LockTimeout must be a string with time unit, e.g. '10s'
Expand All @@ -35,6 +36,10 @@ type DestroyOption interface {
configureDestroy(*destroyConfig)
}

func (opt *DirOption) configureDestroy(conf *destroyConfig) {
conf.dir = opt.path
}

func (opt *ParallelismOption) configureDestroy(conf *destroyConfig) {
conf.parallelism = opt.parallelism
}
Expand Down Expand Up @@ -122,5 +127,10 @@ func (tf *Terraform) destroyCmd(ctx context.Context, opts ...DestroyOption) *exe
}
}

// optional positional argument
if c.dir != "" {
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, args...)
}
3 changes: 2 additions & 1 deletion tfexec/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestDestroyCmd(t *testing.T) {
})

t.Run("override all defaults", func(t *testing.T) {
destroyCmd := tf.destroyCmd(context.Background(), Backup("testbackup"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), VarFile("testvarfile"), Lock(false), Parallelism(99), Refresh(false), Target("target1"), Target("target2"), Var("var1=foo"), Var("var2=bar"))
destroyCmd := tf.destroyCmd(context.Background(), Backup("testbackup"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), VarFile("testvarfile"), Lock(false), Parallelism(99), Refresh(false), Target("target1"), Target("target2"), Var("var1=foo"), Var("var2=bar"), Dir("destroydir"))

assertCmd(t, []string{
"destroy",
Expand All @@ -55,6 +55,7 @@ func TestDestroyCmd(t *testing.T) {
"-target=target2",
"-var", "var1=foo",
"-var", "var2=bar",
"destroydir",
}, nil, destroyCmd)
})
}
10 changes: 10 additions & 0 deletions tfexec/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type initConfig struct {
backend bool
backendConfig []string
dir string
forceCopy bool
fromModule string
get bool
Expand Down Expand Up @@ -45,6 +46,10 @@ func (opt *BackendConfigOption) configureInit(conf *initConfig) {
conf.backendConfig = append(conf.backendConfig, opt.path)
}

func (opt *DirOption) configureInit(conf *initConfig) {
conf.dir = opt.path
}

func (opt *FromModuleOption) configureInit(conf *initConfig) {
conf.fromModule = opt.source
}
Expand Down Expand Up @@ -127,5 +132,10 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) *exec.Cmd
}
}

// optional positional argument
if c.dir != "" {
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, args...)
}
3 changes: 2 additions & 1 deletion tfexec/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestInitCmd(t *testing.T) {
})

t.Run("override all defaults", func(t *testing.T) {
initCmd := tf.initCmd(context.Background(), Backend(false), BackendConfig("confpath1"), BackendConfig("confpath2"), FromModule("testsource"), Get(false), GetPlugins(false), Lock(false), LockTimeout("999s"), PluginDir("testdir1"), PluginDir("testdir2"), Reconfigure(true), Upgrade(true), VerifyPlugins(false))
initCmd := tf.initCmd(context.Background(), Backend(false), BackendConfig("confpath1"), BackendConfig("confpath2"), FromModule("testsource"), Get(false), GetPlugins(false), Lock(false), LockTimeout("999s"), PluginDir("testdir1"), PluginDir("testdir2"), Reconfigure(true), Upgrade(true), VerifyPlugins(false), Dir("initdir"))

assertCmd(t, []string{
"init",
Expand All @@ -63,6 +63,7 @@ func TestInitCmd(t *testing.T) {
"-backend-config=confpath2",
"-plugin-dir=testdir1",
"-plugin-dir=testdir2",
"initdir",
}, nil, initCmd)
})
}
8 changes: 8 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func Config(path string) *ConfigOption {
return &ConfigOption{path}
}

type DirOption struct {
path string
}

func Dir(path string) *DirOption {
return &DirOption{path}
}

type DirOrPlanOption struct {
path string
}
Expand Down
10 changes: 10 additions & 0 deletions tfexec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type planConfig struct {
destroy bool
dir string
lock bool
lockTimeout string
out string
Expand All @@ -32,6 +33,10 @@ type PlanOption interface {
configurePlan(*planConfig)
}

func (opt *DirOption) configurePlan(conf *planConfig) {
conf.dir = opt.path
}

func (opt *VarFileOption) configurePlan(conf *planConfig) {
conf.varFile = opt.path
}
Expand Down Expand Up @@ -121,5 +126,10 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) *exec.Cmd
}
}

// optional positional argument
if c.dir != "" {
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, args...)
}
3 changes: 2 additions & 1 deletion tfexec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPlanCmd(t *testing.T) {
})

t.Run("override all defaults", func(t *testing.T) {
planCmd := tf.planCmd(context.Background(), Destroy(true), Lock(false), LockTimeout("22s"), Out("whale"), Parallelism(42), Refresh(false), State("marvin"), Target("zaphod"), Target("beeblebrox"), Var("android=paranoid"), Var("brain_size=planet"), VarFile("trillian"))
planCmd := tf.planCmd(context.Background(), Destroy(true), Lock(false), LockTimeout("22s"), Out("whale"), Parallelism(42), Refresh(false), State("marvin"), Target("zaphod"), Target("beeblebrox"), Var("android=paranoid"), Var("brain_size=planet"), VarFile("trillian"), Dir("earth"))

assertCmd(t, []string{
"plan",
Expand All @@ -53,6 +53,7 @@ func TestPlanCmd(t *testing.T) {
"-target=beeblebrox",
"-var", "android=paranoid",
"-var", "brain_size=planet",
"earth",
}, nil, planCmd)
})
}

0 comments on commit cf7aa80

Please sign in to comment.