Skip to content

Commit

Permalink
Check if the plugins are installed before updating or installing them
Browse files Browse the repository at this point in the history
Fix user input scanner (use a slightly better syntax)
Add maps to allow list of depguard
  • Loading branch information
mostafa committed Jan 25, 2024
1 parent aeb6d85 commit 28e0456
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ linters-settings:
- "github.com/google/go-cmp"
- "github.com/google/go-github/v53/github"
- "github.com/codingsince1985/checksum"
- "golang.org/x/exp/maps"
test:
files:
- $test
Expand Down
69 changes: 63 additions & 6 deletions cmd/plugin_install.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/gatewayd-io/gatewayd/config"
"github.com/getsentry/sentry-go"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
yamlv3 "gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -89,26 +92,80 @@ var pluginInstallCmd = &cobra.Command{
pluginsList := cast.ToSlice(localPluginsConfig["plugins"])

// Get the list of plugin download URLs.
var pluginURLs []string
pluginURLs := map[string]string{}
existingPluginURLs := map[string]string{}
for _, plugin := range pluginsList {
// Get the plugin instance.
pluginInstance := cast.ToStringMapString(plugin)

// Append the plugin URL to the list of plugin URLs.
name := cast.ToString(pluginInstance["name"])
url := cast.ToString(pluginInstance["url"])
if url != "" {
pluginURLs = append(pluginURLs, url)
} else {
if url == "" {
cmd.Println("Plugin URL or file path not found in the plugins configuration file for", name)
return
}

// Check if duplicate plugin names exist in the plugins configuration file.
if _, ok := pluginURLs[name]; ok {
cmd.Println("Duplicate plugin name found in the plugins configuration file:", name)
return
}

// Update list of plugin URLs based on
// whether the plugin is already installed or not.
localPath := cast.ToString(pluginInstance["localPath"])
if _, err := os.Stat(localPath); err == nil {
existingPluginURLs[name] = url
} else {
pluginURLs[name] = url
}
}

// Check if the plugin is already installed and prompt the user to confirm the update.
if len(existingPluginURLs) > 0 {
pluginNames := strings.Join(maps.Keys[map[string]string](existingPluginURLs), ", ")
cmd.Printf("The following plugins are already installed: %s\n", pluginNames)

if noPrompt {
if !update {
cmd.Println("Use the --update flag to update the plugins")
cmd.Println("Aborting...")
return
} else {

Check warning on line 135 in cmd/plugin_install.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
// Merge the existing plugin URLs with the plugin URLs.
for name, url := range existingPluginURLs {
pluginURLs[name] = url
}
}
} else {
cmd.Print("Do you want to update the existing plugins? [y/N] ")
var response string
_, err := fmt.Scanln(&response)
if err == nil && strings.ToLower(response) == "y" {
// Set the update flag to true, so that the installPlugin function
// can update the existing plugins and doesn't ask for user input again.
update = true

// Merge the existing plugin URLs with the plugin URLs.
for name, url := range existingPluginURLs {
pluginURLs[name] = url
}
} else {
cmd.Println("Existing plugins will not be updated")
}
}
}

// Validate the plugin URLs.
if len(args) == 0 && len(pluginURLs) == 0 {
cmd.Println(
"No plugin URLs or file path found in the plugins configuration file or CLI argument")
if len(existingPluginURLs) > 0 && !update {
cmd.Println("Use the --update flag to update the plugins")
} else {
cmd.Println(
"No plugin URLs or file path found in the plugins configuration file or CLI argument")
cmd.Println("Aborting...")
}
return
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,9 @@ func installPlugin(cmd *cobra.Command, pluginURL string) {
cmd.Print("Do you want to backup the plugins configuration file? [Y/n] ")
var backupOption string
_, err := fmt.Scanln(&backupOption)
if err == nil && (backupOption == "y" || backupOption == "Y") {
if err == nil && strings.ToLower(backupOption) == "n" {
backupConfig = false
} else {
backupConfig = true
}
}
Expand Down Expand Up @@ -720,7 +722,7 @@ func installPlugin(cmd *cobra.Command, pluginURL string) {

var updateOption string
_, err := fmt.Scanln(&updateOption)
if err == nil && (updateOption == "y" || updateOption == "Y") {
if err != nil && strings.ToLower(updateOption) == "y" {
break
}
}
Expand Down

0 comments on commit 28e0456

Please sign in to comment.