1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/hashicorp/terraform/internal/tfdiags"
8)
9
10// GetCommand is a Command implementation that takes a Terraform
11// configuration and downloads all the modules.
12type GetCommand struct {
13	Meta
14}
15
16func (c *GetCommand) Run(args []string) int {
17	var update bool
18
19	args = c.Meta.process(args)
20	cmdFlags := c.Meta.defaultFlagSet("get")
21	cmdFlags.BoolVar(&update, "update", false, "update")
22	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
23	if err := cmdFlags.Parse(args); err != nil {
24		c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
25		return 1
26	}
27
28	path, err := ModulePath(cmdFlags.Args())
29	if err != nil {
30		c.Ui.Error(err.Error())
31		return 1
32	}
33
34	path = c.normalizePath(path)
35
36	diags := getModules(&c.Meta, path, update)
37	c.showDiagnostics(diags)
38	if diags.HasErrors() {
39		return 1
40	}
41
42	return 0
43}
44
45func (c *GetCommand) Help() string {
46	helpText := `
47Usage: terraform [global options] get [options] PATH
48
49  Downloads and installs modules needed for the configuration given by
50  PATH.
51
52  This recursively downloads all modules needed, such as modules
53  imported by modules imported by the root and so on. If a module is
54  already downloaded, it will not be redownloaded or checked for updates
55  unless the -update flag is specified.
56
57  Module installation also happens automatically by default as part of
58  the "terraform init" command, so you should rarely need to run this
59  command separately.
60
61Options:
62
63  -update             Check already-downloaded modules for available updates
64                      and install the newest versions available.
65
66  -no-color           Disable text coloring in the output.
67
68`
69	return strings.TrimSpace(helpText)
70}
71
72func (c *GetCommand) Synopsis() string {
73	return "Install or upgrade remote Terraform modules"
74}
75
76func getModules(m *Meta, path string, upgrade bool) tfdiags.Diagnostics {
77	hooks := uiModuleInstallHooks{
78		Ui:             m.Ui,
79		ShowLocalPaths: true,
80	}
81	return m.installModules(path, upgrade, hooks)
82}
83