From 1ee2b014a6983bd381cf8de9d97ce9d1de9e36b6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 27 May 2015 20:09:52 -0700 Subject: [PATCH] packer: remove Ui/Cache from CoreConfig --- command/build.go | 2 +- command/meta.go | 1 + commands.go | 27 +++++++++------------------ config.go | 3 --- main.go | 29 ++++++++++++++++++++++------- packer/core.go | 15 --------------- packer/testing.go | 3 --- 7 files changed, 33 insertions(+), 47 deletions(-) diff --git a/command/build.go b/command/build.go index ec6f70555..fbadd6dab 100644 --- a/command/build.go +++ b/command/build.go @@ -149,7 +149,7 @@ func (c BuildCommand) Run(args []string) int { name := b.Name() log.Printf("Starting build run: %s", name) ui := buildUis[name] - runArtifacts, err := b.Run(ui, c.CoreConfig.Cache) + runArtifacts, err := b.Run(ui, c.Cache) if err != nil { ui.Error(fmt.Sprintf("Build '%s' errored: %s", name, err)) diff --git a/command/meta.go b/command/meta.go index 0dc721bd1..250f94f6b 100644 --- a/command/meta.go +++ b/command/meta.go @@ -26,6 +26,7 @@ const ( // Packer command inherits. type Meta struct { CoreConfig *packer.CoreConfig + Cache packer.Cache Ui packer.Ui // These are set by command-line flags diff --git a/commands.go b/commands.go index e0f313957..510250721 100644 --- a/commands.go +++ b/commands.go @@ -6,62 +6,53 @@ import ( "github.com/mitchellh/cli" "github.com/mitchellh/packer/command" - "github.com/mitchellh/packer/packer" ) // Commands is the mapping of all the available Terraform commands. var Commands map[string]cli.CommandFactory -// Ui is the cli.Ui used for communicating to the outside world. -var Ui cli.Ui +// CommandMeta is the Meta to use for the commands. This must be written +// before the CLI is started. +var CommandMeta *command.Meta const ErrorPrefix = "e:" const OutputPrefix = "o:" func init() { - meta := command.Meta{ - CoreConfig: &CoreConfig, - Ui: &packer.BasicUi{ - Reader: os.Stdin, - Writer: os.Stdout, - ErrorWriter: os.Stdout, - }, - } - Commands = map[string]cli.CommandFactory{ "build": func() (cli.Command, error) { return &command.BuildCommand{ - Meta: meta, + Meta: *CommandMeta, }, nil }, "fix": func() (cli.Command, error) { return &command.FixCommand{ - Meta: meta, + Meta: *CommandMeta, }, nil }, "inspect": func() (cli.Command, error) { return &command.InspectCommand{ - Meta: meta, + Meta: *CommandMeta, }, nil }, "push": func() (cli.Command, error) { return &command.PushCommand{ - Meta: meta, + Meta: *CommandMeta, }, nil }, "validate": func() (cli.Command, error) { return &command.ValidateCommand{ - Meta: meta, + Meta: *CommandMeta, }, nil }, "version": func() (cli.Command, error) { return &command.VersionCommand{ - Meta: meta, + Meta: *CommandMeta, Revision: GitCommit, Version: Version, VersionPrerelease: VersionPrerelease, diff --git a/config.go b/config.go index a9c07043f..745922b1e 100644 --- a/config.go +++ b/config.go @@ -13,9 +13,6 @@ import ( "github.com/mitchellh/packer/packer/plugin" ) -// CoreConfig is the global CoreConfig we use to initialize the CLI. -var CoreConfig packer.CoreConfig - type config struct { DisableCheckpoint bool `json:"disable_checkpoint"` DisableCheckpointSignature bool `json:"disable_checkpoint_signature"` diff --git a/main.go b/main.go index 73d4b88cf..d00336283 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "sync" "github.com/mitchellh/cli" + "github.com/mitchellh/packer/command" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer/plugin" "github.com/mitchellh/panicwrap" @@ -139,14 +140,14 @@ func wrappedMain() int { defer plugin.CleanupClients() - // Create the environment configuration - CoreConfig.Cache = cache - CoreConfig.Components.Builder = config.LoadBuilder - CoreConfig.Components.Hook = config.LoadHook - CoreConfig.Components.PostProcessor = config.LoadPostProcessor - CoreConfig.Components.Provisioner = config.LoadProvisioner + // Setup the UI if we're being machine-readable + var ui packer.Ui = &packer.BasicUi{ + Reader: os.Stdin, + Writer: os.Stdout, + ErrorWriter: os.Stdout, + } if machineReadable { - CoreConfig.Ui = &packer.MachineReadableUi{ + ui = &packer.MachineReadableUi{ Writer: os.Stdout, } @@ -158,6 +159,20 @@ func wrappedMain() int { } } + // Create the CLI meta + CommandMeta = &command.Meta{ + CoreConfig: &packer.CoreConfig{ + Components: packer.ComponentFinder{ + Builder: config.LoadBuilder, + Hook: config.LoadHook, + PostProcessor: config.LoadPostProcessor, + Provisioner: config.LoadProvisioner, + }, + }, + Cache: cache, + Ui: ui, + } + //setupSignalHandlers(env) cli := &cli.CLI{ diff --git a/packer/core.go b/packer/core.go index 3969da9c9..e8329b821 100644 --- a/packer/core.go +++ b/packer/core.go @@ -2,7 +2,6 @@ package packer import ( "fmt" - "os" "sort" "github.com/hashicorp/go-multierror" @@ -13,9 +12,7 @@ import ( // Core is the main executor of Packer. If Packer is being used as a // library, this is the struct you'll want to instantiate to get anything done. type Core struct { - cache Cache components ComponentFinder - ui Ui template *template.Template variables map[string]string builds map[string]*template.Builder @@ -24,9 +21,7 @@ type Core struct { // CoreConfig is the structure for initializing a new Core. Once a CoreConfig // is used to initialize a Core, it shouldn't be re-used or modified again. type CoreConfig struct { - Cache Cache Components ComponentFinder - Ui Ui Template *template.Template Variables map[string]string } @@ -55,14 +50,6 @@ type ComponentFinder struct { // NewCore creates a new Core. func NewCore(c *CoreConfig) (*Core, error) { - if c.Ui == nil { - c.Ui = &BasicUi{ - Reader: os.Stdin, - Writer: os.Stdout, - ErrorWriter: os.Stdout, - } - } - // Go through and interpolate all the build names. We shuld be able // to do this at this point with the variables. builds := make(map[string]*template.Builder) @@ -80,9 +67,7 @@ func NewCore(c *CoreConfig) (*Core, error) { } return &Core{ - cache: c.Cache, components: c.Components, - ui: c.Ui, template: c.Template, variables: c.Variables, builds: builds, diff --git a/packer/testing.go b/packer/testing.go index 7e7ad0b53..7217a5083 100644 --- a/packer/testing.go +++ b/packer/testing.go @@ -3,7 +3,6 @@ package packer import ( "bytes" "io/ioutil" - "os" "testing" ) @@ -20,9 +19,7 @@ func TestCoreConfig(t *testing.T) *CoreConfig { } return &CoreConfig{ - Cache: &FileCache{CacheDir: os.TempDir()}, Components: components, - Ui: TestUi(t), } }