/
cobra.go
83 lines (57 loc) · 1.33 KB
/
cobra.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
package utils
import (
"strconv"
"broadcastle.co/code/crm/code/db"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// CobraInput returns a string from a flag.
func CobraInput(cmd *cobra.Command, flag string, hint string, value string, skip bool, hidden bool) (string, error) {
result, err := cmd.Flags().GetString(flag)
if err != nil {
return "", err
}
if result == "" && value != "" {
result = value
}
if skip {
return result, nil
}
if hidden {
return InputHidden(hint, result)
}
return Input(hint, result)
}
// CobraInputBool returns a bool from a flag.
func CobraInputBool(cmd *cobra.Command, flag string, hint string, value bool, skip bool) (bool, error) {
result, err := cmd.Flags().GetBool(flag)
if err != nil {
return false, err
}
if value {
result = value
}
if skip {
return result, nil
}
return InputBool(hint, result)
}
// Contacts returns contacts from arguments.
func Contacts(cmd *cobra.Command, args []string) ([]db.Contact, error) {
contacts := []db.Contact{}
for _, x := range args {
single := db.Contact{}
id64, err := strconv.ParseUint(x, 10, 64)
if err == nil {
single.ID = uint(id64)
} else {
single.Slug = x
}
if err := single.Search(); err != nil {
logrus.Warn(err)
break
}
contacts = append(contacts, single)
}
return contacts, nil
}