Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(394)

Side by Side Diff: charm/actions.go

Issue 94540044: Added Actions to charm.
Patch Set: Added Actions to charm. Created 10 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2011, 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details.
3
4 package charm
5
6 import (
7 "fmt"
8 "io"
9 "io/ioutil"
10 "regexp"
11
12 "github.com/binary132/gojsonschema"
13 "launchpad.net/goyaml"
14 )
15
16 var nameRule = regexp.MustCompile("^[a-z](?:[a-z-]*[a-z])?$")
17
18 // Actions defines the available actions for the charm.
19 type Actions struct {
20 ActionSpecs map[string]ActionSpec `yaml:"actions"`
21 }
22
23 // ActionSpec is a definition of the parameters and traits of an Action.
24 type ActionSpec struct {
25 Description string
26 Params map[string]interface{}
27 }
28
29 // ReadActions builds an Actions spec from a charm's actions.yaml.
30 func ReadActionsYaml(r io.Reader) (*Actions, error) {
31 data, err := ioutil.ReadAll(r)
32 if err != nil {
33 return nil, err
34 }
35 var unmarshaledActions Actions
36 if err := goyaml.Unmarshal(data, &unmarshaledActions); err != nil {
37 return nil, err
38 }
39
40 for name, actionSpec := range unmarshaledActions.ActionSpecs {
41 if valid := nameRule.MatchString(name); !valid {
42 return nil, fmt.Errorf("bad action name %s", name)
43 }
44 _, err := gojsonschema.NewJsonSchemaDocument(actionSpec.Params)
45 if err != nil {
46 return nil, fmt.Errorf("invalid params schema for action %q: %v", err)
gz 2014/05/29 15:17:04 This error branch doesn't seem to be tested. The f
47 }
48 for paramName, _ := range unmarshaledActions.ActionSpecs[name].P arams {
49 if valid := nameRule.MatchString(paramName); !valid {
50 return nil, fmt.Errorf("bad param name %s", para mName)
51 }
52 }
53 }
54 return &unmarshaledActions, nil
55 }
OLDNEW
« no previous file with comments | « [revision details] ('k') | charm/actions_test.go » ('j') | charm/actions_test.go » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b