2014-09-22 19:56:50 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-08-16 14:31:21 +02:00
|
|
|
"fmt"
|
2014-09-22 19:56:50 +02:00
|
|
|
"strings"
|
2014-09-22 20:15:27 +02:00
|
|
|
|
2019-01-18 00:56:53 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2014-09-22 19:56:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetCommand is a Command implementation that takes a Terraform
|
|
|
|
// configuration and downloads all the modules.
|
|
|
|
type GetCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GetCommand) Run(args []string) int {
|
2014-09-22 20:18:49 +02:00
|
|
|
var update bool
|
|
|
|
|
2020-04-01 21:01:08 +02:00
|
|
|
args = c.Meta.process(args)
|
2018-11-21 15:35:27 +01:00
|
|
|
cmdFlags := c.Meta.defaultFlagSet("get")
|
2014-09-22 20:18:49 +02:00
|
|
|
cmdFlags.BoolVar(&update, "update", false, "update")
|
2014-09-22 19:56:50 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
2019-08-16 14:31:21 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
|
2014-09-22 19:56:50 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
path, err := ModulePath(cmdFlags.Args())
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2014-09-22 19:56:50 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:56:53 +01:00
|
|
|
path = c.normalizePath(path)
|
2014-09-22 20:18:49 +02:00
|
|
|
|
2019-01-18 00:56:53 +01:00
|
|
|
diags := getModules(&c.Meta, path, update)
|
|
|
|
c.showDiagnostics(diags)
|
|
|
|
if diags.HasErrors() {
|
2014-09-22 19:56:50 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GetCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: terraform get [options] PATH
|
|
|
|
|
|
|
|
Downloads and installs modules needed for the configuration given by
|
|
|
|
PATH.
|
|
|
|
|
|
|
|
This recursively downloads all modules needed, such as modules
|
|
|
|
imported by modules imported by the root and so on. If a module is
|
|
|
|
already downloaded, it will not be redownloaded or checked for updates
|
|
|
|
unless the -update flag is specified.
|
|
|
|
|
2020-10-24 01:55:32 +02:00
|
|
|
Module installation also happens automatically by default as part of
|
|
|
|
the "terraform init" command, so you should rarely need to run this
|
|
|
|
command separately.
|
|
|
|
|
2014-09-22 19:56:50 +02:00
|
|
|
Options:
|
|
|
|
|
2019-01-18 00:56:53 +01:00
|
|
|
-update Check already-downloaded modules for available updates
|
|
|
|
and install the newest versions available.
|
2014-09-22 19:56:50 +02:00
|
|
|
|
2019-01-18 00:56:53 +01:00
|
|
|
-no-color Disable text coloring in the output.
|
2015-06-22 14:14:01 +02:00
|
|
|
|
2014-09-22 19:56:50 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GetCommand) Synopsis() string {
|
2020-10-24 01:55:32 +02:00
|
|
|
return "Install or upgrade remote Terraform modules"
|
2014-09-22 19:56:50 +02:00
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2019-01-18 00:56:53 +01:00
|
|
|
func getModules(m *Meta, path string, upgrade bool) tfdiags.Diagnostics {
|
|
|
|
hooks := uiModuleInstallHooks{
|
|
|
|
Ui: m.Ui,
|
|
|
|
ShowLocalPaths: true,
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
2019-01-18 00:56:53 +01:00
|
|
|
return m.installModules(path, upgrade, hooks)
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|