This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
profileHelper.go
67 lines (63 loc) · 1.9 KB
/
profileHelper.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
package cli
import (
"fmt"
"github.com/digitalrebar/provision/v4/models"
"github.com/spf13/cobra"
)
func (o *ops) profiles() {
o.addCommand(&cobra.Command{
Use: "addprofile [id] [profile]",
Short: fmt.Sprintf("Add profile to the %v's profile list", o.singleName),
Long: fmt.Sprintf("Helper function to add a profile to the %v's profile list.", o.singleName),
Args: func(c *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("%v requires 2 arguments", c.UseLine())
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
data, err := o.refOrFill(args[0])
if err != nil {
return err
}
ex := models.Clone(data).(models.Profiler)
ex.SetProfiles(append(ex.GetProfiles(), args[1]))
res, err := Session.PatchToFull(data, ex, ref != "")
if err != nil {
return generateError(err, "Unable to update %s: %v", o.singleName, args[0])
}
return prettyPrint(res)
},
})
o.addCommand(&cobra.Command{
Use: "removeprofile [id] [profile]",
Short: fmt.Sprintf("Remove a profile from the %v's profile list", o.singleName),
Long: fmt.Sprintf("Helper function to update the %v's profile list by removing one.", o.singleName),
Args: func(c *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("%v requires 2 arguments", c.UseLine())
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
data, err := o.refOrFill(args[0])
if err != nil {
return err
}
ex := models.Clone(data).(models.Profiler)
newProfiles := []string{}
for _, s := range ex.GetProfiles() {
if s == args[1] {
continue
}
newProfiles = append(newProfiles, s)
}
ex.SetProfiles(newProfiles)
res, err := Session.PatchToFull(data, ex, ref != "")
if err != nil {
return generateError(err, "Unable to update %s: %v", o.singleName, args[0])
}
return prettyPrint(res)
},
})
}