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:"
)
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

15
main.go
View File

@ -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()
@ -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)

View File

@ -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
}
@ -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
@ -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