Skip to content

Commit 38bbb86

Browse files
authored
Merge pull request #341 from gatewayd-io/cleanup-after-plugin-install
Clean up downloaded and extracted files after the plugin is installed
2 parents b51cdbf + dcc9941 commit 38bbb86

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

cmd/plugin_install.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"regexp"
99
"runtime"
10+
"slices"
1011
"strings"
1112

1213
"github.com/codingsince1985/checksum"
@@ -31,6 +32,7 @@ const (
3132
var (
3233
pluginOutputDir string
3334
pullOnly bool
35+
cleanup bool
3436
)
3537

3638
// pluginInstallCmd represents the plugin install command.
@@ -39,6 +41,9 @@ var pluginInstallCmd = &cobra.Command{
3941
Short: "Install a plugin from a local archive or a GitHub repository",
4042
Example: " gatewayd plugin install github.com/gatewayd-io/gatewayd-plugin-cache@latest",
4143
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+
4247
// Enable Sentry.
4348
if enableSentry {
4449
// Initialize Sentry.
@@ -139,7 +144,8 @@ var pluginInstallCmd = &cobra.Command{
139144
})
140145
if downloadURL != "" && releaseID != 0 {
141146
cmd.Println("Downloading", downloadURL)
142-
downloadFile(client, account, pluginName, releaseID, pluginFilename)
147+
filePath := downloadFile(client, account, pluginName, releaseID, pluginFilename)
148+
toBeDeleted = append(toBeDeleted, filePath)
143149
cmd.Println("Download completed successfully")
144150
} else {
145151
log.Panic("The plugin file could not be found in the release assets")
@@ -151,7 +157,8 @@ var pluginInstallCmd = &cobra.Command{
151157
})
152158
if checksumsFilename != "" && downloadURL != "" && releaseID != 0 {
153159
cmd.Println("Downloading", downloadURL)
154-
downloadFile(client, account, pluginName, releaseID, checksumsFilename)
160+
filePath := downloadFile(client, account, pluginName, releaseID, checksumsFilename)
161+
toBeDeleted = append(toBeDeleted, filePath)
155162
cmd.Println("Download completed successfully")
156163
} else {
157164
log.Panic("The checksum file could not be found in the release assets")
@@ -185,6 +192,10 @@ var pluginInstallCmd = &cobra.Command{
185192

186193
if pullOnly {
187194
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+
}
188199
return
189200
}
190201
} else {
@@ -203,12 +214,22 @@ var pluginInstallCmd = &cobra.Command{
203214
filenames = extractTarGz(pluginFilename, pluginOutputDir)
204215
}
205216

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+
206221
// Find the extracted plugin binary.
207222
localPath := ""
208223
pluginFileSum := ""
209224
for _, filename := range filenames {
210225
if strings.Contains(filename, pluginName) {
211226
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+
212233
localPath = filename
213234
// Get the checksum for the extracted plugin binary.
214235
// TODO: Should we verify the checksum using the checksum.txt file instead?
@@ -220,9 +241,6 @@ var pluginInstallCmd = &cobra.Command{
220241
}
221242
}
222243

223-
// TODO: Clean up after installing the plugin.
224-
// https://github.com/gatewayd-io/gatewayd/issues/311
225-
226244
// Create a new gatewayd_plugins.yaml file if it doesn't exist.
227245
if _, err := os.Stat(pluginConfigFile); os.IsNotExist(err) {
228246
generateConfig(cmd, Plugins, pluginConfigFile, false)
@@ -306,6 +324,16 @@ var pluginInstallCmd = &cobra.Command{
306324
log.Panic("There was an error writing the plugins configuration file: ", err)
307325
}
308326

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+
309337
// TODO: Add a rollback mechanism.
310338
cmd.Println("Plugin installed successfully")
311339
},
@@ -322,6 +350,9 @@ func init() {
322350
&pluginOutputDir, "output-dir", "o", "./plugins", "Output directory for the plugin")
323351
pluginInstallCmd.Flags().BoolVar(
324352
&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)")
325356
pluginInstallCmd.Flags().BoolVar(
326357
&enableSentry, "sentry", true, "Enable Sentry") // Already exists in run.go
327358
}

cmd/plugin_install_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ func Test_pluginInstallCmd(t *testing.T) {
3636
assert.Contains(t, output, "Name: gatewayd-plugin-cache")
3737

3838
// Clean up.
39+
assert.FileExists(t, "plugins/gatewayd-plugin-cache")
40+
assert.NoFileExists(t, "gatewayd-plugin-cache-linux-amd64-v0.2.4.tar.gz")
41+
assert.NoFileExists(t, "checksums.txt")
42+
assert.NoFileExists(t, "plugins/LICENSE")
43+
assert.NoFileExists(t, "plugins/README.md")
44+
assert.NoFileExists(t, "plugins/checksum.txt")
45+
assert.NoFileExists(t, "plugins/gatewayd_plugin.yaml")
46+
3947
assert.NoError(t, os.RemoveAll("plugins/"))
40-
assert.NoError(t, os.Remove("checksums.txt"))
41-
assert.NoError(t, os.Remove("gatewayd-plugin-cache-linux-amd64-v0.2.4.tar.gz"))
4248
assert.NoError(t, os.Remove(pluginTestConfigFile))
4349
}

cmd/run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ func Test_runCmd(t *testing.T) {
7272
assert.NoError(t, os.Remove(globalTestConfigFile))
7373
}
7474

75+
// Test_runCmdWithMultiTenancy tests the run command with multi-tenancy enabled.
76+
// Note: This test needs two instances of PostgreSQL running on ports 5432 and 5433.
7577
func Test_runCmdWithMultiTenancy(t *testing.T) {
7678
// Create a test plugins config file.
7779
_, err := executeCommandC(rootCmd, "plugin", "init", "--force", "-p", pluginTestConfigFile)
@@ -206,8 +208,6 @@ func Test_runCmdWithCachePlugin(t *testing.T) {
206208

207209
// Clean up.
208210
assert.NoError(t, os.RemoveAll("plugins/"))
209-
assert.NoError(t, os.Remove("checksums.txt"))
210-
assert.NoError(t, os.Remove("gatewayd-plugin-cache-linux-amd64-v0.2.4.tar.gz"))
211211
assert.NoError(t, os.Remove(pluginTestConfigFile))
212212
assert.NoError(t, os.Remove(globalTestConfigFile))
213213
}

cmd/utils.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func findAsset(release *github.RepositoryRelease, match func(string) bool) (stri
390390

391391
func downloadFile(
392392
client *github.Client, account, pluginName string, releaseID int64, filename string,
393-
) {
393+
) string {
394394
// Download the plugin.
395395
readCloser, redirectURL, err := client.Repositories.DownloadReleaseAsset(
396396
context.Background(), account, pluginName, releaseID, http.DefaultClient)
@@ -432,7 +432,8 @@ func downloadFile(
432432
if err != nil {
433433
log.Panic("There was an error downloading the plugin: ", err)
434434
}
435-
output, err := os.Create(path.Join([]string{cwd, filename}...))
435+
filePath := path.Join([]string{cwd, filename}...)
436+
output, err := os.Create(filePath)
436437
if err != nil {
437438
log.Panic("There was an error downloading the plugin: ", err)
438439
}
@@ -443,4 +444,6 @@ func downloadFile(
443444
if err != nil {
444445
log.Panic("There was an error downloading the plugin: ", err)
445446
}
447+
448+
return filePath
446449
}

0 commit comments

Comments
 (0)