mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-27 18:24:01 -04:00
packer: remove Environment
This commit is contained in:
+11
-18
@@ -3,7 +3,6 @@ package command
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@@ -17,28 +16,22 @@ type FixCommand struct {
|
||||
}
|
||||
|
||||
func (c *FixCommand) Run(args []string) int {
|
||||
env, err := c.Meta.Environment()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error initializing environment: %s", err))
|
||||
flags := c.Meta.FlagSet("fix", FlagSetNone)
|
||||
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
cmdFlags := flag.NewFlagSet("fix", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
args = flags.Args()
|
||||
if len(args) != 1 {
|
||||
cmdFlags.Usage()
|
||||
flags.Usage()
|
||||
return 1
|
||||
}
|
||||
|
||||
// Read the file for decoding
|
||||
tplF, err := os.Open(args[0])
|
||||
if err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Error opening template: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Error opening template: %s", err))
|
||||
return 1
|
||||
}
|
||||
defer tplF.Close()
|
||||
@@ -47,7 +40,7 @@ func (c *FixCommand) Run(args []string) int {
|
||||
var templateData map[string]interface{}
|
||||
decoder := json.NewDecoder(tplF)
|
||||
if err := decoder.Decode(&templateData); err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Error parsing template: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Error parsing template: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -65,7 +58,7 @@ func (c *FixCommand) Run(args []string) int {
|
||||
log.Printf("Running fixer: %s", name)
|
||||
input, err = fixer.Fix(input)
|
||||
if err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Error fixing: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Error fixing: %s", err))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
@@ -73,20 +66,20 @@ func (c *FixCommand) Run(args []string) int {
|
||||
var output bytes.Buffer
|
||||
encoder := json.NewEncoder(&output)
|
||||
if err := encoder.Encode(input); err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Error encoding: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Error encoding: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
var indented bytes.Buffer
|
||||
if err := json.Indent(&indented, output.Bytes(), "", " "); err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Error encoding: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Error encoding: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
result := indented.String()
|
||||
result = strings.Replace(result, `\u003c`, "<", -1)
|
||||
result = strings.Replace(result, `\u003e`, ">", -1)
|
||||
env.Ui().Say(result)
|
||||
c.Ui.Say(result)
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
+5
-12
@@ -1,7 +1,6 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"log"
|
||||
@@ -9,19 +8,13 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InspectCommand struct{
|
||||
type InspectCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (c *InspectCommand) Run(args []string) int {
|
||||
env, err := c.Meta.Environment()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error initializing environment: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
flags := flag.NewFlagSet("inspect", flag.ContinueOnError)
|
||||
flags.Usage = func() { env.Ui().Say(c.Help()) }
|
||||
flags := c.Meta.FlagSet("build", FlagSetNone)
|
||||
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
@@ -36,12 +29,12 @@ func (c *InspectCommand) Run(args []string) int {
|
||||
log.Printf("Reading template: %#v", args[0])
|
||||
tpl, err := packer.ParseTemplateFile(args[0], nil)
|
||||
if err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Failed to parse template: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// Convenience...
|
||||
ui := env.Ui()
|
||||
ui := c.Ui
|
||||
|
||||
// Description
|
||||
if tpl.Description != "" {
|
||||
|
||||
@@ -26,7 +26,6 @@ const (
|
||||
// Packer command inherits.
|
||||
type Meta struct {
|
||||
CoreConfig *packer.CoreConfig
|
||||
EnvConfig *packer.EnvironmentConfig
|
||||
Ui packer.Ui
|
||||
|
||||
// These are set by command-line flags
|
||||
@@ -148,7 +147,3 @@ func (m *Meta) ValidateFlags() error {
|
||||
// TODO
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Meta) Environment() (packer.Environment, error) {
|
||||
return packer.NewEnvironment(m.EnvConfig)
|
||||
}
|
||||
|
||||
+41
-59
@@ -1,12 +1,12 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
cmdcommon "github.com/mitchellh/packer/common/command"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"github.com/mitchellh/packer/template"
|
||||
)
|
||||
|
||||
type ValidateCommand struct {
|
||||
@@ -15,72 +15,54 @@ type ValidateCommand struct {
|
||||
|
||||
func (c *ValidateCommand) Run(args []string) int {
|
||||
var cfgSyntaxOnly bool
|
||||
buildOptions := new(cmdcommon.BuildOptions)
|
||||
|
||||
env, err := c.Meta.Environment()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error initializing environment: %s", err))
|
||||
flags := c.Meta.FlagSet("validate", FlagSetBuildFilter|FlagSetVars)
|
||||
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
||||
flags.BoolVar(&cfgSyntaxOnly, "syntax-only", false, "check syntax only")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
|
||||
cmdFlags.BoolVar(&cfgSyntaxOnly, "syntax-only", false, "check syntax only")
|
||||
cmdcommon.BuildOptionFlags(cmdFlags, buildOptions)
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
args = flags.Args()
|
||||
if len(args) != 1 {
|
||||
cmdFlags.Usage()
|
||||
flags.Usage()
|
||||
return 1
|
||||
}
|
||||
|
||||
if err := buildOptions.Validate(); err != nil {
|
||||
env.Ui().Error(err.Error())
|
||||
env.Ui().Error("")
|
||||
env.Ui().Error(c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
userVars, err := buildOptions.AllUserVars()
|
||||
// Parse the template
|
||||
tpl, err := template.ParseFile(args[0])
|
||||
if err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Error compiling user variables: %s", err))
|
||||
env.Ui().Error("")
|
||||
env.Ui().Error(c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
// Parse the template into a machine-usable format
|
||||
log.Printf("Reading template: %s", args[0])
|
||||
tpl, err := packer.ParseTemplateFile(args[0], userVars)
|
||||
if err != nil {
|
||||
env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
|
||||
c.Ui.Error(fmt.Sprintf("Failed to parse template: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// If we're only checking syntax, then we're done already
|
||||
if cfgSyntaxOnly {
|
||||
env.Ui().Say("Syntax-only check passed. Everything looks okay.")
|
||||
c.Ui.Say("Syntax-only check passed. Everything looks okay.")
|
||||
return 0
|
||||
}
|
||||
|
||||
// Get the core
|
||||
core, err := c.Meta.Core(tpl)
|
||||
if err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
errs := make([]error, 0)
|
||||
warnings := make(map[string][]string)
|
||||
|
||||
// The component finder for our builds
|
||||
components := &packer.ComponentFinder{
|
||||
Builder: env.Builder,
|
||||
Hook: env.Hook,
|
||||
PostProcessor: env.PostProcessor,
|
||||
Provisioner: env.Provisioner,
|
||||
}
|
||||
// Get the builds we care about
|
||||
buildNames := c.Meta.BuildNames(core)
|
||||
builds := make([]packer.Build, 0, len(buildNames))
|
||||
for _, n := range buildNames {
|
||||
b, err := core.Build(n)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Failed to initialize build '%s': %s",
|
||||
n, err))
|
||||
}
|
||||
|
||||
// Otherwise, get all the builds
|
||||
builds, err := buildOptions.Builds(tpl, components)
|
||||
if err != nil {
|
||||
env.Ui().Error(err.Error())
|
||||
return 1
|
||||
builds = append(builds, b)
|
||||
}
|
||||
|
||||
// Check the configuration of all builds
|
||||
@@ -96,12 +78,12 @@ func (c *ValidateCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
env.Ui().Error("Template validation failed. Errors are shown below.\n")
|
||||
c.Ui.Error("Template validation failed. Errors are shown below.\n")
|
||||
for i, err := range errs {
|
||||
env.Ui().Error(err.Error())
|
||||
c.Ui.Error(err.Error())
|
||||
|
||||
if (i + 1) < len(errs) {
|
||||
env.Ui().Error("")
|
||||
c.Ui.Error("")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,21 +91,21 @@ func (c *ValidateCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
if len(warnings) > 0 {
|
||||
env.Ui().Say("Template validation succeeded, but there were some warnings.")
|
||||
env.Ui().Say("These are ONLY WARNINGS, and Packer will attempt to build the")
|
||||
env.Ui().Say("template despite them, but they should be paid attention to.\n")
|
||||
c.Ui.Say("Template validation succeeded, but there were some warnings.")
|
||||
c.Ui.Say("These are ONLY WARNINGS, and Packer will attempt to build the")
|
||||
c.Ui.Say("template despite them, but they should be paid attention to.\n")
|
||||
|
||||
for build, warns := range warnings {
|
||||
env.Ui().Say(fmt.Sprintf("Warnings for build '%s':\n", build))
|
||||
c.Ui.Say(fmt.Sprintf("Warnings for build '%s':\n", build))
|
||||
for _, warning := range warns {
|
||||
env.Ui().Say(fmt.Sprintf("* %s", warning))
|
||||
c.Ui.Say(fmt.Sprintf("* %s", warning))
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
env.Ui().Say("Template validated successfully.")
|
||||
c.Ui.Say("Template validated successfully.")
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
+3
-9
@@ -33,15 +33,9 @@ func (c *VersionCommand) Help() string {
|
||||
}
|
||||
|
||||
func (c *VersionCommand) Run(args []string) int {
|
||||
env, err := c.Meta.Environment()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error initializing environment: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
env.Ui().Machine("version", c.Version)
|
||||
env.Ui().Machine("version-prelease", c.VersionPrerelease)
|
||||
env.Ui().Machine("version-commit", c.Revision)
|
||||
c.Ui.Machine("version", c.Version)
|
||||
c.Ui.Machine("version-prelease", c.VersionPrerelease)
|
||||
c.Ui.Machine("version-commit", c.Revision)
|
||||
|
||||
var versionString bytes.Buffer
|
||||
fmt.Fprintf(&versionString, "Packer v%s", c.Version)
|
||||
|
||||
Reference in New Issue
Block a user