This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
new_common.go
98 lines (89 loc) · 1.97 KB
/
new_common.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cli
import (
"fmt"
"github.com/digitalrebar/provision/v4/models"
"github.com/spf13/cobra"
)
type ops struct {
name string
singleName string
example func() models.Model
mustPut bool
noCreate bool
noUpdate bool
noDestroy bool
noWait bool
extraCommands []*cobra.Command
actionName string
}
func maybeEncryptParam(param string,
prefix, key string,
val interface{}) (interface{}, error) {
p := &models.Param{}
if err := Session.FillModel(p, param); err != nil {
return val, nil
}
if !p.Secure {
return val, nil
}
k := []byte{}
if err := Session.Req().UrlFor(prefix, key, "pubkey").Do(&k); err != nil {
return nil, err
}
sv := &models.SecureData{}
return sv, sv.Marshal(k, val)
}
func (o *ops) refOrFill(key string) (data models.Model, err error) {
data = o.example()
if o.singleName == "extended" {
(*(data.(*models.RawModel)))["Type"] = o.name
}
if ref == "" {
if err = Session.FillModel(data, key); err != nil {
return
}
} else {
err = bufOrFileDecode(ref, &data)
}
return
}
func (o *ops) addCommand(c *cobra.Command) {
o.extraCommands = append(o.extraCommands, c)
}
func (o *ops) command(app *cobra.Command) {
res := &cobra.Command{
Use: o.name,
Short: fmt.Sprintf("Access CLI commands relating to %v", o.name),
}
if o.name == "extended" {
res.PersistentFlags().StringVarP(&o.name,
"ldata", "l", "",
"object type for extended data commands")
}
if o.example != nil {
ref := o.example()
if o.singleName == "extended" {
(*(ref.(*models.RawModel)))["Type"] = o.name
}
if _, ok := ref.(models.BootEnver); ok {
o.bootenv()
}
if _, ok := ref.(models.Paramer); ok {
o.params()
}
if _, ok := ref.(models.Profiler); ok {
o.profiles()
}
if _, ok := ref.(models.Tasker); ok {
o.tasks()
}
if _, ok := ref.(models.Actor); ok {
o.actions()
}
if _, ok := ref.(models.MetaHaver); ok {
o.meta()
}
res.AddCommand(o.commands()...)
}
app.AddCommand(res)
}