Coverage for harbor_cli/output/table/search.py: 52%

46 statements  

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

1from __future__ import annotations 

2 

3from typing import Sequence 

4 

5from harborapi.models.models import ChartVersion 

6from harborapi.models.models import Search 

7from harborapi.models.models import SearchRepository 

8from harborapi.models.models import SearchResult 

9from rich.console import Group 

10from rich.panel import Panel 

11from rich.table import Table 

12 

13from ...logs import logger 

14from ..formatting.builtin import bool_str 

15from ..formatting.builtin import int_str 

16from ..formatting.builtin import str_str 

17from ._utils import get_table 

18from .project import project_table 

19 

20 

21def search_panel(search: Sequence[Search]) -> Panel: 

22 """Display one or more repositories in a table.""" 

23 if len(search) > 1: 

24 logger.warning("Can only display one search result at a time.") 

25 s = search[0] # guaranteed to be at least one result 

26 

27 tables = [] 

28 # Re-use the project table function 

29 if s.project: 29 ↛ 30line 29 didn't jump to line 30, because the condition on line 29 was never true

30 tables.append(project_table(s.project)) 

31 

32 if s.repository: 32 ↛ 33line 32 didn't jump to line 33, because the condition on line 32 was never true

33 tables.append(searchrepo_table(s.repository)) 

34 

35 if s.chart: 35 ↛ 36line 35 didn't jump to line 36, because the condition on line 35 was never true

36 tables.append(searchresult_table(s.chart)) 

37 

38 # TODO: chart results when they are available 

39 

40 return Panel(Group(*tables), title=f"Search Results", expand=True) 

41 

42 

43def searchrepo_table(repos: Sequence[SearchRepository]) -> Table: 

44 table = get_table("Repository", repos) 

45 table.add_column("Project") 

46 table.add_column("Name") 

47 table.add_column("Artifacts") 

48 table.add_column("Public") 

49 for repo in repos: 

50 table.add_row( 

51 str(repo.project_name), 

52 str(repo.repository_name), 

53 int_str(repo.artifact_count), 

54 bool_str(repo.project_public), 

55 ) 

56 return table 

57 

58 

59def searchresult_table(results: Sequence[SearchResult]) -> Table: 

60 """Table of Helm chart search results.""" 

61 table = get_table("Chart", results) 

62 table.add_column("Project") 

63 table.add_column("Match") # ?? 

64 table.add_column("Digest") 

65 # TODO: sort by score 

66 for result in results: 

67 if not result.chart: 

68 result.chart = ChartVersion() 

69 table.add_row( 

70 str_str(result.name), 

71 int_str(result.score), 

72 str_str(result.chart.digest), 

73 ) 

74 return table