-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.go
More file actions
168 lines (142 loc) · 4.19 KB
/
logs.go
File metadata and controls
168 lines (142 loc) · 4.19 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"github.com/klev-dev/klev-api-go"
"github.com/spf13/cobra"
)
func logsRoot() *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Short: "interact with logs",
}
cmd.AddCommand(logsList())
cmd.AddCommand(logsCreate())
cmd.AddCommand(logsGet())
cmd.AddCommand(logsStats())
cmd.AddCommand(logsUpdate())
cmd.AddCommand(logsDelete())
return cmd
}
func logsList() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list logs",
Args: cobra.NoArgs,
}
metadata := cmd.Flags().String("metadata", "", "log metadata")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("metadata") {
out, err := klient.Logs.Find(cmd.Context(), *metadata)
return output(out, err)
} else {
out, err := klient.Logs.List(cmd.Context())
return output(out, err)
}
}
return cmd
}
func logsCreate() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "create new log",
Args: cobra.NoArgs,
}
var in klev.LogCreateParams
cmd.Flags().StringVar(&in.Metadata, "metadata", "", "machine readable metadata")
cmd.Flags().BoolVar(&in.Compacting, "compacting", false, "if the log is compacting")
cmd.Flags().Int64Var(&in.TrimSeconds, "trim-seconds", 0, "age of the log to trim")
cmd.Flags().Int64Var(&in.TrimSize, "trim-size", 0, "size of the log to trim")
cmd.Flags().Int64Var(&in.TrimCount, "trim-count", 0, "count of message in the log to trim")
cmd.Flags().Int64Var(&in.CompactSeconds, "compact-seconds", 0, "age of the log to compact")
cmd.Flags().Int64Var(&in.ExpireSeconds, "expire-seconds", 0, "age of the log to expire")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
out, err := klient.Logs.Create(cmd.Context(), in)
return output(out, err)
}
return cmd
}
func logsGet() *cobra.Command {
return &cobra.Command{
Use: "get <log-id>",
Short: "get a log",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := klev.ParseLogID(args[0])
if err != nil {
return outputErr(err)
}
out, err := klient.Logs.Get(cmd.Context(), id)
return output(out, err)
},
}
}
func logsStats() *cobra.Command {
return &cobra.Command{
Use: "stats <log-id>",
Short: "stats a log",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := klev.ParseLogID(args[0])
if err != nil {
return outputErr(err)
}
out, err := klient.Logs.Stats(cmd.Context(), id)
return output(out, err)
},
}
}
func logsUpdate() *cobra.Command {
cmd := &cobra.Command{
Use: "update <log-id>",
Short: "update log",
Args: cobra.ExactArgs(1),
}
metadata := cmd.Flags().String("metadata", "", "machine readable metadata")
trimSeconds := cmd.Flags().Int64("trim-seconds", 0, "age of the log to trim")
trimSize := cmd.Flags().Int64("trim-size", 0, "size of the log to trim")
trimCount := cmd.Flags().Int64("trim-count", 0, "count of message in the log to trim")
compactSeconds := cmd.Flags().Int64("compact-seconds", 0, "age of the log to compact")
expireSeconds := cmd.Flags().Int64("expire-seconds", 0, "age of the log to expire")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
id, err := klev.ParseLogID(args[0])
if err != nil {
return outputErr(err)
}
var in klev.LogUpdateParams
if cmd.Flags().Changed("metadata") {
in.Metadata = metadata
}
if cmd.Flags().Changed("trim-seconds") {
in.TrimSeconds = trimSeconds
}
if cmd.Flags().Changed("trim-size") {
in.TrimSize = trimSize
}
if cmd.Flags().Changed("trim-count") {
in.TrimCount = trimCount
}
if cmd.Flags().Changed("compact-seconds") {
in.CompactSeconds = compactSeconds
}
if cmd.Flags().Changed("expire-seconds") {
in.ExpireSeconds = expireSeconds
}
out, err := klient.Logs.UpdateRaw(cmd.Context(), id, in)
return output(out, err)
}
return cmd
}
func logsDelete() *cobra.Command {
return &cobra.Command{
Use: "delete <log-id>",
Short: "delete a log",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := klev.ParseLogID(args[0])
if err != nil {
return outputErr(err)
}
out, err := klient.Logs.Delete(cmd.Context(), id)
return output(out, err)
},
}
}