Coverage for gelee/cli.py: 86.79%

39 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 18:20:23 +00:00

1#! /usr/bin/env python 

2# -*- coding: utf-8 -*- 

3# pylint: disable=line-too-long 

4"""Validate configuration files against their format and maybe schema.""" 

5import os 

6import pathlib 

7import sys 

8import typing 

9from typing import List 

10 

11import typer 

12 

13import gelee 

14import gelee.gelee as lint 

15 

16DEBUG_VAR = 'GELEE_DEBUG' 

17DEBUG = bool(os.getenv(DEBUG_VAR)) 

18 

19ABORT_VAR = 'GELEE_ABORT' 

20ABORT = bool(os.getenv(ABORT_VAR)) 

21 

22APP_NAME = 'Gelee - a finer confiture.' 

23APP_ALIAS = 'gelee' 

24app = typer.Typer( 

25 add_completion=False, 

26 context_settings={'help_option_names': ['-h', '--help']}, 

27 no_args_is_help=True, 

28) 

29 

30 

31@app.callback(invoke_without_command=True) 

32def callback( 

33 version: bool = typer.Option( 

34 False, 

35 '-V', 

36 '--version', 

37 help='Display the gelee version and exit', 

38 is_eager=True, 

39 ) 

40) -> None: 

41 """ 

42 Gelee - a finer confiture. 

43 

44 Validate configuration files against their format and maybe schema. 

45 """ 

46 if version: 

47 typer.echo(f'{APP_NAME} version {gelee.__version__}') 

48 raise typer.Exit() 

49 

50 

51@app.command('cook') 

52def cook( 

53 unique_trees: List[pathlib.Path], 

54 abort: bool = typer.Option( 

55 False, 

56 '-a', 

57 '--abort', 

58 help='Flag to abort execution on first fail (default is False)', 

59 metavar='bool', 

60 ), 

61 debug: bool = typer.Option( 

62 False, 

63 '-d', 

64 '--debug', 

65 help='Flag to debug execution by adding more context info (default is False)', 

66 metavar='bool', 

67 ), 

68) -> int: 

69 """ 

70 Cook the gelee from the files in the tree below path. 

71 """ 

72 return sys.exit(main(unique_trees, abort=abort, debug=debug)) # type: ignore 

73 

74 

75@app.command('version') 

76def app_version() -> None: 

77 """ 

78 Display the gelee version and exit 

79 """ 

80 callback(True) 

81 

82 

83# pylint: disable=expression-not-assigned 

84# @app.command() 

85@typing.no_type_check 

86def main(argv=None, abort=None, debug=None): 

87 """Dispatch processing of the job. 

88 This is the strings only command line interface. 

89 For python API use interact with lint functions directly. 

90 """ 

91 argv = sys.argv[1:] if argv is None else argv 

92 debug = debug if debug else DEBUG 

93 abort = abort if abort else ABORT 

94 unique_trees = {arg: None for arg in argv} 

95 for tree_or_leaf in unique_trees: 

96 if not pathlib.Path(tree_or_leaf).is_file() and not pathlib.Path(tree_or_leaf).is_dir(): 

97 print('ERROR: For now only existing paths accepted.') 

98 sys.exit(2) 

99 

100 code, _ = lint.main(unique_trees, abort=abort, debug=debug) 

101 return code