Skip to content

Commit

Permalink
main: make configuration available when initializing commands
Browse files Browse the repository at this point in the history
This, in principle, allows us to make use of configuration information
when we populate the Meta structure, though we won't actually make use
of that until a subsequent commit.
  • Loading branch information
apparentlymart committed Sep 29, 2017
1 parent 60d8b6c commit 3f401f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
10 changes: 1 addition & 9 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ const (
OutputPrefix = "o:"
)

func init() {
Ui = &cli.PrefixedUi{
AskPrefix: OutputPrefix,
OutputPrefix: OutputPrefix,
InfoPrefix: OutputPrefix,
ErrorPrefix: ErrorPrefix,
Ui: &cli.BasicUi{Writer: os.Stdout},
}

func initCommands(config *Config) {
var inAutomation bool
if v := os.Getenv(runningInAutomationEnvName); v != "" {
inAutomation = true
Expand Down
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func realMain() int {
return wrappedMain()
}

func init() {
Ui = &cli.PrefixedUi{
AskPrefix: OutputPrefix,
OutputPrefix: OutputPrefix,
InfoPrefix: OutputPrefix,
ErrorPrefix: ErrorPrefix,
Ui: &cli.BasicUi{Writer: os.Stdout},
}
}

func wrappedMain() int {
// We always need to close the DebugInfo before we exit.
defer terraform.CloseDebugInfo()
Expand Down Expand Up @@ -128,6 +138,11 @@ func wrappedMain() int {
config = *config.Merge(usrcfg)
}

// In tests, Commands may already be set to provide mock commands
if Commands == nil {
initCommands(&config)
}

// Run checkpoint
go runCheckpoint(&config)

Expand Down
13 changes: 11 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
defer func() { os.Args = oldArgs }()

// Setup test command and restore that
Commands = make(map[string]cli.CommandFactory)
defer func() {
Commands = nil
}()
testCommandName := "unit-test-cli-args"
testCommand := &testCommandCLI{}
defer func() { delete(Commands, testCommandName) }()
Commands[testCommandName] = func() (cli.Command, error) {
return testCommand, nil
}
Expand Down Expand Up @@ -150,6 +153,12 @@ func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

// Setup test command and restore that
Commands = make(map[string]cli.CommandFactory)
defer func() {
Commands = nil
}()

cases := []struct {
Name string
Command string
Expand Down Expand Up @@ -230,7 +239,7 @@ func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
testCommand.Args = nil
exit := wrappedMain()
if (exit != 0) != tc.Err {
t.Fatalf("bad: %d", exit)
t.Fatalf("unexpected exit status %d; want 0", exit)
}
if tc.Err {
return
Expand Down

0 comments on commit 3f401f0

Please sign in to comment.