forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
command: Get command, not functional yet. Converted to use modules.
- Loading branch information
Showing
9 changed files
with
192 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package command | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// GetCommand is a Command implementation that takes a Terraform | ||
// configuration and downloads all the modules. | ||
type GetCommand struct { | ||
Meta | ||
} | ||
|
||
func (c *GetCommand) Run(args []string) int { | ||
args = c.Meta.process(args, false) | ||
|
||
cmdFlags := flag.NewFlagSet("get", 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) > 1 { | ||
c.Ui.Error("The graph command expects one argument.\n") | ||
cmdFlags.Usage() | ||
return 1 | ||
} else if len(args) == 1 { | ||
path = args[0] | ||
} else { | ||
var err error | ||
path, err = os.Getwd() | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) | ||
} | ||
} | ||
|
||
_, _, err := c.Context(contextOpts{ | ||
Path: path, | ||
}) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (c *GetCommand) Help() string { | ||
helpText := ` | ||
Usage: terraform get [options] PATH | ||
Downloads and installs modules needed for the configuration given by | ||
PATH. | ||
This recursively downloads all modules needed, such as modules | ||
imported by modules imported by the root and so on. If a module is | ||
already downloaded, it will not be redownloaded or checked for updates | ||
unless the -update flag is specified. | ||
Options: | ||
-update=false If true, modules already downloaded will be checked | ||
for updates and updated if necessary. | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *GetCommand) Synopsis() string { | ||
return "Download and install modules for the configuration" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package command | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/mitchellh/cli" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
ui := new(cli.MockUi) | ||
c := &GetCommand{ | ||
Meta: Meta{ | ||
ContextOpts: testCtxConfig(testProvider()), | ||
Ui: ui, | ||
}, | ||
} | ||
|
||
args := []string{ | ||
testFixturePath("graph"), | ||
} | ||
if code := c.Run(args); code != 0 { | ||
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) | ||
} | ||
} | ||
|
||
func TestGet_multipleArgs(t *testing.T) { | ||
ui := new(cli.MockUi) | ||
c := &GetCommand{ | ||
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 TestGet_noArgs(t *testing.T) { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
if err := os.Chdir(testFixturePath("graph")); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
defer os.Chdir(cwd) | ||
|
||
ui := new(cli.MockUi) | ||
c := &GraphCommand{ | ||
Meta: Meta{ | ||
ContextOpts: testCtxConfig(testProvider()), | ||
Ui: ui, | ||
}, | ||
} | ||
|
||
args := []string{} | ||
if code := c.Run(args); code != 0 { | ||
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters