github.com/dtx/terraform@v0.7.4-0.20160907225157-926acfd0821e/command/get.go (about) 1 package command 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/hashicorp/terraform/config/module" 10 ) 11 12 // GetCommand is a Command implementation that takes a Terraform 13 // configuration and downloads all the modules. 14 type GetCommand struct { 15 Meta 16 } 17 18 func (c *GetCommand) Run(args []string) int { 19 var update bool 20 21 args = c.Meta.process(args, false) 22 23 cmdFlags := flag.NewFlagSet("get", flag.ContinueOnError) 24 cmdFlags.BoolVar(&update, "update", false, "update") 25 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 26 if err := cmdFlags.Parse(args); err != nil { 27 return 1 28 } 29 30 var path string 31 args = cmdFlags.Args() 32 if len(args) > 1 { 33 c.Ui.Error("The get command expects one argument.\n") 34 cmdFlags.Usage() 35 return 1 36 } else if len(args) == 1 { 37 path = args[0] 38 } else { 39 var err error 40 path, err = os.Getwd() 41 if err != nil { 42 c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) 43 } 44 } 45 46 mode := module.GetModeGet 47 if update { 48 mode = module.GetModeUpdate 49 } 50 51 _, _, err := c.Context(contextOpts{ 52 Path: path, 53 GetMode: mode, 54 }) 55 if err != nil { 56 c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) 57 return 1 58 } 59 60 return 0 61 } 62 63 func (c *GetCommand) Help() string { 64 helpText := ` 65 Usage: terraform get [options] PATH 66 67 Downloads and installs modules needed for the configuration given by 68 PATH. 69 70 This recursively downloads all modules needed, such as modules 71 imported by modules imported by the root and so on. If a module is 72 already downloaded, it will not be redownloaded or checked for updates 73 unless the -update flag is specified. 74 75 Options: 76 77 -update=false If true, modules already downloaded will be checked 78 for updates and updated if necessary. 79 80 -no-color If specified, output won't contain any color. 81 82 ` 83 return strings.TrimSpace(helpText) 84 } 85 86 func (c *GetCommand) Synopsis() string { 87 return "Download and install modules for the configuration" 88 }