command/fix: boilerplate for the fix command

This commit is contained in:
Mitchell Hashimoto 2013-07-14 15:29:19 +09:00
parent 186e9509d4
commit aae210f12d
5 changed files with 95 additions and 0 deletions

55
command/fix/command.go Normal file
View File

@ -0,0 +1,55 @@
package fix
import (
"encoding/json"
"flag"
"fmt"
"github.com/mitchellh/packer/packer"
"os"
"strings"
)
type Command byte
func (Command) Help() string {
return strings.TrimSpace(helpString)
}
func (c Command) Run(env packer.Environment, args []string) int {
cmdFlags := flag.NewFlagSet("fix", flag.ContinueOnError)
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) != 1 {
cmdFlags.Usage()
return 1
}
// Read the file for decoding
tplF, err := os.Open(args[0])
if err != nil {
env.Ui().Error(fmt.Sprintf("Error opening template: %s", err))
return 1
}
defer tplF.Close()
// Decode the JSON into a generic map structure
var templateData map[string]interface{}
decoder := json.NewDecoder(tplF)
if err := decoder.Decode(&templateData); err != nil {
env.Ui().Error(fmt.Sprintf("Error parsing template: %s", err))
return 1
}
// Close the file since we're done with that
tplF.Close()
return 0
}
func (c Command) Synopsis() string {
return "fixes templates from old versions of packer"
}

View File

@ -0,0 +1,14 @@
package fix
import (
"github.com/mitchellh/packer/packer"
"testing"
)
func TestCommand_Impl(t *testing.T) {
var raw interface{}
raw = new(Command)
if _, ok := raw.(packer.Command); !ok {
t.Fatalf("must be a Command")
}
}

15
command/fix/help.go Normal file
View File

@ -0,0 +1,15 @@
package fix
const helpString = `
Usage: packer fix [options] TEMPLATE
Reads the JSON template and attempts to fix known backwards
incompatibilities. The fixed template will be outputted to standard out.
If the template cannot be fixed due to an error, the command will exit
with a non-zero exit status. Error messages will appear on standard error.
Options:
-verbose Output each fix to standard error
`

View File

@ -27,6 +27,7 @@ const defaultConfig = `
"commands": {
"build": "packer-command-build",
"fix": "packer-command-fix",
"validate": "packer-command-validate"
},

View File

@ -0,0 +1,10 @@
package main
import (
"github.com/mitchellh/packer/command/fix"
"github.com/mitchellh/packer/packer/plugin"
)
func main() {
plugin.ServeCommand(new(fix.Command))
}