Add some exit codes and use a constant for -PACKERSPACE-

This commit is contained in:
Chris Bednarski 2015-08-17 20:33:50 -07:00
parent 9fa93712a1
commit e080e73b04
2 changed files with 25 additions and 12 deletions

View File

@ -3,7 +3,6 @@ package command
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"strings" "strings"
"github.com/mitchellh/packer/builder/amazon/chroot" "github.com/mitchellh/packer/builder/amazon/chroot"
@ -105,7 +104,7 @@ func (c *PluginCommand) Run(args []string) int {
log.Printf("args: %#v", args) log.Printf("args: %#v", args)
if len(args) != 1 { if len(args) != 1 {
c.Ui.Error("Wrong number of args") c.Ui.Error("Wrong number of args")
os.Exit(1) return 1
} }
// Plugin should be called like "packer-builder-amazon-ebs" so we'll take it // Plugin should be called like "packer-builder-amazon-ebs" so we'll take it
@ -124,25 +123,29 @@ func (c *PluginCommand) Run(args []string) int {
server, err := plugin.Server() server, err := plugin.Server()
if err != nil { if err != nil {
panic(err) c.Ui.Error(fmt.Sprintf("Error starting plugin server: %s", err))
return 1
} }
if pluginType == "builder" { if pluginType == "builder" {
builder, found := Builders[pluginName] builder, found := Builders[pluginName]
if !found { if !found {
c.Ui.Error(fmt.Sprintf("Could not load builder: %s", pluginName)) c.Ui.Error(fmt.Sprintf("Could not load builder: %s", pluginName))
return 1
} }
server.RegisterBuilder(builder) server.RegisterBuilder(builder)
} else if pluginType == "provisioner" { } else if pluginType == "provisioner" {
provisioner, found := Provisioners[pluginName] provisioner, found := Provisioners[pluginName]
if !found { if !found {
c.Ui.Error(fmt.Sprintf("Could not load provisioner: %s", pluginName)) c.Ui.Error(fmt.Sprintf("Could not load provisioner: %s", pluginName))
return 1
} }
server.RegisterProvisioner(provisioner) server.RegisterProvisioner(provisioner)
} else if pluginType == "post-processor" { } else if pluginType == "post-processor" {
postProcessor, found := PostProcessors[pluginName] postProcessor, found := PostProcessors[pluginName]
if !found { if !found {
c.Ui.Error(fmt.Sprintf("Could not load post-processor: %s", pluginName)) c.Ui.Error(fmt.Sprintf("Could not load post-processor: %s", pluginName))
return 1
} }
server.RegisterPostProcessor(postProcessor) server.RegisterPostProcessor(postProcessor)
} }
@ -156,13 +159,14 @@ func (*PluginCommand) Help() string {
helpText := ` helpText := `
Usage: packer plugin PLUGIN Usage: packer plugin PLUGIN
Runs an internally-compiled version of a plugin from the packer binary. Note Runs an internally-compiled version of a plugin from the packer binary.
that this is an internal command and you should not call it yourself.
NOTE: this is an internal command and you should not call it yourself.
` `
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
} }
func (c *PluginCommand) Synopsis() string { func (c *PluginCommand) Synopsis() string {
return "call an internal plugin" return "internal plugin command"
} }

View File

@ -16,6 +16,10 @@ import (
"github.com/mitchellh/packer/packer/plugin" "github.com/mitchellh/packer/packer/plugin"
) )
// PACKERSPACE is used to represent the spaces that separate args for a command
// without being confused with spaces in the path to the command itself.
const PACKERSPACE = "-PACKERSPACE-"
type config struct { type config struct {
DisableCheckpoint bool `json:"disable_checkpoint"` DisableCheckpoint bool `json:"disable_checkpoint"`
DisableCheckpointSignature bool `json:"disable_checkpoint_signature"` DisableCheckpointSignature bool `json:"disable_checkpoint_signature"`
@ -80,7 +84,7 @@ func (c *config) Discover() error {
return err return err
} }
// Finally, try to use an internal plugin. Note that this will not Override // Finally, try to use an internal plugin. Note that this will not override
// any previously-loaded plugins. // any previously-loaded plugins.
if err := c.discoverInternal(); err != nil { if err := c.discoverInternal(); err != nil {
return err return err
@ -216,7 +220,8 @@ func (c *config) discoverInternal() error {
_, found := (c.Builders)[builder] _, found := (c.Builders)[builder]
if !found { if !found {
log.Printf("Using internal plugin for %s", builder) log.Printf("Using internal plugin for %s", builder)
(c.Builders)[builder] = fmt.Sprintf("%s-PACKERSPACE-plugin-PACKERSPACE-packer-builder-%s", packerPath, builder) (c.Builders)[builder] = fmt.Sprintf("%s%splugin%spacker-builder-%s",
packerPath, PACKERSPACE, PACKERSPACE, builder)
} }
} }
@ -224,7 +229,9 @@ func (c *config) discoverInternal() error {
_, found := (c.Provisioners)[provisioner] _, found := (c.Provisioners)[provisioner]
if !found { if !found {
log.Printf("Using internal plugin for %s", provisioner) log.Printf("Using internal plugin for %s", provisioner)
(c.Provisioners)[provisioner] = fmt.Sprintf("%s-PACKERSPACE-plugin-PACKERSPACE-packer-provisioner-%s", packerPath, provisioner) (c.Provisioners)[provisioner] = fmt.Sprintf(
"%s%splugin%spacker-provisioner-%s",
packerPath, PACKERSPACE, PACKERSPACE, provisioner)
} }
} }
@ -232,7 +239,9 @@ func (c *config) discoverInternal() error {
_, found := (c.PostProcessors)[postProcessor] _, found := (c.PostProcessors)[postProcessor]
if !found { if !found {
log.Printf("Using internal plugin for %s", postProcessor) log.Printf("Using internal plugin for %s", postProcessor)
(c.PostProcessors)[postProcessor] = fmt.Sprintf("%s-PACKERSPACE-plugin-PACKERSPACE-packer-post-processor-%s", packerPath, postProcessor) (c.PostProcessors)[postProcessor] = fmt.Sprintf(
"%s%splugin%spacker-post-processor-%s",
packerPath, PACKERSPACE, PACKERSPACE, postProcessor)
} }
} }
@ -259,8 +268,8 @@ func (c *config) pluginClient(path string) *plugin.Client {
// Check for special case using `packer plugin PLUGIN` // Check for special case using `packer plugin PLUGIN`
args := []string{} args := []string{}
if strings.Contains(path, "-PACKERSPACE-") { if strings.Contains(path, PACKERSPACE) {
parts := strings.Split(path, "-PACKERSPACE-") parts := strings.Split(path, PACKERSPACE)
path = parts[0] path = parts[0]
args = parts[1:] args = parts[1:]
} }