-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdomain.go
More file actions
152 lines (141 loc) · 3.76 KB
/
domain.go
File metadata and controls
152 lines (141 loc) · 3.76 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package cmd
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/opslevel/cli/common"
"github.com/opslevel/opslevel-go/v2025"
"github.com/spf13/cobra"
)
var exampleDomainCmd = &cobra.Command{
Use: "domain",
Short: "Example Domain",
Long: `Example Domain`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getExample(opslevel.DomainInput{
Name: opslevel.RefOf("example_name"),
Description: opslevel.RefOf("example_description"),
OwnerId: opslevel.RefOf(opslevel.ID("Z2lkOi8vc2VydmljZS8xMjM0NTY3ODk")),
Note: opslevel.RefOf("example_note"),
}))
},
}
var createDomainCmd = &cobra.Command{
Use: "domain",
Short: "Create a domain",
Long: `Create a domain`,
Example: `
cat << EOF | opslevel create domain -f -
name: "My Domain"
description: "Hello World Domain"
owner: "Z2lkOi8vb3BzbGV2ZWwvVGVhbS83NjY"
note: "Additional details"
EOF
`,
Run: func(cmd *cobra.Command, args []string) {
input, err := readResourceInput[opslevel.DomainInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateDomain(*input)
cobra.CheckErr(err)
fmt.Println(result.Id)
},
}
var deleteDomainCmd = &cobra.Command{
Use: "domain ID|ALIAS",
Short: "Delete a domain",
Long: `Delete a domain`,
Example: `
opslevel delete domain my_domain
`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID", "ALIAS"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
err := getClientGQL().DeleteDomain(key)
cobra.CheckErr(err)
fmt.Printf("deleted '%s' domain\n", key)
},
}
var getDomainCmd = &cobra.Command{
Use: "domain ID|ALIAS",
Short: "Get details about a domain",
Long: `Get details about a domain`,
Example: `
opslevel get domain my_domain
`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID", "ALIAS"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
result, err := getClientGQL().GetDomain(key)
cobra.CheckErr(err)
common.WasFound(result.Id == "", key)
if isYamlOutput() {
common.YamlPrint(result)
} else {
common.PrettyPrint(result)
}
},
}
var listDomainCmd = &cobra.Command{
Use: "domain",
Aliases: []string{"domains"},
Short: "Lists the domains",
Long: `Lists the domains`,
Example: `
opslevel list domain
`,
Run: func(cmd *cobra.Command, args []string) {
resp, err := getClientGQL().ListDomains(nil)
list := resp.Nodes
cobra.CheckErr(err)
if isJsonOutput() {
common.JsonPrint(json.MarshalIndent(list, "", " "))
} else if isCsvOutput() {
w := csv.NewWriter(os.Stdout)
w.Write([]string{"NAME", "ID", "ALIASES"})
for _, item := range list {
w.Write([]string{item.Name, string(item.Id), strings.Join(item.Aliases, "/")})
}
w.Flush()
} else {
w := common.NewTabWriter("NAME", "ALIAS", "ID")
for _, item := range list {
fmt.Fprintf(w, "%s\t%s\t%s\t\n", item.Name, item.Id, strings.Join(item.Aliases, ","))
}
w.Flush()
}
},
}
var updateDomainCmd = &cobra.Command{
Use: "domain ID|ALIAS",
Short: "Update a domain",
Long: `Update a domain
cat << EOF | opslevel update domain my_domain -f -
name: "My New Domain"
description: "Hello World New Domain"
owner: "Z2lkOi8vb3BzbGV2ZWwvVGVhbS83ODk"
note: "Additional details for my new domain"
EOF
`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID", "ALIAS"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
input, err := readResourceInput[opslevel.DomainInput]()
cobra.CheckErr(err)
domain, err := getClientGQL().UpdateDomain(key, *input)
cobra.CheckErr(err)
fmt.Println(domain.Id)
},
}
func init() {
exampleCmd.AddCommand(exampleDomainCmd)
createCmd.AddCommand(createDomainCmd)
deleteCmd.AddCommand(deleteDomainCmd)
getCmd.AddCommand(getDomainCmd)
listCmd.AddCommand(listDomainCmd)
updateCmd.AddCommand(updateDomainCmd)
}