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 {
}
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 (
"io/ioutil"
"log"
"os"
"os/exec"
)
func main() {
......@@ -22,27 +21,10 @@ func main() {
defer plugin.CleanupClients()
commands := map[string]string {
"build": "packer-build",
}
commandKeys := make([]string, 0, len(commands))
for k, _ := range commands {
commandKeys = append(commandKeys, k)
}
config := defaultConfig()
envConfig := packer.DefaultEnvironmentConfig()
envConfig.Commands = commandKeys
envConfig.CommandFunc = func(n string) (packer.Command, error) {
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))
}
envConfig.Commands = config.Commands()
envConfig.CommandFunc = config.LoadCommand
env, err := packer.NewEnvironment(envConfig)
if err != nil {
......
......@@ -153,6 +153,8 @@ func (e *coreEnvironment) printHelp() {
command, err := e.commandFunc(key)
if err != nil {
synopsis = fmt.Sprintf("Error loading command: %s", err.Error())
} else if command == nil {
continue
} else {
synopsis = command.Synopsis()
}
......
plugin:
go get -d -v ./...
go build -v -o $(ROOTDIR)/bin/packer-build
go build -v -o $(ROOTDIR)/bin/packer-command-build
format:
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