Commit 8ffbc2ef authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Start pulling out the global config stuff

parent 8a78de02
...@@ -49,5 +49,5 @@ func (Command) Run(env packer.Environment, args []string) int { ...@@ -49,5 +49,5 @@ func (Command) Run(env packer.Environment, args []string) int {
} }
func (Command) Synopsis() string { func (Command) Synopsis() string {
return "build image(s) from tempate" return "build image(s) from template"
} }
package main
import (
"fmt"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer/plugin"
"log"
"os/exec"
)
type config struct {
builds map[string]string
commands map[string]string
}
func defaultConfig() (result *config) {
commands := []string{"build"}
result = new(config)
result.builds = make(map[string]string)
result.commands = make(map[string]string)
for _, name := range commands {
result.commands[name] = fmt.Sprintf("packer-command-%s", name)
}
return
}
func (c *config) Commands() (result []string) {
result = make([]string, 0, len(c.commands))
for name, _ := range c.commands {
result = append(result, name)
}
return
}
func (c *config) LoadCommand(name string) (packer.Command, error) {
log.Printf("Loading command: %s\n", name)
commandBin, ok := c.commands[name]
if !ok {
log.Printf("Command not found: %s\n", name)
return nil, nil
}
return plugin.Command(exec.Command(commandBin))
}
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
) )
func main() { func main() {
...@@ -22,27 +21,10 @@ func main() { ...@@ -22,27 +21,10 @@ func main() {
defer plugin.CleanupClients() defer plugin.CleanupClients()
commands := map[string]string { config := defaultConfig()
"build": "packer-build",
}
commandKeys := make([]string, 0, len(commands))
for k, _ := range commands {
commandKeys = append(commandKeys, k)
}
envConfig := packer.DefaultEnvironmentConfig() envConfig := packer.DefaultEnvironmentConfig()
envConfig.Commands = commandKeys envConfig.Commands = config.Commands()
envConfig.CommandFunc = func(n string) (packer.Command, error) { envConfig.CommandFunc = config.LoadCommand
log.Printf("Loading command: %s\n", n)
commandBin, ok := commands[n]
if !ok {
log.Printf("Command not found: %s\n", n)
return nil, nil
}
return plugin.Command(exec.Command(commandBin))
}
env, err := packer.NewEnvironment(envConfig) env, err := packer.NewEnvironment(envConfig)
if err != nil { if err != nil {
......
...@@ -153,6 +153,8 @@ func (e *coreEnvironment) printHelp() { ...@@ -153,6 +153,8 @@ func (e *coreEnvironment) printHelp() {
command, err := e.commandFunc(key) command, err := e.commandFunc(key)
if err != nil { if err != nil {
synopsis = fmt.Sprintf("Error loading command: %s", err.Error()) synopsis = fmt.Sprintf("Error loading command: %s", err.Error())
} else if command == nil {
continue
} else { } else {
synopsis = command.Synopsis() synopsis = command.Synopsis()
} }
......
plugin: plugin:
go get -d -v ./... go get -d -v ./...
go build -v -o $(ROOTDIR)/bin/packer-build go build -v -o $(ROOTDIR)/bin/packer-command-build
format: format:
go fmt ./... go fmt ./...
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment