1
+ """list-tools command implementation."""
2
+ # dem/cli/command/list_tools_cmd.py
3
+
4
+ from dem .core .platform import Platform
5
+ from dem .core .tool_images import ToolImage
6
+ from dem .cli .console import stdout , stderr
7
+ from rich .table import Table
8
+ import typer
9
+
10
+ def list_local_tools (platform : Platform ) -> None :
11
+ """ List the local tools.
12
+
13
+ Args:
14
+ platform -- the Platform
15
+
16
+ Exceptions:
17
+ typer.Abort -- if no local tool images are available
18
+ """
19
+ # by settings this to True, the update method won't try to update the registry tools
20
+ platform .local_only = True
21
+
22
+ if not platform .tool_images .all_tool_images :
23
+ stdout .print ("[yellow]No local tool images are available.[/]" )
24
+ raise typer .Abort ()
25
+
26
+ stdout .print (f"\n [italic]Local Tool Images[/]" )
27
+ # sorted will return a list of tuples, so we can iterate over the items
28
+ for tool_image in sorted (platform .tool_images .all_tool_images .items ()):
29
+ # tool_image[0] is the name of the tool image and tool_image[1] is the ToolImage instance
30
+ stdout .print (f" { tool_image [0 ]} " )
31
+
32
+ def update_tools_from_selected_regs (platform : Platform , specified_regs : list [str ]) -> None :
33
+ """ Update the tools from the selected registries only.
34
+
35
+ Args:
36
+ platform -- the Platform
37
+ specified_regs -- the selected registry names
38
+
39
+ Exceptions:
40
+ typer.Abort -- if an unknown registry is specified
41
+ """
42
+ # by settings this to True, the update method won't start automatically so we can provide
43
+ # the selected registry names
44
+ platform .disable_tool_update = True
45
+
46
+ available_regs = set ([reg ["name" ] for reg in platform .config_file .registries ])
47
+ specified_regs = set (specified_regs )
48
+
49
+ if not specified_regs .issubset (available_regs ):
50
+ for unkown_reg in specified_regs - available_regs :
51
+ stderr .print (f"[red]Error: Registry { unkown_reg } is not available![/]" )
52
+ raise typer .Abort ()
53
+
54
+ platform .tool_images .update (reg_selection = specified_regs )
55
+
56
+ def list_tools_from_regs (platform : Platform , table : Table ) -> None :
57
+ """ List the available tools from the registries.
58
+
59
+ Args:
60
+ platform -- the Platform
61
+ table -- the rich Table instance to fill with the tool image data
62
+ """
63
+ table .add_column ("Name" )
64
+ table .add_column ("Available locally?" )
65
+ # sorted will return a list of tuples, so we can iterate over the items
66
+ for tool_image in sorted (platform .tool_images .get_registry_ones ().items ()):
67
+ # tool_image[0] is the name of the tool image and tool_image[1] is the ToolImage instance
68
+ tool_image_name = tool_image [0 ]
69
+ tool_image : ToolImage = tool_image [1 ]
70
+
71
+ if tool_image .availability == ToolImage .LOCAL_AND_REGISTRY :
72
+ available_locally = "[green]✔[/]"
73
+ else :
74
+ available_locally = ""
75
+
76
+ table .add_row (tool_image_name , available_locally )
77
+
78
+ def list_tools_from_selected_regs (platform : Platform , specified_regs : list [str ]) -> None :
79
+ """ List the available tools from the selected registries only.
80
+
81
+ Args:
82
+ platform -- the Platform
83
+ specified_regs -- the selected registry names
84
+
85
+ Exceptions:
86
+ typer.Abort -- if no tool images are available in the selected registries
87
+ """
88
+ update_tools_from_selected_regs (platform , specified_regs )
89
+
90
+ if not platform .tool_images .get_registry_ones ():
91
+ stdout .print ("[yellow]No tool images are available in the selected registries.[/]" )
92
+ raise typer .Abort ()
93
+
94
+ table = Table ()
95
+ list_tools_from_regs (platform , table )
96
+ stdout .print (f"\n [italic]Available Tool Images from the selected registries[/]" )
97
+ stdout .print (table )
98
+
99
+ def list_tools_from_all_regs (platform : Platform ) -> None :
100
+ """ List the available tools from all registries.
101
+
102
+ Args:
103
+ platform -- the Platform
104
+
105
+ Exceptions:
106
+ typer.Abort -- if no tool images are available in the registries
107
+ """
108
+ if not platform .tool_images .get_registry_ones ():
109
+ stdout .print ("[yellow]No tool images are available in the registries.[/]" )
110
+ raise typer .Abort ()
111
+
112
+ table = Table ()
113
+ list_tools_from_regs (platform , table )
114
+ stdout .print (f"\n [italic]Available Tool Images from all registries[/]" )
115
+ stdout .print (table )
116
+
117
+ def execute (platform : Platform , reg : bool , selected_regs : list [str ]) -> None :
118
+ """ List the available tools.
119
+
120
+ Args:
121
+ platform -- the Platform
122
+ reg -- the flag to list the tools from the registries
123
+ selected_regs -- the selected registry names
124
+
125
+ Exceptions:
126
+ typer.Abort -- if no tool images are available either locally or in the registries or
127
+ if an unknown registry is specified
128
+ """
129
+ if not reg :
130
+ list_local_tools (platform )
131
+ elif selected_regs :
132
+ list_tools_from_selected_regs (platform , selected_regs )
133
+ else :
134
+ list_tools_from_all_regs (platform )
0 commit comments