github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/command/get.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/config/module"
     9  )
    10  
    11  // GetCommand is a Command implementation that takes a Terraform
    12  // configuration and downloads all the modules.
    13  type GetCommand struct {
    14  	Meta
    15  }
    16  
    17  func (c *GetCommand) Run(args []string) int {
    18  	var update bool
    19  
    20  	args = c.Meta.process(args, false)
    21  
    22  	cmdFlags := flag.NewFlagSet("get", flag.ContinueOnError)
    23  	cmdFlags.BoolVar(&update, "update", false, "update")
    24  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    25  	if err := cmdFlags.Parse(args); err != nil {
    26  		return 1
    27  	}
    28  
    29  	var path string
    30  	path, err := ModulePath(cmdFlags.Args())
    31  	if err != nil {
    32  		c.Ui.Error(err.Error())
    33  		return 1
    34  	}
    35  
    36  	mode := module.GetModeGet
    37  	if update {
    38  		mode = module.GetModeUpdate
    39  	}
    40  
    41  	if err := getModules(&c.Meta, path, mode); err != nil {
    42  		c.Ui.Error(err.Error())
    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=false       If true, modules already downloaded will be checked
    64                        for updates and updated if necessary.
    65  
    66    -no-color           If specified, output won't contain any color.
    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, mode module.GetMode) error {
    77  	mod, err := module.NewTreeModule("", path)
    78  	if err != nil {
    79  		return fmt.Errorf("Error loading configuration: %s", err)
    80  	}
    81  
    82  	err = mod.Load(m.moduleStorage(m.DataDir()), mode)
    83  	if err != nil {
    84  		return fmt.Errorf("Error loading modules: %s", err)
    85  	}
    86  
    87  	return nil
    88  }