github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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, err := c.Meta.process(args, false)
    21  	if err != nil {
    22  		return 1
    23  	}
    24  
    25  	cmdFlags := flag.NewFlagSet("get", flag.ContinueOnError)
    26  	cmdFlags.BoolVar(&update, "update", false, "update")
    27  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    28  	if err := cmdFlags.Parse(args); err != nil {
    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  	mode := module.GetModeGet
    39  	if update {
    40  		mode = module.GetModeUpdate
    41  	}
    42  
    43  	if err := getModules(&c.Meta, path, mode); err != nil {
    44  		c.Ui.Error(err.Error())
    45  		return 1
    46  	}
    47  
    48  	return 0
    49  }
    50  
    51  func (c *GetCommand) Help() string {
    52  	helpText := `
    53  Usage: terraform get [options] PATH
    54  
    55    Downloads and installs modules needed for the configuration given by
    56    PATH.
    57  
    58    This recursively downloads all modules needed, such as modules
    59    imported by modules imported by the root and so on. If a module is
    60    already downloaded, it will not be redownloaded or checked for updates
    61    unless the -update flag is specified.
    62  
    63  Options:
    64  
    65    -update=false       If true, modules already downloaded will be checked
    66                        for updates and updated if necessary.
    67  
    68    -no-color           If specified, output won't contain any color.
    69  
    70  `
    71  	return strings.TrimSpace(helpText)
    72  }
    73  
    74  func (c *GetCommand) Synopsis() string {
    75  	return "Download and install modules for the configuration"
    76  }
    77  
    78  func getModules(m *Meta, path string, mode module.GetMode) error {
    79  	mod, err := module.NewTreeModule("", path)
    80  	if err != nil {
    81  		return fmt.Errorf("Error loading configuration: %s", err)
    82  	}
    83  
    84  	err = mod.Load(m.moduleStorage(m.DataDir(), mode))
    85  	if err != nil {
    86  		return fmt.Errorf("Error loading modules: %s", err)
    87  	}
    88  
    89  	return nil
    90  }