Skip to content

Commit

Permalink
Remove all the files after installing the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Sep 28, 2023
1 parent b51cdbf commit 359539c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
34 changes: 29 additions & 5 deletions cmd/plugin_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"

"github.com/codingsince1985/checksum"
Expand Down Expand Up @@ -39,6 +40,9 @@ var pluginInstallCmd = &cobra.Command{
Short: "Install a plugin from a local archive or a GitHub repository",
Example: " gatewayd plugin install github.com/gatewayd-io/gatewayd-plugin-cache@latest",
Run: func(cmd *cobra.Command, args []string) {
// This is a list of files that will be deleted after the plugin is installed.
toBeDeleted := []string{}

// Enable Sentry.
if enableSentry {
// Initialize Sentry.
Expand Down Expand Up @@ -139,7 +143,8 @@ var pluginInstallCmd = &cobra.Command{
})
if downloadURL != "" && releaseID != 0 {
cmd.Println("Downloading", downloadURL)
downloadFile(client, account, pluginName, releaseID, pluginFilename)
filePath := downloadFile(client, account, pluginName, releaseID, pluginFilename)
toBeDeleted = append(toBeDeleted, filePath)
cmd.Println("Download completed successfully")
} else {
log.Panic("The plugin file could not be found in the release assets")
Expand All @@ -151,7 +156,8 @@ var pluginInstallCmd = &cobra.Command{
})
if checksumsFilename != "" && downloadURL != "" && releaseID != 0 {
cmd.Println("Downloading", downloadURL)
downloadFile(client, account, pluginName, releaseID, checksumsFilename)
filePath := downloadFile(client, account, pluginName, releaseID, checksumsFilename)
toBeDeleted = append(toBeDeleted, filePath)
cmd.Println("Download completed successfully")
} else {
log.Panic("The checksum file could not be found in the release assets")
Expand Down Expand Up @@ -185,6 +191,10 @@ var pluginInstallCmd = &cobra.Command{

if pullOnly {
cmd.Println("Plugin binary downloaded to", pluginFilename)
// Only the checksums file will be deleted if the --pull-only flag is set.
if err := os.Remove(checksumsFilename); err != nil {
log.Panic("There was an error deleting the file: ", err)
}
return
}
} else {
Expand All @@ -203,12 +213,22 @@ var pluginInstallCmd = &cobra.Command{
filenames = extractTarGz(pluginFilename, pluginOutputDir)
}

// Delete all the files except the extracted plugin binary,
// which will be deleted from the list further down.
toBeDeleted = append(toBeDeleted, filenames...)

// Find the extracted plugin binary.
localPath := ""
pluginFileSum := ""
for _, filename := range filenames {
if strings.Contains(filename, pluginName) {
cmd.Println("Plugin binary extracted to", filename)

// Remove the plugin binary from the list of files to be deleted.
toBeDeleted = slices.DeleteFunc[[]string, string](toBeDeleted, func(s string) bool {
return s == filename
})

localPath = filename
// Get the checksum for the extracted plugin binary.
// TODO: Should we verify the checksum using the checksum.txt file instead?
Expand All @@ -220,9 +240,6 @@ var pluginInstallCmd = &cobra.Command{
}
}

// TODO: Clean up after installing the plugin.
// https://github.com/gatewayd-io/gatewayd/issues/311

// Create a new gatewayd_plugins.yaml file if it doesn't exist.
if _, err := os.Stat(pluginConfigFile); os.IsNotExist(err) {
generateConfig(cmd, Plugins, pluginConfigFile, false)
Expand Down Expand Up @@ -306,6 +323,13 @@ var pluginInstallCmd = &cobra.Command{
log.Panic("There was an error writing the plugins configuration file: ", err)
}

// Delete the downloaded files.
for _, filename := range toBeDeleted {
if err := os.Remove(filename); err != nil {
log.Panic("There was an error deleting the file: ", err)
}
}

// TODO: Add a rollback mechanism.
cmd.Println("Plugin installed successfully")
},
Expand Down
7 changes: 5 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func findAsset(release *github.RepositoryRelease, match func(string) bool) (stri

func downloadFile(
client *github.Client, account, pluginName string, releaseID int64, filename string,
) {
) string {
// Download the plugin.
readCloser, redirectURL, err := client.Repositories.DownloadReleaseAsset(
context.Background(), account, pluginName, releaseID, http.DefaultClient)
Expand Down Expand Up @@ -432,7 +432,8 @@ func downloadFile(
if err != nil {
log.Panic("There was an error downloading the plugin: ", err)
}
output, err := os.Create(path.Join([]string{cwd, filename}...))
filePath := path.Join([]string{cwd, filename}...)
output, err := os.Create(filePath)
if err != nil {
log.Panic("There was an error downloading the plugin: ", err)
}
Expand All @@ -443,4 +444,6 @@ func downloadFile(
if err != nil {
log.Panic("There was an error downloading the plugin: ", err)
}

return filePath
}

0 comments on commit 359539c

Please sign in to comment.