From 011c9556cc8156daea0b2eacfef14058421c79c0 Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Sun, 16 Aug 2020 16:02:56 +0100 Subject: [PATCH] implement DIR positional arg --- tfexec/destroy.go | 10 ++++++++++ tfexec/destroy_test.go | 3 ++- tfexec/init.go | 10 ++++++++++ tfexec/init_test.go | 3 ++- tfexec/options.go | 8 ++++++++ tfexec/plan.go | 10 ++++++++++ tfexec/plan_test.go | 3 ++- 7 files changed, 44 insertions(+), 3 deletions(-) diff --git a/tfexec/destroy.go b/tfexec/destroy.go index 3b9214b5..10895118 100644 --- a/tfexec/destroy.go +++ b/tfexec/destroy.go @@ -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' @@ -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 } @@ -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...) } diff --git a/tfexec/destroy_test.go b/tfexec/destroy_test.go index a9b4f02e..9fa24fc8 100644 --- a/tfexec/destroy_test.go +++ b/tfexec/destroy_test.go @@ -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", @@ -55,6 +55,7 @@ func TestDestroyCmd(t *testing.T) { "-target=target2", "-var", "var1=foo", "-var", "var2=bar", + "destroydir", }, nil, destroyCmd) }) } diff --git a/tfexec/init.go b/tfexec/init.go index 69bc6410..a60f4363 100644 --- a/tfexec/init.go +++ b/tfexec/init.go @@ -9,6 +9,7 @@ import ( type initConfig struct { backend bool backendConfig []string + dir string forceCopy bool fromModule string get bool @@ -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 } @@ -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...) } diff --git a/tfexec/init_test.go b/tfexec/init_test.go index 5325dcee..3eb0c1bf 100644 --- a/tfexec/init_test.go +++ b/tfexec/init_test.go @@ -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", @@ -63,6 +63,7 @@ func TestInitCmd(t *testing.T) { "-backend-config=confpath2", "-plugin-dir=testdir1", "-plugin-dir=testdir2", + "initdir", }, nil, initCmd) }) } diff --git a/tfexec/options.go b/tfexec/options.go index 035268ea..61941840 100644 --- a/tfexec/options.go +++ b/tfexec/options.go @@ -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 } diff --git a/tfexec/plan.go b/tfexec/plan.go index 29b04ee3..2aeb27c5 100644 --- a/tfexec/plan.go +++ b/tfexec/plan.go @@ -9,6 +9,7 @@ import ( type planConfig struct { destroy bool + dir string lock bool lockTimeout string out string @@ -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 } @@ -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...) } diff --git a/tfexec/plan_test.go b/tfexec/plan_test.go index a6a9f44b..127f9ea6 100644 --- a/tfexec/plan_test.go +++ b/tfexec/plan_test.go @@ -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", @@ -53,6 +53,7 @@ func TestPlanCmd(t *testing.T) { "-target=beeblebrox", "-var", "android=paranoid", "-var", "brain_size=planet", + "earth", }, nil, planCmd) }) }