Coverage for foran/cli.py: 100.00%

46 statements  

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

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

2 

3import pathlib 

4import sys 

5from typing import List, Union 

6 

7import typer 

8 

9import foran 

10import foran.foran as fb 

11from foran.render import Template 

12 

13CWD = '.' 

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

15APP_ALIAS = 'foran' 

16app = typer.Typer( 

17 add_completion=False, 

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

19 no_args_is_help=True, 

20) 

21 

22 

23@app.callback(invoke_without_command=True) 

24def callback( 

25 version: bool = typer.Option( 

26 False, 

27 '-V', 

28 '--version', 

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

30 is_eager=True, 

31 ) 

32) -> None: 

33 """ 

34 In front or behind (Foran eller bagved)? 

35 

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

37 """ 

38 if version: 

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

40 raise typer.Exit() 

41 

42 

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

44 """ 

45 Process the received template and return the classification 

46 """ 

47 if not template: 

48 return Template.NONE 

49 

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

51 return Template.JINJA_STRING 

52 

53 if '{' in str(template): 

54 return Template.F_STRING 

55 

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

57 return Template.JINJA_PATH 

58 

59 return Template.STRING 

60 

61 

62@app.command('diff') 

63def diff_repo( 

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

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

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

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

68) -> int: 

69 """ 

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

71 """ 

72 repo_root = input if input else source 

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

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

75 

76 

77@app.command('label') 

78def label_repo( 

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

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

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

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

83) -> int: 

84 """ 

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

86 """ 

87 repo_root = input if input else source 

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

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

90 

91 

92@app.command('template') 

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

94 """ 

95 Output an example jinja template representing the defaults 

96 """ 

97 target = 'STD_OUT' if not output else output 

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

99 

100 

101@app.command('version') 

102def app_version() -> None: 

103 """ 

104 Display the foran version and exit 

105 """ 

106 callback(True) 

107 

108 

109# pylint: disable=expression-not-assigned 

110# @app.command() 

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

112 """Delegate processing to functional module.""" 

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

114 return fb.main(argv)