Coverage for harbor_cli/commands/api/scan.py: 61%

23 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-09 12:09 +0100

1from __future__ import annotations 

2 

3import typer 

4 

5from ...harbor.artifact import parse_artifact_name 

6from ...logs import logger 

7from ...output.render import render_result 

8from ...state import state 

9from ..help import ARTIFACT_HELP_STRING 

10 

11# Create a command group 

12app = typer.Typer( 

13 name="scan", 

14 help="Scanning of individual artifacts.", 

15 no_args_is_help=True, 

16) 

17 

18 

19# HarborAsyncClient.scan_artifact() 

20@app.command("start") 

21def start_scan( 

22 ctx: typer.Context, 

23 artifact: str = typer.Argument( 

24 ..., 

25 help=ARTIFACT_HELP_STRING, 

26 ), 

27) -> None: 

28 """Start scanning an artifact.""" 

29 an = parse_artifact_name(artifact) 

30 state.run( 

31 state.client.scan_artifact(an.project, an.repository, an.reference), 

32 "Starting artifact scan...", 

33 ) 

34 logger.info(f"Scan of {artifact!r} started.") 

35 # TODO: add some sort of results? 

36 

37 

38# HarborAsyncClient.stop_artifact_scan() 

39@app.command("stop") 

40def stop_scan( 

41 ctx: typer.Context, 

42 artifact: str = typer.Argument( 

43 ..., 

44 help=ARTIFACT_HELP_STRING, 

45 ), 

46) -> None: 

47 """Stop scanning an artifact.""" 

48 an = parse_artifact_name(artifact) 

49 state.run( 

50 state.client.stop_artifact_scan(an.project, an.repository, an.reference), 

51 "Stopping artifact scan...", 

52 ) 

53 logger.info(f"Scan of {artifact!r} stopped.") 

54 

55 

56# HarborAsyncClient.stop_scan_all_job() 

57@app.command("log") 

58def get_artifact_scan_report_log( 

59 ctx: typer.Context, 

60 artifact: str = typer.Argument( 

61 ..., 

62 help=ARTIFACT_HELP_STRING, 

63 ), 

64 report_id: str = typer.Argument( 

65 ..., 

66 help="ID of the report to retrieve logs of.", 

67 ), 

68) -> None: 

69 """Get the log for a specific scan report.""" 

70 an = parse_artifact_name(artifact) 

71 log = state.run( 

72 state.client.get_artifact_scan_report_log( 

73 an.project, an.repository, an.reference, report_id 

74 ), 

75 f"Fetching Artifact scan report log...", 

76 ) 

77 render_result(log, ctx)