main: make configuration available when initializing commands

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.
This commit is contained in:
Martin Atkins 2017-09-01 15:58:38 -07:00
parent 60d8b6c4d7
commit 3f401f0cd4
3 changed files with 27 additions and 11 deletions

View File

@ -25,15 +25,7 @@ const (
OutputPrefix = "o:" OutputPrefix = "o:"
) )
func init() { func initCommands(config *Config) {
Ui = &cli.PrefixedUi{
AskPrefix: OutputPrefix,
OutputPrefix: OutputPrefix,
InfoPrefix: OutputPrefix,
ErrorPrefix: ErrorPrefix,
Ui: &cli.BasicUi{Writer: os.Stdout},
}
var inAutomation bool var inAutomation bool
if v := os.Getenv(runningInAutomationEnvName); v != "" { if v := os.Getenv(runningInAutomationEnvName); v != "" {
inAutomation = true inAutomation = true

15
main.go
View File

@ -96,6 +96,16 @@ func realMain() int {
return wrappedMain() return wrappedMain()
} }
func init() {
Ui = &cli.PrefixedUi{
AskPrefix: OutputPrefix,
OutputPrefix: OutputPrefix,
InfoPrefix: OutputPrefix,
ErrorPrefix: ErrorPrefix,
Ui: &cli.BasicUi{Writer: os.Stdout},
}
}
func wrappedMain() int { func wrappedMain() int {
// We always need to close the DebugInfo before we exit. // We always need to close the DebugInfo before we exit.
defer terraform.CloseDebugInfo() defer terraform.CloseDebugInfo()
@ -128,6 +138,11 @@ func wrappedMain() int {
config = *config.Merge(usrcfg) config = *config.Merge(usrcfg)
} }
// In tests, Commands may already be set to provide mock commands
if Commands == nil {
initCommands(&config)
}
// Run checkpoint // Run checkpoint
go runCheckpoint(&config) go runCheckpoint(&config)

View File

@ -18,9 +18,12 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
defer func() { os.Args = oldArgs }() defer func() { os.Args = oldArgs }()
// Setup test command and restore that // Setup test command and restore that
Commands = make(map[string]cli.CommandFactory)
defer func() {
Commands = nil
}()
testCommandName := "unit-test-cli-args" testCommandName := "unit-test-cli-args"
testCommand := &testCommandCLI{} testCommand := &testCommandCLI{}
defer func() { delete(Commands, testCommandName) }()
Commands[testCommandName] = func() (cli.Command, error) { Commands[testCommandName] = func() (cli.Command, error) {
return testCommand, nil return testCommand, nil
} }
@ -150,6 +153,12 @@ func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
oldArgs := os.Args oldArgs := os.Args
defer func() { os.Args = oldArgs }() defer func() { os.Args = oldArgs }()
// Setup test command and restore that
Commands = make(map[string]cli.CommandFactory)
defer func() {
Commands = nil
}()
cases := []struct { cases := []struct {
Name string Name string
Command string Command string
@ -230,7 +239,7 @@ func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
testCommand.Args = nil testCommand.Args = nil
exit := wrappedMain() exit := wrappedMain()
if (exit != 0) != tc.Err { if (exit != 0) != tc.Err {
t.Fatalf("bad: %d", exit) t.Fatalf("unexpected exit status %d; want 0", exit)
} }
if tc.Err { if tc.Err {
return return