@@ -82,22 +82,13 @@ func UninstallIstioctl() {
82
82
}
83
83
}
84
84
85
- // Remove istioctl binary from system
86
- osName := runtime .GOOS
87
- var rmCmd * exec.Cmd
88
-
89
- if osName == "windows" {
90
- // Windows: Remove from C:\istioctl
91
- rmCmd = exec .Command ("del" , "/f" , "/q" , "C:\\ istioctl\\ istioctl.exe" )
92
- } else {
93
- // Unix-like: Remove from local bin directory
94
- homeDir , err := os .UserHomeDir ()
95
- if err != nil {
96
- warnError (fmt .Errorf ("failed to get user home directory: %w" , err ))
97
- return
98
- }
99
- rmCmd = exec .Command ("rm" , "-f" , homeDir + "/.local/bin/istioctl" )
85
+ // Remove istioctl binary from local bin directory
86
+ homeDir , err := os .UserHomeDir ()
87
+ if err != nil {
88
+ warnError (fmt .Errorf ("failed to get user home directory: %w" , err ))
89
+ return
100
90
}
91
+ rmCmd := exec .Command ("rm" , "-f" , homeDir + "/.local/bin/istioctl" )
101
92
102
93
if _ , err := Run (rmCmd ); err != nil {
103
94
warnError (fmt .Errorf ("failed to remove istioctl binary: %w" , err ))
@@ -123,21 +114,16 @@ func InstallIstioctl() error {
123
114
return fmt .Errorf ("unsupported architecture: %s" , archName )
124
115
}
125
116
126
- // Map Go OS names to Istio release names and determine file extension
127
- var fileExt string
117
+ // Map Go OS names to Istio release names
128
118
switch osName {
129
119
case "linux" :
130
120
osName = "linux"
131
- fileExt = "tar.gz"
132
121
case "darwin" :
133
122
osName = "osx"
134
- fileExt = "tar.gz"
135
- case "windows" :
136
- osName = "win"
137
- fileExt = "zip"
138
123
default :
139
- return fmt .Errorf ("unsupported operating system : %s" , osName )
124
+ return fmt .Errorf ("only Linux and macOS are supported, got : %s" , osName )
140
125
}
126
+ fileExt := "tar.gz"
141
127
142
128
// Construct the download URL dynamically
143
129
fileName := fmt .Sprintf ("istioctl-%s-%s-%s.%s" , istioctlVersion , osName , archName , fileExt )
@@ -149,43 +135,25 @@ func InstallIstioctl() error {
149
135
binaryName = "istioctl.exe"
150
136
}
151
137
152
- // Download the file using platform-appropriate method with fallbacks
138
+ // Download the file using curl with wget as fallback
153
139
downloadSuccess := false
154
140
155
- // Try primary download method
156
- var primaryCmd * exec.Cmd
157
- var fallbackCmd * exec.Cmd
158
-
159
- if osName == "win" {
160
- // Windows: PowerShell first, curl as fallback
161
- // Use proper PowerShell syntax with double quotes and escape handling
162
- primaryCmd = exec .Command ("powershell" , "-ExecutionPolicy" , "Bypass" , "-Command" ,
163
- fmt .Sprintf (`Invoke-WebRequest -Uri "%s" -OutFile "%s"` , url , fileName ))
164
- fallbackCmd = exec .Command ("curl" , "-L" , url , "-o" , fileName )
165
- } else {
166
- // Unix-like: curl first, wget as fallback
167
- primaryCmd = exec .Command ("curl" , "-L" , url , "-o" , fileName )
168
- fallbackCmd = exec .Command ("wget" , "-O" , fileName , url )
169
- }
170
-
171
- // Try primary method
172
- primaryCmd .Stdout , primaryCmd .Stderr = os .Stdout , os .Stderr
173
- if err := primaryCmd .Run (); err == nil {
141
+ // Try curl first
142
+ curlCmd := exec .Command ("curl" , "-L" , url , "-o" , fileName )
143
+ curlCmd .Stdout , curlCmd .Stderr = os .Stdout , os .Stderr
144
+ if err := curlCmd .Run (); err == nil {
174
145
downloadSuccess = true
175
146
} else {
176
- // Try fallback method
177
- fallbackCmd .Stdout , fallbackCmd .Stderr = os .Stdout , os .Stderr
178
- if err := fallbackCmd .Run (); err == nil {
147
+ // Try wget as fallback
148
+ wgetCmd := exec .Command ("wget" , "-O" , fileName , url )
149
+ wgetCmd .Stdout , wgetCmd .Stderr = os .Stdout , os .Stderr
150
+ if err := wgetCmd .Run (); err == nil {
179
151
downloadSuccess = true
180
152
}
181
153
}
182
154
183
155
if ! downloadSuccess {
184
- if osName == "win" {
185
- return fmt .Errorf ("failed to download istioctl from %s using both PowerShell and curl" , url )
186
- } else {
187
- return fmt .Errorf ("failed to download istioctl from %s using both curl and wget" , url )
188
- }
156
+ return fmt .Errorf ("failed to download istioctl from %s using both curl and wget" , url )
189
157
}
190
158
191
159
// Extract based on file type
@@ -226,69 +194,43 @@ func InstallIstioctl() error {
226
194
normalizedPath := strings .TrimPrefix (binaryPath , "./" )
227
195
if normalizedPath != binaryName {
228
196
var cpCmd * exec.Cmd
229
- if osName == "win" {
230
- cpCmd = exec .Command ("copy" , binaryPath , binaryName )
231
- } else {
232
- cpCmd = exec .Command ("cp" , binaryPath , binaryName )
233
- }
197
+ cpCmd = exec .Command ("cp" , binaryPath , binaryName )
198
+
234
199
if err := cpCmd .Run (); err != nil {
235
200
return fmt .Errorf ("failed to copy istioctl binary: %w" , err )
236
201
}
237
202
}
238
-
239
- // Make executable (not needed on Windows)
240
- if osName != "win" {
241
- chmodCmd := exec .Command ("chmod" , "+x" , binaryName )
242
- if err := chmodCmd .Run (); err != nil {
243
- return fmt .Errorf ("failed to make istioctl executable: %w" , err )
244
- }
203
+ chmodCmd := exec .Command ("chmod" , "+x" , binaryName )
204
+ if err := chmodCmd .Run (); err != nil {
205
+ return fmt .Errorf ("failed to make istioctl executable: %w" , err )
245
206
}
207
+ // Move to local bin directory
208
+ homeDir , err := os .UserHomeDir ()
209
+ if err != nil {
210
+ return fmt .Errorf ("failed to get user home directory: %w" , err )
211
+ }
212
+ binDir := homeDir + "/.local/bin"
246
213
247
- // Move to appropriate bin directory
248
- // Use local bin directory to avoid sudo requirements
249
- var binDir string
250
- var moveCmd * exec.Cmd
251
-
252
- if osName == "win" {
253
- // Use a local bin directory on Windows
254
- binDir = "C:\\ istioctl"
255
- mkdirCmd := exec .Command ("mkdir" , "-p" , binDir )
256
- mkdirCmd .Run () // Ignore errors if directory exists
257
- moveCmd = exec .Command ("move" , binaryName , binDir + "\\ istioctl.exe" )
258
- } else {
259
- // Use local bin directory instead of /usr/local/bin to avoid sudo
260
- homeDir , err := os .UserHomeDir ()
261
- if err != nil {
262
- return fmt .Errorf ("failed to get user home directory: %w" , err )
263
- }
264
- binDir = homeDir + "/.local/bin"
265
-
266
- // Create the bin directory if it doesn't exist
267
- mkdirCmd := exec .Command ("mkdir" , "-p" , binDir )
268
- if err := mkdirCmd .Run (); err != nil {
269
- return fmt .Errorf ("failed to create local bin directory: %w" , err )
270
- }
271
-
272
- moveCmd = exec .Command ("mv" , binaryName , binDir + "/istioctl" )
214
+ // Create the bin directory if it doesn't exist
215
+ mkdirCmd := exec .Command ("mkdir" , "-p" , binDir )
216
+ if err := mkdirCmd .Run (); err != nil {
217
+ return fmt .Errorf ("failed to create local bin directory: %w" , err )
273
218
}
274
219
220
+ moveCmd := exec .Command ("mv" , binaryName , binDir + "/istioctl" )
221
+
275
222
moveCmd .Stdout , moveCmd .Stderr = os .Stdout , os .Stderr
276
223
if err := moveCmd .Run (); err != nil {
277
224
return fmt .Errorf ("failed to move istioctl to bin directory: %w" , err )
278
225
}
279
226
280
227
// Add to PATH notice
281
- if osName != "win" {
282
- fmt .Printf ("istioctl installed to %s\n " , binDir )
283
- fmt .Printf ("Make sure %s is in your PATH by adding this to your shell profile:\n " , binDir )
284
- fmt .Printf ("export PATH=\" %s:$PATH\" \n " , binDir )
285
- }
228
+ fmt .Printf ("istioctl installed to %s\n " , binDir )
229
+ fmt .Printf ("Make sure %s is in your PATH by adding this to your shell profile:\n " , binDir )
230
+ fmt .Printf ("export PATH=\" %s:$PATH\" \n " , binDir )
286
231
287
232
// Clean up downloaded files
288
233
cleanupCmd := exec .Command ("rm" , "-f" , fileName )
289
- if osName == "win" {
290
- cleanupCmd = exec .Command ("del" , "/f" , fileName )
291
- }
292
234
cleanupCmd .Run () // Ignore cleanup errors
293
235
294
236
return nil
0 commit comments