packer: Start pulling out the global config stuff
This commit is contained in:
parent
8a78de02c7
commit
8ffbc2efe7
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
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))
|
||||
}
|
24
packer.go
24
packer.go
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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 ./...
|
||||
|
|
Loading…
Reference in New Issue