/
cli_contacts.go
131 lines (114 loc) · 2.76 KB
/
cli_contacts.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package upctl
import (
"context"
"github.com/spf13/cobra"
"github.com/uptime-com/uptime-client-go/v2/pkg/upapi"
)
var (
contactsCmd = &cobra.Command{
Use: "contacts",
Short: "Manage contacts",
}
)
func init() {
cmd.AddCommand(contactsCmd)
}
var (
contactsListFlags = upapi.ContactListOptions{
Page: 1,
PageSize: 100,
Ordering: "pk",
}
contactsListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List contacts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return output(contactsList(cmd.Context()))
},
}
)
func init() {
err := Bind(contactsListCmd.Flags(), &contactsListFlags)
if err != nil {
panic(err)
}
contactsCmd.AddCommand(contactsListCmd)
}
func contactsList(ctx context.Context) ([]upapi.Contact, error) {
return api.Contacts().List(ctx, contactsListFlags)
}
var (
contactsCreateFlags = upapi.Contact{}
contactsCreateCmd = &cobra.Command{
Use: "create <type>",
Aliases: []string{"add"},
Short: "Create a new check",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return output(contactsCreate(cmd.Context()))
},
}
)
func init() {
err := Bind(contactsCreateCmd.Flags(), &contactsCreateFlags)
if err != nil {
panic(err)
}
contactsCmd.AddCommand(contactsCreateCmd)
}
func contactsCreate(ctx context.Context) (*upapi.Contact, error) {
return api.Contacts().Create(ctx, contactsCreateFlags)
}
var (
contactsUpdateFlags = upapi.Contact{}
contactsUpdateCmd = &cobra.Command{
Use: "update <pk>",
Aliases: []string{"up"},
Short: "Update existing contact",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return output(contactsUpdate(cmd.Context(), args[0]))
},
}
)
func init() {
err := Bind(contactsUpdateCmd.Flags(), &contactsUpdateFlags)
if err != nil {
panic(err)
}
contactsCmd.AddCommand(contactsUpdateCmd)
}
func contactsUpdate(ctx context.Context, arg string) (*upapi.Contact, error) {
pk, err := parsePK(arg)
if err != nil {
return nil, err
}
return api.Contacts().Update(ctx, upapi.PrimaryKey(pk), contactsUpdateFlags)
}
var (
contactsDeleteCmd = &cobra.Command{
Use: "delete",
Aliases: []string{"del", "rm"},
Short: "Delete a tag",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return output(contactsDelete(cmd.Context(), args[0]))
},
}
)
func init() {
contactsCmd.AddCommand(contactsDeleteCmd)
}
func contactsDelete(ctx context.Context, pkstr string) (*upapi.Contact, error) {
pk, err := parsePK(pkstr)
if err != nil {
return nil, err
}
obj, err := api.Contacts().Get(ctx, upapi.PrimaryKey(pk))
if err != nil {
return nil, err
}
return obj, api.Contacts().Delete(ctx, upapi.PrimaryKey(pk))
}