github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/action-set.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 5 6 import ( 7 "fmt" 8 "regexp" 9 "strings" 10 11 "github.com/juju/cmd" 12 "github.com/juju/gnuflag" 13 ) 14 15 var keyRule = regexp.MustCompile("^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$") 16 17 // ActionSetCommand implements the action-set command. 18 type ActionSetCommand struct { 19 cmd.CommandBase 20 ctx Context 21 args [][]string 22 } 23 24 // NewActionSetCommand returns a new ActionSetCommand with the given context. 25 func NewActionSetCommand(ctx Context) (cmd.Command, error) { 26 return &ActionSetCommand{ctx: ctx}, nil 27 } 28 29 // Info returns the content for --help. 30 func (c *ActionSetCommand) Info() *cmd.Info { 31 doc := ` 32 action-set adds the given values to the results map of the Action. This map 33 is returned to the user after the completion of the Action. Keys must start 34 and end with lowercase alphanumeric, and contain only lowercase alphanumeric, 35 hyphens and periods. 36 37 Example usage: 38 action-set outfile.size=10G 39 action-set foo.bar=2 40 action-set foo.baz.val=3 41 action-set foo.bar.zab=4 42 action-set foo.baz=1 43 44 will yield: 45 46 outfile: 47 size: "10G" 48 foo: 49 bar: 50 zab: "4" 51 baz: "1" 52 ` 53 return &cmd.Info{ 54 Name: "action-set", 55 Args: "<key>=<value> [<key>=<value> ...]", 56 Purpose: "set action results", 57 Doc: doc, 58 } 59 } 60 61 // SetFlags handles known option flags. 62 func (c *ActionSetCommand) SetFlags(f *gnuflag.FlagSet) { 63 // TODO(binary132): add cmd.Input type as in cmd.Output for YAML piping. 64 } 65 66 // Init accepts maps in the form of key=value, key.key2.keyN....=value 67 func (c *ActionSetCommand) Init(args []string) error { 68 c.args = make([][]string, 0) 69 for _, arg := range args { 70 thisArg := strings.SplitN(arg, "=", 2) 71 if len(thisArg) != 2 { 72 return fmt.Errorf("argument %q must be of the form key...=value", arg) 73 } 74 keySlice := strings.Split(thisArg[0], ".") 75 // check each key for validity 76 for _, key := range keySlice { 77 if valid := keyRule.MatchString(key); !valid { 78 return fmt.Errorf("key %q must start and end with lowercase alphanumeric, and contain only lowercase alphanumeric, hyphens and periods", key) 79 } 80 } 81 // [key, key, key, key, value] 82 c.args = append(c.args, append(keySlice, thisArg[1])) 83 } 84 85 return nil 86 } 87 88 // Run adds the given <key list>/<value> pairs, such as foo.bar=baz to the 89 // existing map of results for the Action. 90 func (c *ActionSetCommand) Run(ctx *cmd.Context) error { 91 for _, argSlice := range c.args { 92 valueIndex := len(argSlice) - 1 93 keys := argSlice[:valueIndex] 94 value := argSlice[valueIndex] 95 err := c.ctx.UpdateActionResults(keys, value) 96 if err != nil { 97 return err 98 } 99 } 100 101 return nil 102 }