Coverage for turvallisuusneuvonta/cli.py: 100.00%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-18 20:29:38 +00:00

1#! /usr/bin/env python 

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

3# pylint: disable=line-too-long 

4"""Commandline API gateway for turvallisuusneuvonta.""" 

5import pathlib 

6import sys 

7from typing import List, Union 

8 

9import typer 

10 

11import turvallisuusneuvonta 

12import turvallisuusneuvonta.turvallisuusneuvonta as tu 

13 

14APP_NAME = 'Security advisory (Finnish: turvallisuusneuvonta) audit tool.' 

15APP_ALIAS = 'turvallisuusneuvonta' 

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 turvallisuusneuvonta version and exit', 

30 is_eager=True, 

31 ) 

32) -> None: 

33 """ 

34 Security advisory (Finnish: turvallisuusneuvonta) audit tool. 

35 """ 

36 if version: 

37 typer.echo(f'{APP_NAME} version {turvallisuusneuvonta.__version__}') 

38 raise typer.Exit() 

39 

40 

41@app.command('verify') 

42def verify( 

43 source: str = typer.Argument(tu.STDIN), 

44 inp: str = typer.Option( 

45 '', 

46 '-i', 

47 '--input', 

48 help='Path to input file (default is reading from standard in)', 

49 metavar='<sourcepath>', 

50 ), 

51 conf: str = typer.Option( 

52 '', 

53 '-c', 

54 '--config', 

55 help='Path to config file (default is $HOME/.turvallisuusneuvonta.json)', 

56 metavar='<configpath>', 

57 ), 

58) -> int: 

59 """ 

60 Answer the question if now is a working hour. 

61 """ 

62 command = 'verify' 

63 incoming = inp if inp else (source if source != tu.STDIN else '') 

64 config = conf if conf else pathlib.Path.home() / tu.DEFAULT_CONFIG_NAME 

65 action = [command, str(incoming), str(config)] 

66 return sys.exit(tu.main(action)) 

67 

68 

69@app.command('version') 

70def app_version() -> None: 

71 """ 

72 Display the turvallisuusneuvonta version and exit 

73 """ 

74 callback(True) 

75 

76 

77# pylint: disable=expression-not-assigned 

78# @app.command() 

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

80 """Delegate processing to functional module.""" 

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

82 return tu.main(argv)