-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcatalog_types.go
82 lines (66 loc) · 1.99 KB
/
catalog_types.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
package main
import (
"context"
"fmt"
"os"
"github.com/alexeldeib/incli/client"
kitlog "github.com/go-kit/log"
"github.com/spf13/cobra"
)
func NewGetCatalogTypesCommand() *cobra.Command {
opts := &GetCatalogTypesOptions{}
cmd := &cobra.Command{
Use: "get",
Short: "get one or all catalog types, by name or 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().StringVar(&opts.catalogTypeName, "name", "", "catalog type name, e.g. PagerdutyService")
cmd.Flags().StringVar(&opts.catalogTypeID, "id", "", "catalog type id, e.g. 01HE6...")
return cmd
}
type GetCatalogTypesOptions struct {
catalogTypeName string
catalogTypeID string
}
func (o *GetCatalogTypesOptions) 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.catalogTypeID != "" {
res, err := FindCatalogTypeByID(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 != "" {
res, err := FindCatalogTypeByName(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
}
res, err := ListAllCatalogTypes(ctx, logger, cl)
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
}