github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/command/get.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/tfdiags"
     8  )
     9  
    10  // GetCommand is a Command implementation that takes a Terraform
    11  // configuration and downloads all the modules.
    12  type GetCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *GetCommand) Run(args []string) int {
    17  	var update bool
    18  
    19  	args, err := c.Meta.process(args, false)
    20  	if err != nil {
    21  		return 1
    22  	}
    23  
    24  	cmdFlags := c.Meta.defaultFlagSet("get")
    25  	cmdFlags.BoolVar(&update, "update", false, "update")
    26  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    27  	if err := cmdFlags.Parse(args); err != nil {
    28  		c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
    29  		return 1
    30  	}
    31  
    32  	path, err := ModulePath(cmdFlags.Args())
    33  	if err != nil {
    34  		c.Ui.Error(err.Error())
    35  		return 1
    36  	}
    37  
    38  	path = c.normalizePath(path)
    39  
    40  	diags := getModules(&c.Meta, path, update)
    41  	c.showDiagnostics(diags)
    42  	if diags.HasErrors() {
    43  		return 1
    44  	}
    45  
    46  	return 0
    47  }
    48  
    49  func (c *GetCommand) Help() string {
    50  	helpText := `
    51  Usage: terraform get [options] PATH
    52  
    53    Downloads and installs modules needed for the configuration given by
    54    PATH.
    55  
    56    This recursively downloads all modules needed, such as modules
    57    imported by modules imported by the root and so on. If a module is
    58    already downloaded, it will not be redownloaded or checked for updates
    59    unless the -update flag is specified.
    60  
    61  Options:
    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  
    72  func (c *GetCommand) Synopsis() string {
    73  	return "Download and install modules for the configuration"
    74  }
    75  
    76  func 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  }