-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcatalog_entries.go
150 lines (127 loc) · 4.06 KB
/
catalog_entries.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"fmt"
"os"
"github.com/alexeldeib/incli/client"
kitlog "github.com/go-kit/log"
"github.com/spf13/cobra"
)
func NewGetCatalogEntriesCommand() *cobra.Command {
opts := &GetCatalogEntriesOptions{}
cmd := &cobra.Command{
Use: "get",
Short: "get one, many, or all catalog entries, by name/id with or without type name/id",
Run: func(cmd *cobra.Command, args []string) {
ctx, logger, cl, err := setup()
if err != nil {
fmt.Printf("failed to setup: %s", err)
os.Exit(1)
}
if err := opts.Run(ctx, logger, cl); err != nil {
logger.Log("msg", "failed to run", "error", err)
os.Exit(1)
}
},
}
cmd.Flags().StringVarP(&opts.catalogTypeID, "type-id", "t", "", "catalog type id, e.g. 01HE6...")
cmd.Flags().StringVar(&opts.catalogTypeName, "type-name", "", "catalog type name, e.g. PagerdutyService")
cmd.Flags().StringVarP(&opts.catalogEntryName, "name", "n", "", "name or alias of custom catalog entry, e.g. Serving Infra Default")
cmd.Flags().StringVar(&opts.catalogEntryID, "id", "", "custom field to patch, e.g. --field foo=bar --field baz=qux. --field foo=bar=baz sets field `foo` to `bar=baz`")
return cmd
}
type GetCatalogEntriesOptions struct {
catalogTypeName string
catalogTypeID string
catalogEntryName string
catalogEntryID string
}
func (o *GetCatalogEntriesOptions) Run(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) error {
if o.catalogTypeName != "" && o.catalogTypeID != "" {
return fmt.Errorf("exactly one of --type-name or --type-id may be specified")
}
if o.catalogEntryName != "" && o.catalogEntryID != "" {
return fmt.Errorf("exactly one of --entry-name or --entry-id may be specified")
}
if o.catalogEntryID != "" && (o.catalogTypeID != "" || o.catalogTypeName != "") {
return fmt.Errorf("--entry-id is mutually exclusive with both --type-id and --type-name")
}
if o.catalogEntryID != "" {
res, err := FindCatalogEntryByID(ctx, logger, cl, o.catalogEntryID)
if err != nil {
return fmt.Errorf("failed to find catalog entry: %s", err)
}
if err := serialize(res); err != nil {
return fmt.Errorf("failed to marshal json: %q", err)
}
return nil
}
if o.catalogTypeID != "" {
if o.catalogEntryName != "" {
res, err := FindCatalogEntryByNameWithTypeID(ctx, logger, cl, o.catalogEntryName, o.catalogTypeID)
if err != nil {
return fmt.Errorf("failed to find catalog entry: %s", err)
}
if err := serialize(res); err != nil {
return fmt.Errorf("failed to marshal json: %q", err)
}
return nil
} else {
res, err := ListAllCatalogEntriesByTypeID(ctx, logger, cl, o.catalogTypeID)
if err != nil {
return fmt.Errorf("failed to find catalog entry: %s", err)
}
if err := serialize(res); err != nil {
return fmt.Errorf("failed to marshal json: %q", err)
}
return nil
}
} else if o.catalogTypeName != "" {
if o.catalogEntryName != "" {
res, err := FindCatalogEntryByNameWithTypeName(ctx, logger, cl, o.catalogEntryName, o.catalogTypeName)
if err != nil {
return fmt.Errorf("failed to find catalog entry: %s", err)
}
if err := serialize(res); err != nil {
return fmt.Errorf("failed to marshal json: %q", err)
}
} else {
res, err := ListAllCatalogEntriesByTypeName(ctx, logger, cl, o.catalogTypeName)
if err != nil {
return fmt.Errorf("failed to find catalog entry: %s", err)
}
if err := serialize(res); err != nil {
return fmt.Errorf("failed to marshal json: %q", err)
}
return nil
}
} else {
res, err := ListAllCatalogEntries(ctx, logger, cl)
if err != nil {
return fmt.Errorf("failed to list all catalog entries: %s", err)
}
if o.catalogEntryName != "" {
n := 0
for _, v := range res {
if v.Name == o.catalogEntryName {
res[n] = v
n++
}
}
res = res[:n]
} else if o.catalogEntryID != "" {
n := 0
for _, v := range res {
if v.Id == o.catalogEntryID {
res[n] = v
n++
}
}
res = res[:n]
}
if err := serialize(res); err != nil {
return fmt.Errorf("failed to marshal json: %q", err)
}
}
return nil
}