-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
70 lines (65 loc) · 1.96 KB
/
new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package action
import (
"fmt"
"strings"
"github.com/pkg/errors"
"admini.dev/admini/app/util"
)
func newAction(args []string, title string, typ *Type, pkg util.Pkg) (*Action, error) {
if typ == nil {
return nil, errors.New("action has nil type")
}
cfg := util.ValueMap{}
switch typ {
case TypeFolder:
return base(typ, "folder", title, pkg, cfg), nil
case TypeStatic:
return base(typ, typ.Key, title, pkg, cfg), nil
case TypeSeparator:
return base(typ, "", "", pkg, cfg), nil
case TypeAll:
return base(typ, typ.Key, title, pkg, cfg), nil
case TypeSource:
if len(args) == 0 {
return nil, errors.New("require one argument")
}
srcKey := args[0]
cfg[TypeSource.Key] = srcKey
return base(typ, srcKey, title, pkg, cfg), nil
case TypePackage:
if len(args) < 2 {
return nil, errors.Errorf("require at least two arguments, observed [%d]", len(args))
}
srcKey := args[0]
cfg[TypeSource.Key] = srcKey
key := args[len(args)-1]
cfg[TypePackage.Key] = strings.Join(args[1:], "/")
return base(typ, key, title, pkg, cfg), nil
case TypeModel:
if len(args) < 2 {
return nil, errors.Errorf("require at least two arguments, observed [%d]", len(args))
}
srcKey := args[0]
cfg[TypeSource.Key] = srcKey
key := args[len(args)-1]
cfg[TypeModel.Key] = strings.Join(args[1:], "/")
return base(typ, key, title, pkg, cfg), nil
case TypeActivity:
if len(args) != 2 {
return nil, errors.Errorf("require exactly two arguments, observed [%d]", len(args))
}
srcKey := args[0]
activity := args[1]
cfg[TypeSource.Key] = srcKey
cfg[TypeActivity.Key] = activity
return base(typ, fmt.Sprintf("%s-%s", srcKey, activity), title, pkg, cfg), nil
default:
return nil, errors.Errorf("can't create unhandled action [%s]", typ.Key)
}
}
func base(typ *Type, key string, title string, pkg util.Pkg, cfg util.ValueMap) *Action {
if cfg == nil {
cfg = util.ValueMap{}
}
return &Action{Key: "__" + key, TypeKey: typ.Key, Title: title, Pkg: pkg, Config: cfg}
}