-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.py
171 lines (163 loc) · 5.22 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright (c) 2025 SUSE LLC. All rights reserved.
#
# This file is part of kiwi-stackbuild.
#
# kiwi-stackbuild is free software: you can redistribute it and/or modify
# it under the terms owf the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# kiwi-stackbuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kiwi-stackbuild. If not, see <http://www.gnu.org/licenses/>
#
import typer
import itertools
from pathlib import Path
from typing import (
Annotated, Optional, List, Union, no_type_check
)
typers = {
'stackbuild': typer.Typer(
add_completion=False
),
'stash': typer.Typer(
add_completion=False
)
}
system_stackbuild = typers['stackbuild']
system_stash = typers['stash']
@no_type_check
@system_stackbuild.command(
context_settings={
'allow_extra_args': True,
'ignore_unknown_options': True
}
)
def kiwi(
ctx: typer.Context
):
"""
List of command parameters as supported by the kiwi-ng
build or create command. The information given here is passed
along to the kiwi-ng system build or the kiwi-ng system create
command depending on the presence of the --description
option.
"""
Cli = ctx.obj
args = ctx.args
for option in list(set(args)):
if type(option) is not str or not option.startswith('-'):
continue
k: List[Union[str, List]] = [option]
v = []
indexes = [n for n, x in enumerate(args) if x == option]
if len(indexes) > 1:
for index in indexes:
v.append(args[index + 1])
for index in sorted(indexes, reverse=True):
del args[index + 1]
del args[index]
k.append(v)
args += k
Cli.subcommand_args['stackbuild']['system_build_or_create'] = \
dict(itertools.zip_longest(*[iter(args)] * 2))
Cli.global_args['command'] = 'stackbuild'
Cli.global_args['system'] = True
Cli.cli_ok = True
@system_stackbuild.callback(
help='Build an image based on a given stash container root. '
'If no KIWI --description parameter is provided, '
'stackbuild rebuilds the image from the stash container. '
'In this case, the given kiwi parameters are passed to the '
'kiwi-ng system create command. If a KIWI description is '
'provided, this description takes over precedence and a new '
'image from this description based on the given stash container '
'root will be built. In this case, the given kiwi parameters '
'are passed to the kiwi-ng system build command.',
invoke_without_command=False,
subcommand_metavar='kiwi [OPTIONS]'
)
def stackbuild(
ctx: typer.Context,
stash: Annotated[
List[str], typer.Option(
help='<name> Name of the stash container. See system stash --list '
'for available stashes. Multiple --stash options will be stacked '
'together in the given order'
)
],
target_dir: Annotated[
Path, typer.Option(
help='<directory> The target directory to store the '
'system image file(s)'
)
],
description: Annotated[
Optional[Path], typer.Option(
help='<directory> Path to KIWI image description'
)
] = None,
from_registry: Annotated[
Optional[str], typer.Option(
help='<URI> Pull given stash container name from the '
'provided registry URI'
)
] = None,
):
Cli = ctx.obj
Cli.subcommand_args['stackbuild'] = {
'--stash': stash,
'--target-dir': target_dir,
'--description': description,
'--from-registry': from_registry,
'help': False
}
@system_stash.callback(
help='Create a container from the given root directory',
invoke_without_command=True,
subcommand_metavar=''
)
def stash(
ctx: typer.Context,
root: Annotated[
Optional[Path], typer.Option(
help='<directory> The path to the root directory, '
'usually the result of a former system prepare or '
'build call'
)
] = None,
tag: Annotated[
Optional[str], typer.Option(
help='<name> The tag name for the container. '
'By default set to: latest'
)
] = None,
container_name: Annotated[
Optional[str], typer.Option(
help='<name> The name of the container. By default '
'set to the image name of the stash'
)
] = None,
stash_list: Annotated[
Optional[bool], typer.Option(
'--list',
help='List the available stashes'
)
] = False
):
Cli = ctx.obj
Cli.subcommand_args['stash'] = {
'--root': root,
'--tag': tag,
'--container-name': container_name,
'--list': stash_list,
'help': False
}
Cli.global_args['command'] = 'stash'
Cli.global_args['system'] = True
Cli.cli_ok = True