From 5834333ea322bbd55153638c0703b8b36d976bcc Mon Sep 17 00:00:00 2001 From: Gavin Williams Date: Mon, 12 Jun 2017 14:14:40 -0700 Subject: [PATCH] command: terraform get -upgrade As of this commit this just upgrades modules, but this option will also later upgrade plugins and indeed anything else that's being downloaded and installed as part of the init. --- command/init.go | 24 ++++++++++++++++++------ command/init_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/command/init.go b/command/init.go index 924f4565b0..2cce24e067 100644 --- a/command/init.go +++ b/command/init.go @@ -31,7 +31,7 @@ type InitCommand struct { } func (c *InitCommand) Run(args []string) int { - var flagBackend, flagGet, flagGetPlugins bool + var flagBackend, flagGet, flagGetPlugins, flagUpgrade bool var flagConfigExtra map[string]interface{} args = c.Meta.process(args, false) @@ -44,6 +44,7 @@ func (c *InitCommand) Run(args []string) int { cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state") cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout") cmdFlags.BoolVar(&c.reconfigure, "reconfigure", false, "reconfigure") + cmdFlags.BoolVar(&flagUpgrade, "upgrade", false, "") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } if err := cmdFlags.Parse(args); err != nil { @@ -112,10 +113,17 @@ func (c *InitCommand) Run(args []string) int { if flagGet && len(conf.Modules) > 0 { header = true - c.Ui.Output(c.Colorize().Color(fmt.Sprintf( - "[reset][bold]" + - "Downloading modules (if any)..."))) - if err := getModules(&c.Meta, path, module.GetModeGet); err != nil { + getMode := module.GetModeGet + if flagUpgrade { + getMode = module.GetModeUpdate + c.Ui.Output(c.Colorize().Color(fmt.Sprintf( + "[reset][bold]Upgrading modules..."))) + } else { + c.Ui.Output(c.Colorize().Color(fmt.Sprintf( + "[reset][bold]Downloading modules..."))) + } + + if err := getModules(&c.Meta, path, getMode); err != nil { c.Ui.Error(fmt.Sprintf( "Error downloading modules: %s", err)) return 1 @@ -324,7 +332,11 @@ Options: -no-color If specified, output won't contain any color. - -reconfigure Reconfigure the backend, ignoring any saved configuration. + -reconfigure Reconfigure the backend, ignoring any saved configuration. + + -upgrade=false If installing modules (-get) or plugins (-get-plugins), + ignore previously-downloaded objects and install the + latest version allowed within configured constraints. ` return strings.TrimSpace(helpText) } diff --git a/command/init_test.go b/command/init_test.go index 816a06fd45..de4f3c1db5 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -80,6 +80,39 @@ func TestInit_get(t *testing.T) { } } +func TestInit_getUpgradeModules(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + os.MkdirAll(td, 0755) + // copy.CopyDir(testFixturePath("init-get"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + + args := []string{ + "-get=true", + "-get-plugins=false", + "-upgrade", + testFixturePath("init-get"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("command did not complete successfully:\n%s", ui.ErrorWriter.String()) + } + + // Check output + output := ui.OutputWriter.String() + if !strings.Contains(output, "(update)") { + t.Fatalf("doesn't look like get upgrade: %s", output) + } +} + func TestInit_backend(t *testing.T) { // Create a temporary working directory that is empty td := tempDir(t)