/
get_ingest_check.go
154 lines (143 loc) · 4.64 KB
/
get_ingest_check.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
151
152
153
154
package ingestcheck
import (
"context"
"encoding/json"
"fmt"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/formatters"
)
func NewCmdGetIngestCheck(c *cfg.Config) *cobra.Command {
var (
outputFormat string
showIDs bool
goTemplate string
)
cmd := &cobra.Command{
Use: "ingest_check INGEST_CHECK_ID",
Short: "Get a specific ingest check",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
id := args[0]
check, err := c.Cloud.IngestCheck(ctx, id)
if err != nil {
return err
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, check)
}
switch outputFormat {
case "table":
tw := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 3, 1, ' ', 0)
if showIDs {
fmt.Fprintf(tw, "ID\t")
}
fmt.Fprint(tw, "STATUS\tRETRIES\t")
fmt.Fprintln(tw, "AGE")
if showIDs {
fmt.Fprintf(tw, "%s\t", check.ID)
}
fmt.Fprintf(tw, "%s\t", check.Status)
fmt.Fprintf(tw, "%d\t", check.Retries)
fmt.Fprintln(tw, formatters.FmtTime(check.CreatedAt))
err := tw.Flush()
if err != nil {
return err
}
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(check)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(check)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
return nil
},
}
fs := cmd.Flags()
fs.BoolVar(&showIDs, "show-ids", false, "Include member IDs in table output")
fs.StringVarP(&outputFormat, "output-format", "o", "table", "Output format. Allowed: table, json, yaml, go-template, go-template-file")
fs.StringVar(&goTemplate, "template", "", "Template string or path to use when -o=go-template, -o=go-template-file. The template format is golang templates\n[http://golang.org/pkg/text/template/#pkg-overview]")
_ = cmd.RegisterFlagCompletionFunc("output-format", formatters.CompleteOutputFormat)
return cmd
}
func NewCmdGetIngestChecks(c *cfg.Config) *cobra.Command {
var (
outputFormat string
showIDs bool
last uint
goTemplate string
environment string
)
completer := completer.Completer{Config: c}
cmd := &cobra.Command{
Use: "ingest_checks CORE_INSTANCE",
Short: "Get a list of ingest checks",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
id := args[0]
var environmentID string
if environment != "" {
var err error
environmentID, err = completer.LoadEnvironmentID(environment)
if err != nil {
return err
}
}
aggregatorID, err := completer.LoadCoreInstanceID(id, environmentID)
if err != nil {
return err
}
check, err := c.Cloud.IngestChecks(ctx, aggregatorID, types.IngestChecksParams{Last: &last})
if err != nil {
return err
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, check.Items)
}
switch outputFormat {
case "table":
tw := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 3, 1, ' ', 0)
if showIDs {
fmt.Fprintf(tw, "ID\t")
}
fmt.Fprint(tw, "STATUS\tRETRIES\t")
fmt.Fprintln(tw, "AGE")
for _, m := range check.Items {
if showIDs {
fmt.Fprintf(tw, "%s\t", m.ID)
}
fmt.Fprintf(tw, "%s\t", m.Status)
fmt.Fprintf(tw, "%d\t", m.Retries)
fmt.Fprintln(tw, formatters.FmtTime(m.CreatedAt))
}
err := tw.Flush()
if err != nil {
return err
}
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(check.Items)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(check.Items)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
return nil
},
}
fs := cmd.Flags()
fs.UintVarP(&last, "last", "l", 0, "Last `N` members. 0 means no limit")
fs.BoolVar(&showIDs, "show-ids", false, "Include member IDs in table output")
fs.StringVarP(&outputFormat, "output-format", "o", "table", "Output format. Allowed: table, json, yaml, go-template, go-template-file")
fs.StringVar(&goTemplate, "template", "", "Template string or path to use when -o=go-template, -o=go-template-file. The template format is golang templates\n[http://golang.org/pkg/text/template/#pkg-overview]")
fs.StringVar(&environment, "environment", "default", "Environment name")
_ = cmd.RegisterFlagCompletionFunc("output-format", formatters.CompleteOutputFormat)
return cmd
}