@@ -12,25 +12,25 @@ def get_os_arch():
12
12
system = platform .system ()
13
13
machine = platform .machine ()
14
14
15
- if system == ' Linux' :
16
- if machine == ' x86_64' :
17
- return ' linux' , ' amd64'
18
- elif machine == ' aarch64' :
19
- return ' linux' , ' arm64'
15
+ if system == " Linux" :
16
+ if machine == " x86_64" :
17
+ return " linux" , " amd64"
18
+ elif machine == " aarch64" :
19
+ return " linux" , " arm64"
20
20
else :
21
21
print ("Unsupported architecture for Linux. Aborting download." )
22
22
return None , None
23
- elif system == ' Windows' :
24
- if machine == ' AMD64' :
25
- return ' windows' , ' amd64'
23
+ elif system == " Windows" :
24
+ if machine == " AMD64" :
25
+ return " windows" , " amd64"
26
26
else :
27
27
print ("Unsupported architecture for Windows. Aborting download." )
28
28
return None , None
29
- elif system == ' Darwin' :
30
- if machine == ' x86_64' :
31
- return ' macos' , ' amd64'
32
- elif machine == ' arm64' :
33
- return ' macos' , ' arm64'
29
+ elif system == " Darwin" :
30
+ if machine == " x86_64" :
31
+ return " macos" , " amd64"
32
+ elif machine == " arm64" :
33
+ return " macos" , " arm64"
34
34
else :
35
35
print ("Unsupported architecture for macOS. Aborting download." )
36
36
return None , None
@@ -40,6 +40,9 @@ def get_os_arch():
40
40
41
41
42
42
def download_snyk_cli (download_version , base_url ):
43
+ success = 0
44
+ fail = 1
45
+
43
46
os_type , arch_type = get_os_arch ()
44
47
45
48
if not os_type or not arch_type :
@@ -48,7 +51,8 @@ def download_snyk_cli(download_version, base_url):
48
51
filename , output_filename = get_filename (arch_type , os_type )
49
52
50
53
if download_version != "latest" :
51
- download_version = f"v{ download_version } "
54
+ if download_version [0 ] != "v" : # Add a "v" prefix if it's missing
55
+ download_version = f"v{ download_version } "
52
56
53
57
url = f"{ base_url } /cli/{ download_version } /{ filename } "
54
58
@@ -64,7 +68,7 @@ def download_snyk_cli(download_version, base_url):
64
68
65
69
downloaded_file_path = filename
66
70
67
- with open (downloaded_file_path , 'wb' ) as f :
71
+ with open (downloaded_file_path , "wb" ) as f :
68
72
f .write (response .content )
69
73
70
74
if verify_checksum (downloaded_file_path , sha256_checksum ):
@@ -83,30 +87,31 @@ def download_snyk_cli(download_version, base_url):
83
87
else :
84
88
os .remove (downloaded_file_path )
85
89
print ("SHA256 checksum verification failed. Downloaded file deleted." )
86
- return 0
90
+ return fail
91
+ return success
87
92
else :
88
93
print (f"Failed to download Snyk CLI { download_version } " )
89
- return 1
94
+ return fail
90
95
91
96
92
97
def get_filename (arch_type , os_type ):
93
98
filename = ""
94
99
output_filename = "snyk"
95
100
suffix = ""
96
101
97
- if os_type == ' linux' and arch_type == ' arm64' :
102
+ if os_type == " linux" and arch_type == " arm64" :
98
103
filename = "snyk-linux-arm64"
99
- if os_type == ' linux' and arch_type == ' amd64' :
104
+ if os_type == " linux" and arch_type == " amd64" :
100
105
filename = "snyk-linux"
101
106
stat_result = os .path .exists ("/lib/ld-musl-x86_64.so.1" )
102
107
if stat_result :
103
108
filename = "snyk-alpine"
104
- if os_type == ' windows' and arch_type == ' amd64' :
109
+ if os_type == " windows" and arch_type == " amd64" :
105
110
filename = "snyk-win"
106
111
suffix = ".exe"
107
- if os_type == ' macos' and arch_type == ' amd64' :
112
+ if os_type == " macos" and arch_type == " amd64" :
108
113
filename = "snyk-macos"
109
- if os_type == ' macos' and arch_type == ' arm64' :
114
+ if os_type == " macos" and arch_type == " arm64" :
110
115
filename = "snyk-macos-arm64"
111
116
112
117
filename = filename + suffix
@@ -117,7 +122,7 @@ def get_filename(arch_type, os_type):
117
122
118
123
def verify_checksum (file_path , expected_checksum ):
119
124
sha256 = hashlib .sha256 ()
120
- with open (file_path , 'rb' ) as f :
125
+ with open (file_path , "rb" ) as f :
121
126
while True :
122
127
data = f .read (65536 )
123
128
if not data :
@@ -127,19 +132,36 @@ def verify_checksum(file_path, expected_checksum):
127
132
128
133
129
134
if __name__ == "__main__" :
130
- parser = argparse .ArgumentParser (description = "Download and install a specific version of Snyk CLI." )
131
- parser .add_argument ("version" , help = "Version of Snyk CLI to download (e.g., 1.123.456)" )
132
- parser .add_argument ("--base_url" , help = "Base URL to download from" , default = "https://static.snyk.io" )
135
+ parser = argparse .ArgumentParser (
136
+ description = "Download and install a specific version of Snyk CLI."
137
+ )
138
+ parser .add_argument (
139
+ "version" , help = "Version of Snyk CLI to download (e.g., 1.123.456)"
140
+ )
141
+ parser .add_argument (
142
+ "--base_url" , help = "Base URL to download from" , default = "https://static.snyk.io"
143
+ )
133
144
parser .add_argument ("--retry" , help = "number of retries" , default = 3 )
134
145
135
146
args = parser .parse_args ()
136
147
137
148
for retry in range (1 , args .retry + 1 ):
138
- print ("Trying to download: #" + str (retry ) + " of #" + str (args .retry ))
149
+ print (
150
+ "Trying to download version "
151
+ + str (args .version )
152
+ + ": #"
153
+ + str (retry )
154
+ + " of #"
155
+ + str (args .retry )
156
+ )
139
157
ret_value = download_snyk_cli (args .version , args .base_url )
140
158
if ret_value == 0 :
141
159
break
142
160
else :
143
161
sleep_time = retry * 10
144
- print ("Failed to download Snyk CLI. Retrying in " + str (sleep_time ) + " seconds..." )
162
+ print (
163
+ "Failed to download Snyk CLI. Retrying in "
164
+ + str (sleep_time )
165
+ + " seconds..."
166
+ )
145
167
time .sleep (sleep_time )
0 commit comments