7
7
"path/filepath"
8
8
"regexp"
9
9
"runtime"
10
+ "slices"
10
11
"strings"
11
12
12
13
"github.com/codingsince1985/checksum"
@@ -31,6 +32,7 @@ const (
31
32
var (
32
33
pluginOutputDir string
33
34
pullOnly bool
35
+ cleanup bool
34
36
)
35
37
36
38
// pluginInstallCmd represents the plugin install command.
@@ -39,6 +41,9 @@ var pluginInstallCmd = &cobra.Command{
39
41
Short : "Install a plugin from a local archive or a GitHub repository" ,
40
42
Example : " gatewayd plugin install github.com/gatewayd-io/gatewayd-plugin-cache@latest" ,
41
43
Run : func (cmd * cobra.Command , args []string ) {
44
+ // This is a list of files that will be deleted after the plugin is installed.
45
+ toBeDeleted := []string {}
46
+
42
47
// Enable Sentry.
43
48
if enableSentry {
44
49
// Initialize Sentry.
@@ -139,7 +144,8 @@ var pluginInstallCmd = &cobra.Command{
139
144
})
140
145
if downloadURL != "" && releaseID != 0 {
141
146
cmd .Println ("Downloading" , downloadURL )
142
- downloadFile (client , account , pluginName , releaseID , pluginFilename )
147
+ filePath := downloadFile (client , account , pluginName , releaseID , pluginFilename )
148
+ toBeDeleted = append (toBeDeleted , filePath )
143
149
cmd .Println ("Download completed successfully" )
144
150
} else {
145
151
log .Panic ("The plugin file could not be found in the release assets" )
@@ -151,7 +157,8 @@ var pluginInstallCmd = &cobra.Command{
151
157
})
152
158
if checksumsFilename != "" && downloadURL != "" && releaseID != 0 {
153
159
cmd .Println ("Downloading" , downloadURL )
154
- downloadFile (client , account , pluginName , releaseID , checksumsFilename )
160
+ filePath := downloadFile (client , account , pluginName , releaseID , checksumsFilename )
161
+ toBeDeleted = append (toBeDeleted , filePath )
155
162
cmd .Println ("Download completed successfully" )
156
163
} else {
157
164
log .Panic ("The checksum file could not be found in the release assets" )
@@ -185,6 +192,10 @@ var pluginInstallCmd = &cobra.Command{
185
192
186
193
if pullOnly {
187
194
cmd .Println ("Plugin binary downloaded to" , pluginFilename )
195
+ // Only the checksums file will be deleted if the --pull-only flag is set.
196
+ if err := os .Remove (checksumsFilename ); err != nil {
197
+ log .Panic ("There was an error deleting the file: " , err )
198
+ }
188
199
return
189
200
}
190
201
} else {
@@ -203,12 +214,22 @@ var pluginInstallCmd = &cobra.Command{
203
214
filenames = extractTarGz (pluginFilename , pluginOutputDir )
204
215
}
205
216
217
+ // Delete all the files except the extracted plugin binary,
218
+ // which will be deleted from the list further down.
219
+ toBeDeleted = append (toBeDeleted , filenames ... )
220
+
206
221
// Find the extracted plugin binary.
207
222
localPath := ""
208
223
pluginFileSum := ""
209
224
for _ , filename := range filenames {
210
225
if strings .Contains (filename , pluginName ) {
211
226
cmd .Println ("Plugin binary extracted to" , filename )
227
+
228
+ // Remove the plugin binary from the list of files to be deleted.
229
+ toBeDeleted = slices .DeleteFunc [[]string , string ](toBeDeleted , func (s string ) bool {
230
+ return s == filename
231
+ })
232
+
212
233
localPath = filename
213
234
// Get the checksum for the extracted plugin binary.
214
235
// TODO: Should we verify the checksum using the checksum.txt file instead?
@@ -220,9 +241,6 @@ var pluginInstallCmd = &cobra.Command{
220
241
}
221
242
}
222
243
223
- // TODO: Clean up after installing the plugin.
224
- // https://github.com/gatewayd-io/gatewayd/issues/311
225
-
226
244
// Create a new gatewayd_plugins.yaml file if it doesn't exist.
227
245
if _ , err := os .Stat (pluginConfigFile ); os .IsNotExist (err ) {
228
246
generateConfig (cmd , Plugins , pluginConfigFile , false )
@@ -306,6 +324,16 @@ var pluginInstallCmd = &cobra.Command{
306
324
log .Panic ("There was an error writing the plugins configuration file: " , err )
307
325
}
308
326
327
+ // Delete the downloaded and extracted files, except the plugin binary,
328
+ // if the --cleanup flag is set.
329
+ if cleanup {
330
+ for _ , filename := range toBeDeleted {
331
+ if err := os .Remove (filename ); err != nil {
332
+ log .Panic ("There was an error deleting the file: " , err )
333
+ }
334
+ }
335
+ }
336
+
309
337
// TODO: Add a rollback mechanism.
310
338
cmd .Println ("Plugin installed successfully" )
311
339
},
@@ -322,6 +350,9 @@ func init() {
322
350
& pluginOutputDir , "output-dir" , "o" , "./plugins" , "Output directory for the plugin" )
323
351
pluginInstallCmd .Flags ().BoolVar (
324
352
& pullOnly , "pull-only" , false , "Only pull the plugin, don't install it" )
353
+ pluginInstallCmd .Flags ().BoolVar (
354
+ & cleanup , "cleanup" , true ,
355
+ "Delete downloaded and extracted files after installing the plugin (except the plugin binary)" )
325
356
pluginInstallCmd .Flags ().BoolVar (
326
357
& enableSentry , "sentry" , true , "Enable Sentry" ) // Already exists in run.go
327
358
}
0 commit comments