@@ -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+
1621type ExecutionContext struct {
1722 Out * Output
1823 cleanupHandlers []func ()
@@ -86,7 +91,7 @@ type OutVars map[string]interface{}
8691
8792func (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
125130func (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
149154func (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
174179func (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 (
265270func (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}
0 commit comments