Parse/load configs
This commit is contained in:
parent
8ffbc2efe7
commit
5f8330ecc2
38
config.go
38
config.go
|
@ -1,43 +1,47 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"github.com/mitchellh/packer/packer/plugin"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// This is the default, built-in configuration that ships with
|
||||
// Packer.
|
||||
const defaultConfig = `
|
||||
[commands]
|
||||
build = "packer-command-build"
|
||||
`
|
||||
|
||||
type config struct {
|
||||
builds map[string]string
|
||||
commands map[string]string
|
||||
Builders map[string]string
|
||||
Commands map[string]string
|
||||
}
|
||||
|
||||
func defaultConfig() (result *config) {
|
||||
commands := []string{"build"}
|
||||
|
||||
// Parses a configuration file and returns a proper configuration
|
||||
// struct.
|
||||
func parseConfig(data string) (result *config, err error) {
|
||||
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)
|
||||
}
|
||||
|
||||
_, err = toml.Decode(data, &result)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *config) Commands() (result []string) {
|
||||
result = make([]string, 0, len(c.commands))
|
||||
for name, _ := range c.commands {
|
||||
// Returns an array of defined command names.
|
||||
func (c *config) CommandNames() (result []string) {
|
||||
result = make([]string, 0, len(c.Commands))
|
||||
for name, _ := range c.Commands {
|
||||
result = append(result, name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// This is a proper packer.CommandFunc that can be used to load packer.Command
|
||||
// implementations from the defined plugins.
|
||||
func (c *config) LoadCommand(name string) (packer.Command, error) {
|
||||
log.Printf("Loading command: %s\n", name)
|
||||
commandBin, ok := c.commands[name]
|
||||
commandBin, ok := c.Commands[name]
|
||||
if !ok {
|
||||
log.Printf("Command not found: %s\n", name)
|
||||
return nil, nil
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"cgl.tideland.biz/asserts"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfig_ParseConfig_Bad(t *testing.T) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
data := `
|
||||
[commands]
|
||||
foo = bar
|
||||
`
|
||||
|
||||
_, err := parseConfig(data)
|
||||
assert.NotNil(err, "should have an error")
|
||||
}
|
||||
|
||||
func TestConfig_ParseConfig_Good(t *testing.T) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
data := `
|
||||
[commands]
|
||||
foo = "bar"
|
||||
`
|
||||
|
||||
c, err := parseConfig(data)
|
||||
assert.Nil(err, "should not have an error")
|
||||
assert.Equal(c.CommandNames(), []string{"foo"}, "should have correct command names")
|
||||
assert.Equal(c.Commands["foo"], "bar", "should have the command")
|
||||
}
|
|
@ -21,9 +21,14 @@ func main() {
|
|||
|
||||
defer plugin.CleanupClients()
|
||||
|
||||
config := defaultConfig()
|
||||
config, err := parseConfig(defaultConfig)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading global Packer configuration: \n\n%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
envConfig := packer.DefaultEnvironmentConfig()
|
||||
envConfig.Commands = config.Commands()
|
||||
envConfig.Commands = config.CommandNames()
|
||||
envConfig.CommandFunc = config.LoadCommand
|
||||
|
||||
env, err := packer.NewEnvironment(envConfig)
|
||||
|
|
Loading…
Reference in New Issue