Introduce the 'cli' module and call it from the script
This commit is contained in:
62
bin/yamllint
62
bin/yamllint
@@ -15,68 +15,10 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from __future__ import print_function
|
||||
import os.path
|
||||
import sys
|
||||
|
||||
import argparse
|
||||
|
||||
from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION
|
||||
from yamllint import config
|
||||
from yamllint.errors import YamlLintConfigError
|
||||
from yamllint import lint
|
||||
from yamllint import output
|
||||
from yamllint import cli
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(prog=APP_NAME,
|
||||
description=APP_DESCRIPTION)
|
||||
parser.add_argument('files', metavar='FILES', nargs='+',
|
||||
help='files to check')
|
||||
parser.add_argument('-c', '--config', dest='config_file', action='store',
|
||||
help='path to a custom configuration')
|
||||
parser.add_argument('-f', '--format',
|
||||
choices=('parsable', 'standard'), default='standard',
|
||||
help='format for parsing output')
|
||||
parser.add_argument('-v', '--version', action='version',
|
||||
version='%s %s' % (APP_NAME, APP_VERSION))
|
||||
|
||||
# TODO: read from stdin when no filename?
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
if args.config_file is not None:
|
||||
conf = config.parse_config_from_file(args.config_file)
|
||||
elif os.path.isfile('.yamllint'):
|
||||
conf = config.parse_config_from_file('.yamllint')
|
||||
else:
|
||||
conf = config.parse_config('extends: default')
|
||||
except YamlLintConfigError as e:
|
||||
print(e, file=sys.stderr)
|
||||
sys.exit(-1)
|
||||
|
||||
return_code = 0
|
||||
|
||||
for file in args.files:
|
||||
if args.format != 'parsable':
|
||||
print('\033[4m%s\033[0m' % file)
|
||||
|
||||
try:
|
||||
with open(file) as f:
|
||||
for problem in lint(f, conf):
|
||||
if args.format == 'parsable':
|
||||
print(output.parsable_format(problem, file))
|
||||
else:
|
||||
print(output.standard_format(problem, file))
|
||||
|
||||
if return_code == 0 and problem.level == 'error':
|
||||
return_code = 1
|
||||
except EnvironmentError as e:
|
||||
print(e)
|
||||
return_code = -1
|
||||
|
||||
if args.format != 'parsable':
|
||||
print('')
|
||||
|
||||
sys.exit(return_code)
|
||||
cli.run(sys.argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user