Skip to content

Commit eec7eeb

Browse files
committed
cleanup and rename console-output to console-format to be consistent with other flag names
1 parent 361325f commit eec7eeb

File tree

30 files changed

+126
-131
lines changed

30 files changed

+126
-131
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ Global options:
316316
- `--verbose` - enable info logs
317317
- `--log-level` - set the logging level ('debug', 'info', 'warn' (default), 'error', 'fatal', 'panic')
318318
- `--log-format` - set the format used by logs ('text' (default), or 'json')
319-
- `--console-output` - set the console output format to use ('text' (default), or 'json')
319+
- `--console-format` - set the console output format to use ('text' (default), or 'json')
320320
- `--log` - log file to store logs
321321
- `--host` - Docker host address or socket (prefix with `tcp://` or `unix://`)
322322
- `--tls` - use TLS connecting to Docker

pkg/app/execontext.go

Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
"github.com/docker-slim/docker-slim/pkg/util/errutil"
1414
)
1515

16+
const (
17+
cfJSON = "json"
18+
cfText = "text"
19+
)
20+
1621
type ExecutionContext struct {
1722
Out *Output
1823
cleanupHandlers []func()
@@ -86,7 +91,7 @@ type OutVars map[string]interface{}
8691

8792
func (ref *Output) LogDump(logType, data string, params ...OutVars) {
8893
var info string
89-
msg := make(map[string]string)
94+
msg := map[string]string{}
9095
var jsonData []byte
9196

9297
msg["cmd"] = ref.CmdName
@@ -109,10 +114,10 @@ func (ref *Output) LogDump(logType, data string, params ...OutVars) {
109114
}
110115
}
111116
switch ref.JSONFlag {
112-
case "json":
117+
case cfJSON:
113118
jsonData, _ = json.Marshal(msg)
114119
fmt.Println(string(jsonData))
115-
case "text":
120+
case cfText:
116121
fmt.Printf("cmd=%s log='%s' event=LOG.START %s ====================\n", ref.CmdName, logType, info)
117122
fmt.Println(data)
118123
fmt.Printf("cmd=%s log='%s' event=LOG.END %s ====================\n", ref.CmdName, logType, info)
@@ -123,11 +128,8 @@ func (ref *Output) LogDump(logType, data string, params ...OutVars) {
123128
}
124129

125130
func (ref *Output) Prompt(data string) {
126-
color.Set(color.FgHiRed)
127-
defer color.Unset()
128-
129131
switch ref.JSONFlag {
130-
case "json":
132+
case cfJSON:
131133
//marshal data to json
132134
var jsonData []byte
133135
if len(data) > 0 {
@@ -138,7 +140,10 @@ func (ref *Output) Prompt(data string) {
138140
jsonData, _ = json.Marshal(msg)
139141
fmt.Println(string(jsonData))
140142
}
141-
case "text":
143+
case cfText:
144+
color.Set(color.FgHiRed)
145+
defer color.Unset()
146+
142147
fmt.Printf("cmd=%s prompt='%s'\n", ref.CmdName, data)
143148
default:
144149
log.Fatalf("Unknown console output flag: %s\n. It should be either 'text' or 'json", ref.JSONFlag)
@@ -147,11 +152,8 @@ func (ref *Output) Prompt(data string) {
147152
}
148153

149154
func (ref *Output) Error(errType string, data string) {
150-
color.Set(color.FgHiRed)
151-
defer color.Unset()
152-
153155
switch ref.JSONFlag {
154-
case "json":
156+
case cfJSON:
155157
//marshal data to json
156158
var jsonData []byte
157159
if len(data) > 0 {
@@ -163,7 +165,10 @@ func (ref *Output) Error(errType string, data string) {
163165
jsonData, _ = json.Marshal(msg)
164166
fmt.Println(string(jsonData))
165167
}
166-
case "text":
168+
case cfText:
169+
color.Set(color.FgHiRed)
170+
defer color.Unset()
171+
167172
fmt.Printf("cmd=%s error=%s message='%s'\n", ref.CmdName, errType, data)
168173
default:
169174
log.Fatalf("Unknown console output flag: %s\n. It should be either 'text' or 'json", ref.JSONFlag)
@@ -172,11 +177,8 @@ func (ref *Output) Error(errType string, data string) {
172177
}
173178

174179
func (ref *Output) Message(data string) {
175-
color.Set(color.FgHiMagenta)
176-
defer color.Unset()
177-
178180
switch ref.JSONFlag {
179-
case "json":
181+
case cfJSON:
180182
//marshal data to json
181183
var jsonData []byte
182184
if len(data) > 0 {
@@ -187,7 +189,10 @@ func (ref *Output) Message(data string) {
187189
jsonData, _ = json.Marshal(msg)
188190
fmt.Println(string(jsonData))
189191
}
190-
case "text":
192+
case cfText:
193+
color.Set(color.FgHiMagenta)
194+
defer color.Unset()
195+
191196
fmt.Printf("cmd=%s message='%s'\n", ref.CmdName, data)
192197
default:
193198
log.Fatalf("Unknown console output flag: %s\n. It should be either 'text' or 'json", ref.JSONFlag)
@@ -199,7 +204,7 @@ func (ref *Output) State(state string, params ...OutVars) {
199204
var exitInfo string
200205
var info string
201206
var sep string
202-
msg := make(map[string]string)
207+
msg := map[string]string{}
203208
var jsonData []byte
204209
msg["cmd"] = ref.CmdName
205210
msg["state"] = state
@@ -237,18 +242,18 @@ func (ref *Output) State(state string, params ...OutVars) {
237242
}
238243
}
239244

240-
if state == "exited" || strings.Contains(state, "error") {
241-
color.Set(color.FgHiRed, color.Bold)
242-
} else {
243-
color.Set(color.FgCyan, color.Bold)
244-
}
245-
defer color.Unset()
246-
247245
switch ref.JSONFlag {
248-
case "json":
246+
case cfJSON:
249247
jsonData, _ = json.Marshal(msg)
250248
fmt.Println(string(jsonData))
251-
case "text":
249+
case cfText:
250+
if state == "exited" || strings.Contains(state, "error") {
251+
color.Set(color.FgHiRed, color.Bold)
252+
} else {
253+
color.Set(color.FgCyan, color.Bold)
254+
}
255+
defer color.Unset()
256+
252257
fmt.Printf("cmd=%s state=%s%s%s%s\n", ref.CmdName, state, exitInfo, sep, info)
253258

254259
default:
@@ -265,7 +270,7 @@ var (
265270
func (ref *Output) Info(infoType string, params ...OutVars) {
266271
var data string
267272
var sep string
268-
msg := make(map[string]string)
273+
msg := map[string]string{}
269274
var jsonData []byte
270275
msg["cmd"] = ref.CmdName
271276
msg["info"] = infoType
@@ -289,10 +294,10 @@ func (ref *Output) Info(infoType string, params ...OutVars) {
289294
}
290295

291296
switch ref.JSONFlag {
292-
case "json":
297+
case cfJSON:
293298
jsonData, _ = json.Marshal(msg)
294299
fmt.Println(string(jsonData))
295-
case "text":
300+
case cfText:
296301
fmt.Printf("cmd=%s info=%s%s%s\n", ref.CmdName, itcolor(infoType), sep, data)
297302

298303
default:
@@ -301,52 +306,41 @@ func (ref *Output) Info(infoType string, params ...OutVars) {
301306

302307
}
303308

304-
func ShowCommunityInfo(jsonFlag string) {
305-
306-
type Data struct {
309+
func ShowCommunityInfo(consoleFormat string) {
310+
lines := []struct {
307311
App string `json:"app"`
308312
Message string `json:"message"`
309313
Info string `json:"info"`
314+
}{
315+
{
316+
App: consts.AppName,
317+
Message: "Join the Gitter channel to ask questions or to share your feedback",
318+
Info: consts.CommunityGitter,
319+
},
320+
{
321+
App: consts.AppName,
322+
Message: "Join the Discord server to ask questions or to share your feedback",
323+
Info: consts.CommunityDiscord,
324+
},
325+
{
326+
App: consts.AppName,
327+
Message: "GitHub Discussions",
328+
Info: consts.CommunityDiscussions,
329+
},
310330
}
311331

312-
type CommunityInfo struct {
313-
Data []Data `json:"data"`
314-
}
315-
316-
var data Data
317-
var community CommunityInfo
318-
319-
color.Set(color.FgHiMagenta)
320-
defer color.Unset()
321-
322-
data.App = "docker-slim"
323-
data.Message = "Join the Gitter channel to ask questions or to share your feedback"
324-
data.Info = consts.CommunityGitter
325-
326-
community.Data = append(community.Data, data)
327-
328-
data.App = "docker-slim"
329-
data.Message = "Join the Discord server to ask questions or to share your feedback"
330-
data.Info = consts.CommunityDiscord
331-
332-
community.Data = append(community.Data, data)
333-
334-
data.App = "docker-slim"
335-
data.Message = "GitHub Discussions"
336-
data.Info = consts.CommunityDiscussions
337-
338-
community.Data = append(community.Data, data)
339-
340-
switch jsonFlag {
341-
case "json":
342-
var jsonData []byte
343-
jsonData, _ = json.Marshal(community.Data)
344-
fmt.Println(string(jsonData))
345-
case "text":
346-
for _, v := range community.Data {
347-
fmt.Printf("'app':'%s' 'message':'%s' 'info':'%s'\n", v.App, v.Message, v.Info)
332+
switch consoleFormat {
333+
case cfJSON:
334+
for _, v := range lines {
335+
jsonData, _ := json.Marshal(v)
336+
fmt.Println(string(jsonData))
348337
}
349338
default:
350-
log.Fatalf("Unknown console output flag: %s\n. It should be either 'text' or 'json", jsonFlag)
339+
color.Set(color.FgHiMagenta)
340+
defer color.Unset()
341+
342+
for _, v := range lines {
343+
fmt.Printf("app='%s' message='%s' info='%s'\n", v.App, v.Message, v.Info)
344+
}
351345
}
352346
}

pkg/app/master/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func newCLI() *cli.App {
157157
cliApp.After = func(ctx *cli.Context) error {
158158
//tmp hack
159159
if !strings.Contains(strings.Join(os.Args, " "), " docker-cli-plugin-metadata") {
160-
app.ShowCommunityInfo(ctx.String(commands.FlagConsoleOutput))
160+
app.ShowCommunityInfo(ctx.String(commands.FlagConsoleFormat))
161161
}
162162
return nil
163163
}

pkg/app/master/commands/build/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ var CLI = &cli.Command{
167167
commands.Cflag(commands.FlagSensorIPCMode),
168168
},
169169
Action: func(ctx *cli.Context) error {
170-
xc := app.NewExecutionContext(Name, ctx.String(commands.FlagConsoleOutput))
170+
xc := app.NewExecutionContext(Name, ctx.String(commands.FlagConsoleFormat))
171171

172172
cbOpts, err := GetContainerBuildOptions(ctx)
173173
if err != nil {

pkg/app/master/commands/build/prompt.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,25 @@ var CommandFlagSuggestions = &commands.FlagSuggestions{
181181
commands.FullFlagName(FlagIncludeCertPKDirs): commands.CompleteBool,
182182
commands.FullFlagName(FlagIncludeNew): commands.CompleteBool,
183183
commands.FullFlagName(commands.FlagContinueAfter): commands.CompleteContinueAfter,
184-
commands.FullFlagName(commands.FlagConsoleOutput): commands.CompleteConsoleOutput,
185-
commands.FullFlagName(commands.FlagUseLocalMounts): commands.CompleteBool,
186-
commands.FullFlagName(commands.FlagUseSensorVolume): commands.CompleteVolume,
187-
commands.FullFlagName(FlagKeepTmpArtifacts): commands.CompleteBool,
188-
commands.FullFlagName(FlagIncludeAppNuxtDir): commands.CompleteBool,
189-
commands.FullFlagName(FlagIncludeAppNuxtBuildDir): commands.CompleteBool,
190-
commands.FullFlagName(FlagIncludeAppNuxtDistDir): commands.CompleteBool,
191-
commands.FullFlagName(FlagIncludeAppNuxtStaticDir): commands.CompleteBool,
192-
commands.FullFlagName(FlagIncludeAppNuxtNodeModulesDir): commands.CompleteBool,
193-
commands.FullFlagName(FlagIncludeAppNextDir): commands.CompleteBool,
194-
commands.FullFlagName(FlagIncludeAppNextBuildDir): commands.CompleteBool,
195-
commands.FullFlagName(FlagIncludeAppNextDistDir): commands.CompleteBool,
196-
commands.FullFlagName(FlagIncludeAppNextStaticDir): commands.CompleteBool,
197-
commands.FullFlagName(FlagIncludeAppNextNodeModulesDir): commands.CompleteBool,
198-
commands.FullFlagName(commands.FlagCROHostConfigFile): commands.CompleteFile,
199-
commands.FullFlagName(FlagDockerfileContext): commands.CompleteFile,
200-
commands.FullFlagName(FlagDeleteFatImage): commands.CompleteBool,
201-
commands.FullFlagName(commands.FlagRTAOnbuildBaseImage): commands.CompleteBool,
202-
commands.FullFlagName(commands.FlagRTASourcePT): commands.CompleteBool,
203-
commands.FullFlagName(commands.FlagSensorIPCMode): commands.CompleteIPCMode,
184+
//commands.FullFlagName(commands.FlagConsoleFormat): commands.CompleteConsoleOutput,
185+
commands.FullFlagName(commands.FlagUseLocalMounts): commands.CompleteBool,
186+
commands.FullFlagName(commands.FlagUseSensorVolume): commands.CompleteVolume,
187+
commands.FullFlagName(FlagKeepTmpArtifacts): commands.CompleteBool,
188+
commands.FullFlagName(FlagIncludeAppNuxtDir): commands.CompleteBool,
189+
commands.FullFlagName(FlagIncludeAppNuxtBuildDir): commands.CompleteBool,
190+
commands.FullFlagName(FlagIncludeAppNuxtDistDir): commands.CompleteBool,
191+
commands.FullFlagName(FlagIncludeAppNuxtStaticDir): commands.CompleteBool,
192+
commands.FullFlagName(FlagIncludeAppNuxtNodeModulesDir): commands.CompleteBool,
193+
commands.FullFlagName(FlagIncludeAppNextDir): commands.CompleteBool,
194+
commands.FullFlagName(FlagIncludeAppNextBuildDir): commands.CompleteBool,
195+
commands.FullFlagName(FlagIncludeAppNextDistDir): commands.CompleteBool,
196+
commands.FullFlagName(FlagIncludeAppNextStaticDir): commands.CompleteBool,
197+
commands.FullFlagName(FlagIncludeAppNextNodeModulesDir): commands.CompleteBool,
198+
commands.FullFlagName(commands.FlagCROHostConfigFile): commands.CompleteFile,
199+
commands.FullFlagName(FlagDockerfileContext): commands.CompleteFile,
200+
commands.FullFlagName(FlagDeleteFatImage): commands.CompleteBool,
201+
commands.FullFlagName(commands.FlagRTAOnbuildBaseImage): commands.CompleteBool,
202+
commands.FullFlagName(commands.FlagRTASourcePT): commands.CompleteBool,
203+
commands.FullFlagName(commands.FlagSensorIPCMode): commands.CompleteIPCMode,
204204
},
205205
}

pkg/app/master/commands/cliflags.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626
FlagInContainer = "in-container"
2727
FlagArchiveState = "archive-state"
2828
FlagNoColor = "no-color"
29-
FlagConsoleOutput = "console-output"
29+
FlagConsoleFormat = "console-format"
3030
)
3131

3232
// Global flag usage info
@@ -38,7 +38,7 @@ const (
3838
FlagLogLevelUsage = "set the logging level ('trace', 'debug', 'info', 'warn' (default), 'error', 'fatal', 'panic')"
3939
FlagLogUsage = "log file to store logs"
4040
FlagLogFormatUsage = "set the format used by logs ('text' (default), or 'json')"
41-
FlagConsoleOutputUsage = "set the format used by logs ('text' (default), or 'json')"
41+
FlagConsoleFormatUsage = "set the console output format to use ('text' (default), or 'json')"
4242
FlagUseTLSUsage = "use TLS"
4343
FlagVerifyTLSUsage = "verify TLS"
4444
FlagTLSCertPathUsage = "path to TLS cert files"
@@ -292,9 +292,9 @@ func GlobalFlags() []cli.Flag {
292292
Usage: "set the format used by logs ('text' (default), or 'json')",
293293
},
294294
&cli.StringFlag{
295-
Name: FlagConsoleOutput,
295+
Name: FlagConsoleFormat,
296296
Value: "text",
297-
Usage: "set the console output format to use ('text' (default), or 'json')",
297+
Usage: FlagConsoleFormatUsage,
298298
},
299299
&cli.BoolFlag{
300300
Name: FlagUseTLS,

pkg/app/master/commands/clifvgetter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func GlobalFlagValues(ctx *cli.Context) (*GenericParams, error) {
277277
Verbose: ctx.Bool(FlagVerbose),
278278
LogLevel: ctx.String(FlagLogLevel),
279279
LogFormat: ctx.String(FlagLogFormat),
280-
ConsoleOutput: ctx.String(FlagConsoleOutput),
280+
ConsoleOutput: ctx.String(FlagConsoleFormat),
281281
Log: ctx.String(FlagLog),
282282
StatePath: ctx.String(FlagStatePath),
283283
ReportLocation: ctx.String(FlagCommandReport),

pkg/app/master/commands/cliprompt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (ia *InteractiveApp) execute(command string) {
7979
}
8080

8181
if parts[0] == "exit" {
82-
app.ShowCommunityInfo(FlagConsoleOutput)
82+
app.ShowCommunityInfo(FlagConsoleFormat)
8383
os.Exit(0)
8484
}
8585

@@ -210,7 +210,7 @@ var GlobalFlagSuggestions = []prompt.Suggest{
210210
{Text: FullFlagName(FlagLogLevel), Description: FlagLogLevelUsage},
211211
{Text: FullFlagName(FlagLog), Description: FlagLogUsage},
212212
{Text: FullFlagName(FlagLogFormat), Description: FlagLogFormatUsage},
213-
{Text: FullFlagName(FlagConsoleOutput), Description: FlagConsoleOutputUsage},
213+
{Text: FullFlagName(FlagConsoleFormat), Description: FlagConsoleFormatUsage},
214214
{Text: FullFlagName(FlagUseTLS), Description: FlagUseTLSUsage},
215215
{Text: FullFlagName(FlagVerifyTLS), Description: FlagVerifyTLSUsage},
216216
{Text: FullFlagName(FlagTLSCertPath), Description: FlagTLSCertPathUsage},

pkg/app/master/commands/containerize/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var CLI = &cli.Command{
3333

3434
targetRef := ctx.Args().First()
3535

36-
xc := app.NewExecutionContext(Name, ctx.String(commands.FlagConsoleOutput))
36+
xc := app.NewExecutionContext(Name, ctx.String(commands.FlagConsoleFormat))
3737

3838
OnCommand(
3939
xc,

pkg/app/master/commands/convert/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var CLI = &cli.Command{
3333

3434
targetRef := ctx.Args().First()
3535

36-
xc := app.NewExecutionContext(Name, ctx.String(commands.FlagConsoleOutput))
36+
xc := app.NewExecutionContext(Name, ctx.String(commands.FlagConsoleFormat))
3737

3838
OnCommand(
3939
xc,

0 commit comments

Comments
 (0)