77using Serilog ;
88using Serilog . Events ;
99
10- namespace Octopus . CommandLine
10+ namespace Octopus . CommandLine ;
11+
12+ public class CommandOutputProvider : ICommandOutputProvider
1113{
12- public class CommandOutputProvider : ICommandOutputProvider
13- {
14- readonly ILogger logger ;
14+ readonly ILogger logger ;
1515
16- readonly string applicationName ;
17- readonly ICommandOutputJsonSerializer commandOutputJsonSerializer ;
16+ readonly string applicationName ;
17+ readonly ICommandOutputJsonSerializer commandOutputJsonSerializer ;
1818
19- public CommandOutputProvider ( string applicationName , string applicationVersion , ILogger logger )
20- : this ( applicationName , applicationVersion , new DefaultCommandOutputJsonSerializer ( ) , logger )
21- {
22- }
19+ public CommandOutputProvider ( string applicationName , string applicationVersion , ILogger logger )
20+ : this ( applicationName , applicationVersion , new DefaultCommandOutputJsonSerializer ( ) , logger )
21+ {
22+ }
2323
24- public CommandOutputProvider ( string applicationName , string applicationVersion , ICommandOutputJsonSerializer commandOutputJsonSerializer , ILogger logger )
25- {
26- this . applicationVersion = applicationVersion ;
27- this . applicationName = applicationName ;
28- this . commandOutputJsonSerializer = commandOutputJsonSerializer ;
29- this . logger = logger ;
30- PrintMessages = true ; // unless told otherwise
31- }
24+ public CommandOutputProvider ( string applicationName , string applicationVersion , ICommandOutputJsonSerializer commandOutputJsonSerializer , ILogger logger )
25+ {
26+ this . applicationVersion = applicationVersion ;
27+ this . applicationName = applicationName ;
28+ this . commandOutputJsonSerializer = commandOutputJsonSerializer ;
29+ this . logger = logger ;
30+ PrintMessages = true ; // unless told otherwise
31+ }
3232
33- public string applicationVersion { get ; }
33+ public string applicationVersion { get ; }
3434
35- public bool PrintMessages { get ; set ; }
35+ public bool PrintMessages { get ; set ; }
3636
37- public void PrintHeader ( )
37+ public void PrintHeader ( )
38+ {
39+ if ( PrintMessages )
3840 {
39- if ( PrintMessages )
40- {
41- logger . Information ( $ "{ applicationName } , version { applicationVersion } ") ;
42- logger . Information ( string . Empty ) ;
43- }
41+ logger . Information ( $ "{ applicationName } , version { applicationVersion } ") ;
42+ logger . Information ( string . Empty ) ;
4443 }
44+ }
4545
46- public void PrintCommandHelpHeader ( string executable , string commandName , string description , TextWriter textWriter )
47- {
48- if ( PrintMessages )
49- {
50- Console . ResetColor ( ) ;
51- textWriter . WriteLine ( description ) ;
52- textWriter . WriteLine ( ) ;
53- textWriter . Write ( "Usage: " ) ;
54- Console . ForegroundColor = ConsoleColor . White ;
55- textWriter . WriteLine ( $ "{ executable } { commandName } [<options>]") ;
56- Console . ResetColor ( ) ;
57- textWriter . WriteLine ( ) ;
58- textWriter . WriteLine ( "Where [<options>] is any of: " ) ;
59- textWriter . WriteLine ( ) ;
60- }
46+ public void PrintCommandHelpHeader ( string executable , string commandName , string description , TextWriter textWriter )
47+ {
48+ if ( PrintMessages )
49+ {
50+ Console . ResetColor ( ) ;
51+ textWriter . WriteLine ( description ) ;
52+ textWriter . WriteLine ( ) ;
53+ textWriter . Write ( "Usage: " ) ;
54+ Console . ForegroundColor = ConsoleColor . White ;
55+ textWriter . WriteLine ( $ "{ executable } { commandName } [<options>]") ;
56+ Console . ResetColor ( ) ;
57+ textWriter . WriteLine ( ) ;
58+ textWriter . WriteLine ( "Where [<options>] is any of: " ) ;
59+ textWriter . WriteLine ( ) ;
6160 }
61+ }
6262
63- public void PrintCommandOptions ( Options options , TextWriter writer )
64- {
65- if ( PrintMessages )
66- foreach ( var g in options . OptionSets . Keys . Reverse ( ) )
67- {
68- writer . WriteLine ( $ "{ g } : ") ;
69- writer . WriteLine ( ) ;
70- options . OptionSets [ g ] . WriteOptionDescriptions ( writer ) ;
71- writer . WriteLine ( ) ;
72- }
73- }
63+ public void PrintCommandOptions ( Options options , TextWriter writer )
64+ {
65+ if ( PrintMessages )
66+ foreach ( var g in options . OptionSets . Keys . Reverse ( ) )
67+ {
68+ writer . WriteLine ( $ "{ g } : ") ;
69+ writer . WriteLine ( ) ;
70+ options . OptionSets [ g ] . WriteOptionDescriptions ( writer ) ;
71+ writer . WriteLine ( ) ;
72+ }
73+ }
7474
75- public void Debug ( string template , string propertyValue )
76- {
77- if ( PrintMessages )
78- logger . Debug ( template , propertyValue ) ;
79- }
75+ public void Debug ( string template , string propertyValue )
76+ {
77+ if ( PrintMessages )
78+ logger . Debug ( template , propertyValue ) ;
79+ }
8080
81- public void Debug ( string template , params object [ ] propertyValues )
82- {
83- if ( PrintMessages )
84- logger . Debug ( template , propertyValues ) ;
85- }
81+ public void Debug ( string template , params object [ ] propertyValues )
82+ {
83+ if ( PrintMessages )
84+ logger . Debug ( template , propertyValues ) ;
85+ }
8686
87- public void Information ( string template , string propertyValue )
88- {
89- if ( PrintMessages )
90- logger . Information ( template , propertyValue ) ;
91- }
87+ public void Information ( string template , string propertyValue )
88+ {
89+ if ( PrintMessages )
90+ logger . Information ( template , propertyValue ) ;
91+ }
9292
93- public void Information ( string template , params object [ ] propertyValues )
94- {
95- if ( PrintMessages )
96- logger . Information ( template , propertyValues ) ;
97- }
93+ public void Information ( string template , params object [ ] propertyValues )
94+ {
95+ if ( PrintMessages )
96+ logger . Information ( template , propertyValues ) ;
97+ }
9898
99- public void Json ( object o )
100- {
101- logger . Information ( commandOutputJsonSerializer . SerializeObjectToJson ( o ) ) ;
102- }
99+ public void Json ( object o )
100+ {
101+ logger . Information ( commandOutputJsonSerializer . SerializeObjectToJson ( o ) ) ;
102+ }
103103
104- public void Json ( object o , TextWriter writer )
104+ public void Json ( object o , TextWriter writer )
105+ {
106+ var settings = new JsonSerializerSettings
105107 {
106- var settings = new JsonSerializerSettings
107- {
108- NullValueHandling = NullValueHandling . Ignore ,
109- Formatting = Formatting . Indented ,
110- Converters = new JsonConverterCollection { new StringEnumConverter ( ) }
111- } ;
112- writer . WriteLine ( JsonConvert . SerializeObject ( o , settings ) ) ;
113- }
108+ NullValueHandling = NullValueHandling . Ignore ,
109+ Formatting = Formatting . Indented ,
110+ Converters = new JsonConverterCollection { new StringEnumConverter ( ) }
111+ } ;
112+ writer . WriteLine ( JsonConvert . SerializeObject ( o , settings ) ) ;
113+ }
114114
115- public void Warning ( string s )
116- {
117- if ( PrintMessages )
118- logger . Warning ( s ) ;
119- }
115+ public void Warning ( string s )
116+ {
117+ if ( PrintMessages )
118+ logger . Warning ( s ) ;
119+ }
120120
121- public void Warning ( string template , params object [ ] propertyValues )
122- {
123- if ( PrintMessages )
124- logger . Warning ( template , propertyValues ) ;
125- }
121+ public void Warning ( string template , params object [ ] propertyValues )
122+ {
123+ if ( PrintMessages )
124+ logger . Warning ( template , propertyValues ) ;
125+ }
126126
127- public void Error ( string template , params object [ ] propertyValues )
128- {
129- if ( PrintMessages )
130- logger . Error ( template , propertyValues ) ;
131- }
127+ public void Error ( string template , params object [ ] propertyValues )
128+ {
129+ if ( PrintMessages )
130+ logger . Error ( template , propertyValues ) ;
131+ }
132132
133- public void Write ( LogEventLevel logEventLevel , string messageTemplate , params object [ ] propertyValues )
134- {
135- if ( PrintMessages )
136- logger . Write ( logEventLevel , messageTemplate , propertyValues ) ;
137- }
133+ public void Write ( LogEventLevel logEventLevel , string messageTemplate , params object [ ] propertyValues )
134+ {
135+ if ( PrintMessages )
136+ logger . Write ( logEventLevel , messageTemplate , propertyValues ) ;
137+ }
138138
139- public void Error ( Exception ex , string messageTemplate )
140- {
141- if ( PrintMessages )
142- logger . Error ( ex , messageTemplate ) ;
143- }
139+ public void Error ( Exception ex , string messageTemplate )
140+ {
141+ if ( PrintMessages )
142+ logger . Error ( ex , messageTemplate ) ;
144143 }
145144}
0 commit comments