-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_properties.go
More file actions
112 lines (91 loc) · 2.79 KB
/
contact_properties.go
File metadata and controls
112 lines (91 loc) · 2.79 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cmd
import (
"fmt"
"github.com/loops-so/cli/internal/api"
"github.com/loops-so/cli/internal/config"
"github.com/spf13/cobra"
)
func runContactPropertiesList(cfg *config.Config, customOnly bool) ([]api.ContactProperty, error) {
return newAPIClient(cfg).ListContactProperties(customOnly)
}
func runContactPropertiesCreate(cfg *config.Config, name, propType string) error {
return newAPIClient(cfg).CreateContactProperty(name, propType)
}
var contactPropertiesCmd = &cobra.Command{
Use: "contact-properties",
Short: "Manage contact properties",
}
var contactPropertiesListCmd = &cobra.Command{
Use: "list",
Short: "List contact properties",
RunE: func(cmd *cobra.Command, args []string) error {
if err := validatePickFlags(cmd); err != nil {
return err
}
customOnly, _ := cmd.Flags().GetBool("custom")
cfg, err := loadConfig()
if err != nil {
return err
}
props, err := runContactPropertiesList(cfg, customOnly)
if err != nil {
return err
}
if isJSONOutput() {
if props == nil {
props = []api.ContactProperty{}
}
return printJSON(cmd.OutOrStdout(), props)
}
if len(props) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No contact properties found.")
return nil
}
headers := []string{"KEY", "LABEL", "TYPE"}
rows := make([][]string, 0, len(props))
for _, p := range props {
rows = append(rows, []string{p.Key, p.Label, p.Type})
}
if isPicking(cmd) {
return runPicker(headers, rows, []pickBinding{
copyColumnBinding("enter", "copy key", "property key", rows, 0, cmd.OutOrStdout()),
})
}
t := newStyledTable(cmd.OutOrStdout(), headers...)
for _, r := range rows {
t.Row(r...)
}
return t.Render()
},
}
var contactPropertiesCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a contact property",
RunE: func(cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
propType, _ := cmd.Flags().GetString("type")
cfg, err := loadConfig()
if err != nil {
return err
}
if err := runContactPropertiesCreate(cfg, name, propType); err != nil {
return err
}
if isJSONOutput() {
return printJSON(cmd.OutOrStdout(), Result{Success: true})
}
fmt.Fprintln(cmd.OutOrStdout(), "Created.")
return nil
},
}
func init() {
contactPropertiesListCmd.Flags().Bool("custom", false, "Only list custom properties")
addPickFlag(contactPropertiesListCmd)
contactPropertiesCmd.AddCommand(contactPropertiesListCmd)
contactPropertiesCreateCmd.Flags().String("name", "", "Property name (camelCase, e.g. planName)")
contactPropertiesCreateCmd.Flags().String("type", "", "Property type")
contactPropertiesCreateCmd.MarkFlagRequired("name")
contactPropertiesCreateCmd.MarkFlagRequired("type")
contactPropertiesCmd.AddCommand(contactPropertiesCreateCmd)
rootCmd.AddCommand(contactPropertiesCmd)
}