/
logging_cache.go
175 lines (164 loc) · 5.22 KB
/
logging_cache.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cli
import (
"context"
"github.com/deciduosity/jasper/remote"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
// Constants representing the jasper.LoggingCache interface as CLI commands.
const (
LoggingCacheCommand = "logging-cache"
LoggingCacheCreateCommand = "create"
LoggingCacheGetCommand = "get"
LoggingCacheRemoveCommand = "remove"
LoggingCacheCloseAndRemoveCommand = "close-and-remove"
LoggingCacheClearCommand = "clear"
LoggingCachePruneCommand = "prune"
LoggingCacheLenCommand = "len"
)
// LoggingCache creates a cli.Command that supports the jasper.LoggingCache
// interface. (jasper.LoggingCache).Put is not supported as there is no CLI
// equivalent.
func LoggingCache() cli.Command {
return cli.Command{
Name: LoggingCacheCommand,
Subcommands: []cli.Command{
loggingCacheCreate(),
loggingCacheGet(),
loggingCacheRemove(),
loggingCacheCloseAndRemove(),
loggingCacheClear(),
loggingCachePrune(),
loggingCacheLen(),
},
}
}
func loggingCacheCreate() cli.Command {
return cli.Command{
Name: LoggingCacheCreateCommand,
Flags: clientFlags(),
Before: clientBefore(),
Action: func(c *cli.Context) error {
input := LoggingCacheCreateInput{}
return doPassthroughInputOutput(c, &input, func(ctx context.Context, client remote.Manager) interface{} {
lc := client.LoggingCache(ctx)
if lc == nil {
return &CachedLoggerResponse{OutcomeResponse: *makeOutcomeResponse(errors.New("logging cache not supported"))}
}
logger, err := lc.Create(input.ID, &input.Output)
if err != nil {
return &CachedLoggerResponse{OutcomeResponse: *makeOutcomeResponse(err)}
}
return &CachedLoggerResponse{Logger: *logger, OutcomeResponse: *makeOutcomeResponse(nil)}
})
},
}
}
func loggingCacheGet() cli.Command {
return cli.Command{
Name: LoggingCacheGetCommand,
Flags: clientFlags(),
Before: clientBefore(),
Action: func(c *cli.Context) error {
input := IDInput{}
return doPassthroughInputOutput(c, &input, func(ctx context.Context, client remote.Manager) interface{} {
lc := client.LoggingCache(ctx)
if lc == nil {
return &CachedLoggerResponse{OutcomeResponse: *makeOutcomeResponse(errors.New("logging cache not supported"))}
}
logger := lc.Get(input.ID)
if logger == nil {
return &CachedLoggerResponse{OutcomeResponse: *makeOutcomeResponse(errors.Errorf("logger with id '%s' not found", input.ID))}
}
return &CachedLoggerResponse{Logger: *logger, OutcomeResponse: *makeOutcomeResponse(nil)}
})
},
}
}
func loggingCacheRemove() cli.Command {
return cli.Command{
Name: LoggingCacheRemoveCommand,
Flags: clientFlags(),
Before: clientBefore(),
Action: func(c *cli.Context) error {
input := IDInput{}
return doPassthroughInputOutput(c, &input, func(ctx context.Context, client remote.Manager) interface{} {
lc := client.LoggingCache(ctx)
if lc == nil {
return makeOutcomeResponse(errors.New("logging cache not supported"))
}
lc.Remove(input.ID)
return makeOutcomeResponse(nil)
})
},
}
}
func loggingCacheCloseAndRemove() cli.Command {
return cli.Command{
Name: LoggingCacheCloseAndRemoveCommand,
Flags: clientFlags(),
Before: clientBefore(),
Action: func(c *cli.Context) error {
input := IDInput{}
return doPassthroughInputOutput(c, &input, func(ctx context.Context, client remote.Manager) interface{} {
lc := client.LoggingCache(ctx)
if lc == nil {
return makeOutcomeResponse(errors.New("logging cache not supported"))
}
err := lc.CloseAndRemove(ctx, input.ID)
return makeOutcomeResponse(err)
})
},
}
}
func loggingCacheClear() cli.Command {
return cli.Command{
Name: LoggingCacheClearCommand,
Flags: clientFlags(),
Before: clientBefore(),
Action: func(c *cli.Context) error {
return doPassthroughOutput(c, func(ctx context.Context, client remote.Manager) interface{} {
lc := client.LoggingCache(ctx)
if lc == nil {
return makeOutcomeResponse(errors.New("logging cache not supported"))
}
err := lc.Clear(ctx)
return makeOutcomeResponse(err)
})
},
}
}
func loggingCachePrune() cli.Command {
return cli.Command{
Name: LoggingCachePruneCommand,
Flags: clientFlags(),
Before: clientBefore(),
Action: func(c *cli.Context) error {
input := LoggingCachePruneInput{}
return doPassthroughInputOutput(c, &input, func(ctx context.Context, client remote.Manager) interface{} {
lc := client.LoggingCache(ctx)
if lc == nil {
return makeOutcomeResponse(errors.New("logging cache not supported"))
}
lc.Prune(input.LastAccessed)
return makeOutcomeResponse(nil)
})
},
}
}
func loggingCacheLen() cli.Command {
return cli.Command{
Name: LoggingCacheLenCommand,
Flags: clientFlags(),
Before: clientBefore(),
Action: func(c *cli.Context) error {
return doPassthroughOutput(c, func(ctx context.Context, client remote.Manager) interface{} {
lc := client.LoggingCache(ctx)
if lc == nil {
return &LoggingCacheLenResponse{OutcomeResponse: *makeOutcomeResponse(errors.New("logging cache not supported"))}
}
return &LoggingCacheLenResponse{Length: lc.Len(), OutcomeResponse: *makeOutcomeResponse(nil)}
})
},
}
}