2014-09-22 12:56:50 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-08-16 07:31:21 -05:00
|
|
|
"fmt"
|
2014-09-22 12:56:50 -05:00
|
|
|
"strings"
|
2014-09-22 13:15:27 -05:00
|
|
|
|
2019-01-17 17:56:53 -06:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2014-09-22 12:56:50 -05: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 13:18:49 -05:00
|
|
|
var update bool
|
|
|
|
|
2020-04-01 14:01:08 -05:00
|
|
|
args = c.Meta.process(args)
|
2018-11-21 08:35:27 -06:00
|
|
|
cmdFlags := c.Meta.defaultFlagSet("get")
|
2014-09-22 13:18:49 -05:00
|
|
|
cmdFlags.BoolVar(&update, "update", false, "update")
|
2014-09-22 12:56:50 -05:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
2019-08-16 07:31:21 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
|
2014-09-22 12:56:50 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
path, err := ModulePath(cmdFlags.Args())
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2014-09-22 12:56:50 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-01-17 17:56:53 -06:00
|
|
|
path = c.normalizePath(path)
|
2014-09-22 13:18:49 -05:00
|
|
|
|
2019-01-17 17:56:53 -06:00
|
|
|
diags := getModules(&c.Meta, path, update)
|
|
|
|
c.showDiagnostics(diags)
|
|
|
|
if diags.HasErrors() {
|
2014-09-22 12:56:50 -05: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.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2019-01-17 17:56:53 -06:00
|
|
|
-update Check already-downloaded modules for available updates
|
|
|
|
and install the newest versions available.
|
2014-09-22 12:56:50 -05:00
|
|
|
|
2019-01-17 17:56:53 -06:00
|
|
|
-no-color Disable text coloring in the output.
|
2015-06-22 07:14:01 -05:00
|
|
|
|
2014-09-22 12:56:50 -05:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GetCommand) Synopsis() string {
|
|
|
|
return "Download and install modules for the configuration"
|
|
|
|
}
|
2017-01-18 22:50:45 -06:00
|
|
|
|
2019-01-17 17:56:53 -06:00
|
|
|
func getModules(m *Meta, path string, upgrade bool) tfdiags.Diagnostics {
|
|
|
|
hooks := uiModuleInstallHooks{
|
|
|
|
Ui: m.Ui,
|
|
|
|
ShowLocalPaths: true,
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|
2019-01-17 17:56:53 -06:00
|
|
|
return m.installModules(path, upgrade, hooks)
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|