From 6ef6599bae3b6d55a5b95642c00ee839c1361775 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 28 Jul 2018 13:27:17 +0800 Subject: [PATCH] feat: support build command. --- main.go | 18 ++++++++++++++++++ plugin.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/main.go b/main.go index 5f359b5..a13bd31 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,21 @@ func main() { Usage: "Only check syntax. Do not verify config of the template", EnvVar: "PLUGIN_SYNTAX_ONLY", }, + cli.BoolFlag{ + Name: "color", + Usage: "Disable color output (on by default)", + EnvVar: "PLUGIN_COLOR", + }, + cli.BoolFlag{ + Name: "debug", + Usage: "Debug mode enabled for builds", + EnvVar: "PLUGIN_SYNTAX_ONLY", + }, + cli.BoolFlag{ + Name: "parallel", + Usage: "Disable parallelization (on by default)", + EnvVar: "PLUGIN_PARALLEL", + }, } app.Version = Version @@ -95,6 +110,9 @@ func run(c *cli.Context) error { Except: c.StringSlice("except"), Only: c.StringSlice("only"), SyntaxOnly: c.Bool("syntax_only"), + Color: c.Bool("color"), + Debug: c.Bool("debug"), + Parallel: c.Bool("parallel"), }, } diff --git a/plugin.go b/plugin.go index 16cb663..0b662a2 100644 --- a/plugin.go +++ b/plugin.go @@ -20,6 +20,9 @@ type ( SyntaxOnly bool Except []string Only []string + Color bool + Debug bool + Parallel bool } // Plugin values @@ -61,6 +64,47 @@ func pkValidate(config Config) *exec.Cmd { ) } +func pkBuild(config Config) *exec.Cmd { + args := []string{ + "build", + } + + for _, v := range config.VarFiles { + args = append(args, "-var-file", fmt.Sprintf("%s", v)) + } + + for k, v := range config.Vars { + args = append(args, "-var") + args = append(args, fmt.Sprintf("%s=%s", k, v)) + } + + if len(config.Except) > 0 { + args = append(args, "-except="+strings.Join(config.Except, ",")) + } + + if len(config.Only) > 0 { + args = append(args, "-only="+strings.Join(config.Only, ",")) + } + + if config.Parallel { + args = append(args, "-parallel=true") + } + + if config.Color { + args = append(args, "-color=true") + } + + if config.Debug { + args = append(args, "-debug") + } + + args = append(args, config.Template) + return exec.Command( + "packer", + args..., + ) +} + // Exec executes the plugin. func (p *Plugin) Exec() error { var commands []*exec.Cmd @@ -79,6 +123,8 @@ func (p *Plugin) Exec() error { switch action { case "validate": commands = append(commands, pkValidate(p.Config)) + case "build": + commands = append(commands, pkBuild(p.Config)) default: return fmt.Errorf("valid actions are: validate, build You provided %s", action) }