15
15
use Symfony \Component \Console \Input \InputInterface ;
16
16
use Symfony \Component \Console \Input \InputOption ;
17
17
use Symfony \Component \Console \Output \OutputInterface ;
18
+ use Symfony \Component \Console \Style \SymfonyStyle ;
18
19
use Symfony \Component \Finder \Finder ;
19
20
use Symfony \Component \Yaml \Exception \ParseException ;
20
21
use Symfony \Component \Yaml \Parser ;
@@ -62,8 +63,10 @@ protected function configure()
62
63
63
64
protected function execute (InputInterface $ input , OutputInterface $ output )
64
65
{
66
+ $ stdout = $ output ;
67
+ $ output = new SymfonyStyle ($ input , $ output );
65
68
if (false !== strpos ($ input ->getFirstArgument (), ':l ' )) {
66
- $ output ->writeln ( ' <comment> The use of "yaml:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:yaml" instead.</comment> ' );
69
+ $ output ->caution ( ' The use of "yaml:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:yaml" instead. ' );
67
70
}
68
71
69
72
$ filename = $ input ->getArgument ('filename ' );
@@ -78,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
78
81
$ content .= fread (STDIN , 1024 );
79
82
}
80
83
81
- return $ this ->display ($ input , $ output , array ($ this ->validate ($ content )));
84
+ return $ this ->display ($ input , $ stdout , $ output , array ($ this ->validate ($ content )));
82
85
}
83
86
84
87
if (0 !== strpos ($ filename , '@ ' ) && !is_readable ($ filename )) {
@@ -100,7 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
100
103
$ filesInfo [] = $ this ->validate (file_get_contents ($ file ), $ file );
101
104
}
102
105
103
- return $ this ->display ($ input , $ output , $ filesInfo );
106
+ return $ this ->display ($ input , $ stdout , $ output , $ filesInfo );
104
107
}
105
108
106
109
private function validate ($ content , $ file = null )
@@ -115,33 +118,37 @@ private function validate($content, $file = null)
115
118
return array ('file ' => $ file , 'valid ' => true );
116
119
}
117
120
118
- private function display (InputInterface $ input , OutputInterface $ output , $ files )
121
+ private function display (InputInterface $ input , OutputInterface $ stdout , $ output , $ files )
119
122
{
120
123
switch ($ input ->getOption ('format ' )) {
121
124
case 'txt ' :
122
- return $ this ->displayTxt ($ output , $ files );
125
+ return $ this ->displayTxt ($ stdout , $ output , $ files );
123
126
case 'json ' :
124
127
return $ this ->displayJson ($ output , $ files );
125
128
default :
126
129
throw new \InvalidArgumentException (sprintf ('The format "%s" is not supported. ' , $ input ->getOption ('format ' )));
127
130
}
128
131
}
129
132
130
- private function displayTxt (OutputInterface $ output , $ filesInfo )
133
+ private function displayTxt (OutputInterface $ stdout , $ output , $ filesInfo )
131
134
{
132
135
$ errors = 0 ;
133
136
134
137
foreach ($ filesInfo as $ info ) {
135
- if ($ info ['valid ' ] && $ output ->isVerbose ()) {
136
- $ output ->writeln ('<info>OK</info> ' .($ info ['file ' ] ? sprintf (' in %s ' , $ info ['file ' ]) : '' ));
138
+ if ($ info ['valid ' ] && $ stdout ->isVerbose ()) {
139
+ $ output ->comment ('<info>OK</info> ' .($ info ['file ' ] ? sprintf (' in %s ' , $ info ['file ' ]) : '' ));
137
140
} elseif (!$ info ['valid ' ]) {
138
141
++$ errors ;
139
- $ output ->writeln (sprintf ('<error>KO </error> in %s ' , $ info ['file ' ]));
140
- $ output ->writeln (sprintf ('<error>>> %s</error> ' , $ info ['message ' ]));
142
+ $ output ->text (sprintf ('<error> ERROR </error> in %s ' , $ info ['file ' ]));
143
+ $ output ->text (sprintf ('<error> >> %s</error> ' , $ info ['message ' ]));
141
144
}
142
145
}
143
146
144
- $ output ->writeln (sprintf ('<comment>%d/%d valid files</comment> ' , count ($ filesInfo ) - $ errors , count ($ filesInfo )));
147
+ if ($ errors === 0 ) {
148
+ $ output ->success (sprintf ('All %d YAML files contain valid syntax. ' , count ($ filesInfo )));
149
+ } else {
150
+ $ output ->warning (sprintf ('%d YAML files have valid syntax and %d contain errors. ' , count($ filesInfo ) - $ errors , $ errors ));
151
+ }
145
152
146
153
return min ($ errors , 1 );
147
154
}
0 commit comments