Coverage for foran/cli.py: 100.00%

46 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2023-01-02 19:51 +0100

1"""Commandline API gateway for foran.""" 

2import pathlib 

3import sys 

4from typing import List, Union 

5 

6import typer 

7 

8import foran 

9import foran.foran as fb 

10from foran.render import Template 

11 

12CWD = '.' 

13APP_NAME = 'In front or behind (Foran eller bagved)?' 

14APP_ALIAS = 'foran' 

15app = typer.Typer( 

16 add_completion=False, 

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

18 no_args_is_help=True, 

19) 

20 

21 

22@app.callback(invoke_without_command=True) 

23def callback( 

24 version: bool = typer.Option( 

25 False, 

26 '-V', 

27 '--version', 

28 help='Display the foran version and exit', 

29 is_eager=True, 

30 ) 

31) -> None: 

32 """ 

33 In front or behind (Foran eller bagved)? 

34 

35 Identify and diff a local repository against the remote using templates for reports. 

36 """ 

37 if version: 

38 typer.echo(f'{APP_NAME} version {foran.__version__}') 

39 raise typer.Exit() 

40 

41 

42def classify_template(template: str) -> Template: 

43 """ 

44 Process the received template and return the classification 

45 """ 

46 if not template: 

47 return Template.NONE 

48 

49 if '{{' in str(template): 

50 return Template.JINJA_STRING 

51 

52 if '{' in str(template): 

53 return Template.F_STRING 

54 

55 if pathlib.Path(str(template)).is_file(): 

56 return Template.JINJA_PATH 

57 

58 return Template.STRING 

59 

60 

61@app.command('diff') 

62def diff_repo( 

63 source: str = typer.Argument(CWD), 

64 input: str = typer.Option('', '-i', '--input'), 

65 output: str = typer.Option('', '-o', '--output'), 

66 template: str = typer.Option('', '-t', '--template'), 

67) -> int: 

68 """ 

69 Diff the local against the remote repository state (no template handling yet) 

70 """ 

71 repo_root = input if input else source 

72 target = 'STD_OUT' if not str(output) else str(output) 

73 return fb.main(['diff', repo_root, target, template]) 

74 

75 

76@app.command('label') 

77def label_repo( 

78 source: str = typer.Argument(CWD), 

79 input: str = typer.Option('', '-i', '--input'), 

80 output: str = typer.Option('', '-o', '--output'), 

81 template: str = typer.Option('', '-t', '--template'), 

82) -> int: 

83 """ 

84 Labels the local repository state (no template handling yet) 

85 """ 

86 repo_root = input if input else source 

87 target = 'STD_OUT' if not str(output) else str(output) 

88 return fb.main(['label', repo_root, target, template]) 

89 

90 

91@app.command('template') 

92def app_template(output: str = typer.Option('', '-o', '--output')) -> None: 

93 """ 

94 Output an example jinja template representing the defaults 

95 """ 

96 target = 'STD_OUT' if not output else output 

97 typer.echo(f'Example template generated per {target}') 

98 

99 

100@app.command('version') 

101def app_version() -> None: 

102 """ 

103 Display the foran version and exit 

104 """ 

105 callback(True) 

106 

107 

108# pylint: disable=expression-not-assigned 

109# @app.command() 

110def main(argv: Union[List[str], None] = None) -> int: 

111 """Delegate processing to functional module.""" 

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

113 return fb.main(argv)