mirror of https://github.com/hashicorp/terraform
commit
c8ccc6f72e
@ -0,0 +1,108 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/config"
|
||||
"github.com/hashicorp/terraform/config/module"
|
||||
)
|
||||
|
||||
// InitCommand is a Command implementation that takes a Terraform
|
||||
// module and clones it to the working directory.
|
||||
type InitCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (c *InitCommand) Run(args []string) int {
|
||||
args = c.Meta.process(args, false)
|
||||
|
||||
cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
var path string
|
||||
args = cmdFlags.Args()
|
||||
if len(args) > 2 {
|
||||
c.Ui.Error("The init command expects at most two arguments.\n")
|
||||
cmdFlags.Usage()
|
||||
return 1
|
||||
} else if len(args) < 1 {
|
||||
c.Ui.Error("The init command expects at least one arguments.\n")
|
||||
cmdFlags.Usage()
|
||||
return 1
|
||||
}
|
||||
|
||||
if len(args) == 2 {
|
||||
path = args[1]
|
||||
} else {
|
||||
var err error
|
||||
path, err = os.Getwd()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
source := args[0]
|
||||
|
||||
// Get our pwd since we need it
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error reading working directory: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// Verify the directory is empty
|
||||
if empty, err := config.IsEmptyDir(path); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error checking on destination path: %s", err))
|
||||
return 1
|
||||
} else if !empty {
|
||||
c.Ui.Error(
|
||||
"The destination path has Terraform configuration files. The\n" +
|
||||
"init command can only be used on a directory without existing Terraform\n" +
|
||||
"files.")
|
||||
return 1
|
||||
}
|
||||
|
||||
// Detect
|
||||
source, err = module.Detect(source, pwd)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error with module source: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// Get it!
|
||||
if err := module.GetCopy(path, source); err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *InitCommand) Help() string {
|
||||
helpText := `
|
||||
Usage: terraform init [options] SOURCE [PATH]
|
||||
|
||||
Downloads the module given by SOURCE into the PATH. The PATH defaults
|
||||
to the working directory. PATH must be empty of any Terraform files.
|
||||
Any conflicting non-Terraform files will be overwritten.
|
||||
|
||||
The module downloaded is a copy. If you're downloading a module from
|
||||
Git, it will not preserve the Git history, it will only copy the
|
||||
latest files.
|
||||
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (c *InitCommand) Synopsis() string {
|
||||
return "Initializes Terraform configuration from a module"
|
||||
}
|
||||
@ -0,0 +1,102 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
dir := tempDir(t)
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &InitCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
testFixturePath("init"),
|
||||
dir,
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_cwd(t *testing.T) {
|
||||
dir := tempDir(t)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Change to the temporary directory
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
defer os.Chdir(cwd)
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &InitCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
testFixturePath("init"),
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat("hello.tf"); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_multipleArgs(t *testing.T) {
|
||||
ui := new(cli.MockUi)
|
||||
c := &InitCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"bad",
|
||||
"bad",
|
||||
}
|
||||
if code := c.Run(args); code != 1 {
|
||||
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_noArgs(t *testing.T) {
|
||||
ui := new(cli.MockUi)
|
||||
c := &InitCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{}
|
||||
if code := c.Run(args); code != 1 {
|
||||
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
---
|
||||
layout: "docs"
|
||||
page_title: "Command: init"
|
||||
sidebar_current: "docs-commands-init"
|
||||
---
|
||||
|
||||
# Command: init
|
||||
|
||||
The `terraform init` command is used to initialize a Terraform configuration
|
||||
using another
|
||||
[module](/docs/modules/index.html)
|
||||
as a skeleton.
|
||||
|
||||
## Usage
|
||||
|
||||
Usage: `terraform init [options] SOURCE [DIR]`
|
||||
|
||||
Init will download the module from SOURCE and copy it into the DIR
|
||||
(which defaults to the current working directory). Version control
|
||||
information from the module (such as Git history) will not be copied.
|
||||
|
||||
The directory being initialized must be empty of all Terraform configurations.
|
||||
If the module has other files which conflict with what is already in the
|
||||
directory, they _will be overwritten_.
|
||||
Loading…
Reference in new issue