github.com/lamielle/terraform@v0.3.2-0.20141121070651-81f008ba53d5/command/init.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/terraform/config"
    10  	"github.com/hashicorp/terraform/config/module"
    11  )
    12  
    13  // InitCommand is a Command implementation that takes a Terraform
    14  // module and clones it to the working directory.
    15  type InitCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *InitCommand) Run(args []string) int {
    20  	args = c.Meta.process(args, false)
    21  
    22  	cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError)
    23  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    24  	if err := cmdFlags.Parse(args); err != nil {
    25  		return 1
    26  	}
    27  
    28  	var path string
    29  	args = cmdFlags.Args()
    30  	if len(args) > 2 {
    31  		c.Ui.Error("The init command expects at most two arguments.\n")
    32  		cmdFlags.Usage()
    33  		return 1
    34  	} else if len(args) < 1 {
    35  		c.Ui.Error("The init command expects at least one arguments.\n")
    36  		cmdFlags.Usage()
    37  		return 1
    38  	}
    39  
    40  	if len(args) == 2 {
    41  		path = args[1]
    42  	} else {
    43  		var err error
    44  		path, err = os.Getwd()
    45  		if err != nil {
    46  			c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
    47  		}
    48  	}
    49  
    50  	source := args[0]
    51  
    52  	// Get our pwd since we need it
    53  	pwd, err := os.Getwd()
    54  	if err != nil {
    55  		c.Ui.Error(fmt.Sprintf(
    56  			"Error reading working directory: %s", err))
    57  		return 1
    58  	}
    59  
    60  	// Verify the directory is empty
    61  	if empty, err := config.IsEmptyDir(path); err != nil {
    62  		c.Ui.Error(fmt.Sprintf(
    63  			"Error checking on destination path: %s", err))
    64  		return 1
    65  	} else if !empty {
    66  		c.Ui.Error(
    67  			"The destination path has Terraform configuration files. The\n" +
    68  				"init command can only be used on a directory without existing Terraform\n" +
    69  				"files.")
    70  		return 1
    71  	}
    72  
    73  	// Detect
    74  	source, err = module.Detect(source, pwd)
    75  	if err != nil {
    76  		c.Ui.Error(fmt.Sprintf(
    77  			"Error with module source: %s", err))
    78  		return 1
    79  	}
    80  
    81  	// Get it!
    82  	if err := module.GetCopy(path, source); err != nil {
    83  		c.Ui.Error(err.Error())
    84  		return 1
    85  	}
    86  
    87  	return 0
    88  }
    89  
    90  func (c *InitCommand) Help() string {
    91  	helpText := `
    92  Usage: terraform init [options] SOURCE [PATH]
    93  
    94    Downloads the module given by SOURCE into the PATH. The PATH defaults
    95    to the working directory. PATH must be empty of any Terraform files.
    96    Any conflicting non-Terraform files will be overwritten.
    97  
    98    The module downloaded is a copy. If you're downloading a module from
    99    Git, it will not preserve the Git history, it will only copy the
   100    latest files.
   101  
   102  `
   103  	return strings.TrimSpace(helpText)
   104  }
   105  
   106  func (c *InitCommand) Synopsis() string {
   107  	return "Initializes Terraform configuration from a module"
   108  }