18
18
import datetime
19
19
import pytz
20
20
import tzlocal
21
+ import sys
21
22
from lynkctx import LynkContext
22
23
23
24
@@ -33,18 +34,18 @@ def print_products(lynk_ctx):
33
34
34
35
# Calculate dynamic column widths
35
36
name_width = max (len ("NAME" ), max (len (prod ['name' ])
36
- for prod in products ))
37
+ for prod in products ))
37
38
updated_at_width = max (len ("UPDATED AT" ),
38
39
max (len (user_time (prod ['updatedAt' ]))
39
40
for prod in products ))
40
41
id_width = max (len ("ID" ), max (len (prod ['id' ]) for prod in products ))
41
42
version_width = len ("VERSIONS" )
42
43
43
44
header = (
44
- f"{ 'NAME' :<{name_width }} | "
45
- f"{ 'ID' :<{id_width }} | "
46
- f"{ 'VERSIONS' :<{version_width }} | "
47
- f"{ 'UPDATED AT' :<{updated_at_width }} | "
45
+ f"{ 'NAME' :<{name_width }} | "
46
+ f"{ 'ID' :<{id_width }} | "
47
+ f"{ 'VERSIONS' :<{version_width }} | "
48
+ f"{ 'UPDATED AT' :<{updated_at_width }} | "
48
49
)
49
50
print (header )
50
51
@@ -119,18 +120,21 @@ def print_versions(lynk_ctx):
119
120
120
121
return 0
121
122
123
+
122
124
def download_sbom (lynk_ctx ):
123
125
sbom = lynk_ctx .download ()
124
126
if sbom is None :
125
127
print ('Failed to fetch SBOM' )
126
128
return 1
127
129
128
- print (sbom .encode ("utf-8" ))
130
+ sys . stdout . buffer . write (sbom .encode ("utf-8" , errors = 'ignore' ))
129
131
return 0
130
132
133
+
131
134
def upload_sbom (lynk_ctx , sbom_file ):
132
135
return lynk_ctx .upload (sbom_file )
133
-
136
+
137
+
134
138
def setup_args ():
135
139
parser = argparse .ArgumentParser (description = 'Interlynk command line tool' )
136
140
parser .add_argument ('--verbose' , '-v' , action = 'count' , default = 0 )
@@ -164,11 +168,13 @@ def setup_args():
164
168
help = "Security token" )
165
169
166
170
download_parser = subparsers .add_parser ("download" , help = "Download SBOM" )
167
- download_group = download_parser .add_mutually_exclusive_group (required = True )
171
+ download_group = download_parser .add_mutually_exclusive_group (
172
+ required = True )
168
173
download_group .add_argument ("--prod" , help = "Product name" )
169
174
download_group .add_argument ("--prodId" , help = "Product ID" )
170
175
171
- download_group = download_parser .add_mutually_exclusive_group (required = True )
176
+ download_group = download_parser .add_mutually_exclusive_group (
177
+ required = True )
172
178
download_group .add_argument ("--ver" , help = "Version" )
173
179
download_group .add_argument ("--verId" , help = "Version ID" )
174
180
@@ -186,18 +192,21 @@ def setup_log_level(args):
186
192
logging .basicConfig (level = logging .ERROR )
187
193
logging .basicConfig (level = logging .DEBUG )
188
194
195
+
189
196
def setup_lynk_context (args ):
190
197
return LynkContext (
191
198
os .environ .get ('INTERLYNK_API_URL' ),
192
- getattr (args , 'token' , None ) or os .environ .get ('INTERLYNK_SECURITY_TOKEN' ),
199
+ getattr (args , 'token' , None ) or os .environ .get (
200
+ 'INTERLYNK_SECURITY_TOKEN' ),
193
201
getattr (args , 'prodId' , None ),
194
202
getattr (args , 'prod' , None ),
195
203
getattr (args , 'envId' , None ),
196
204
getattr (args , 'env' , None ),
197
205
getattr (args , 'verId' , None ),
198
206
getattr (args , 'ver' , None )
199
207
)
200
-
208
+
209
+
201
210
def main () -> int :
202
211
args = setup_args ()
203
212
setup_log_level (args )
@@ -207,17 +216,18 @@ def main() -> int:
207
216
208
217
if args .subcommand == "prods" :
209
218
print_products (lynk_ctx )
210
- elif args .subcommand == "vers" :
219
+ elif args .subcommand == "vers" :
211
220
print_versions (lynk_ctx )
212
221
elif args .subcommand == "upload" :
213
222
upload_sbom (lynk_ctx , args .sbom )
214
223
elif args .subcommand == "download" :
215
224
download_sbom (lynk_ctx )
216
225
else :
217
- print ("Missing or invalid command. \
226
+ print ("Missing or invalid command. \
218
227
Supported commands: {prods, upload, download, sign, verify}" )
219
- exit (1 )
228
+ exit (1 )
220
229
exit (0 )
221
230
231
+
222
232
if __name__ == "__main__" :
223
233
main ()
0 commit comments