Parse/load configs
This commit is contained in:
parent
8ffbc2efe7
commit
5f8330ecc2
38
config.go
38
config.go
|
@ -1,43 +1,47 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"github.com/mitchellh/packer/packer/plugin"
|
"github.com/mitchellh/packer/packer/plugin"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// This is the default, built-in configuration that ships with
|
||||||
|
// Packer.
|
||||||
|
const defaultConfig = `
|
||||||
|
[commands]
|
||||||
|
build = "packer-command-build"
|
||||||
|
`
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
builds map[string]string
|
Builders map[string]string
|
||||||
commands map[string]string
|
Commands map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultConfig() (result *config) {
|
// Parses a configuration file and returns a proper configuration
|
||||||
commands := []string{"build"}
|
// struct.
|
||||||
|
func parseConfig(data string) (result *config, err error) {
|
||||||
result = new(config)
|
result = new(config)
|
||||||
result.builds = make(map[string]string)
|
_, err = toml.Decode(data, &result)
|
||||||
result.commands = make(map[string]string)
|
|
||||||
|
|
||||||
for _, name := range commands {
|
|
||||||
result.commands[name] = fmt.Sprintf("packer-command-%s", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *config) Commands() (result []string) {
|
// Returns an array of defined command names.
|
||||||
result = make([]string, 0, len(c.commands))
|
func (c *config) CommandNames() (result []string) {
|
||||||
for name, _ := range c.commands {
|
result = make([]string, 0, len(c.Commands))
|
||||||
|
for name, _ := range c.Commands {
|
||||||
result = append(result, name)
|
result = append(result, name)
|
||||||
}
|
}
|
||||||
return
|
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) {
|
func (c *config) LoadCommand(name string) (packer.Command, error) {
|
||||||
log.Printf("Loading command: %s\n", name)
|
log.Printf("Loading command: %s\n", name)
|
||||||
commandBin, ok := c.commands[name]
|
commandBin, ok := c.Commands[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Printf("Command not found: %s\n", name)
|
log.Printf("Command not found: %s\n", name)
|
||||||
return nil, nil
|
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()
|
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 := packer.DefaultEnvironmentConfig()
|
||||||
envConfig.Commands = config.Commands()
|
envConfig.Commands = config.CommandNames()
|
||||||
envConfig.CommandFunc = config.LoadCommand
|
envConfig.CommandFunc = config.LoadCommand
|
||||||
|
|
||||||
env, err := packer.NewEnvironment(envConfig)
|
env, err := packer.NewEnvironment(envConfig)
|
||||||
|
|
Loading…
Reference in New Issue