Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move default plugin definitions to services plugin package #3650

Closed
wants to merge 13 commits into from
Closed
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,6 @@ golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
1 change: 1 addition & 0 deletions ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func LoadPlugins(ctx context.Context, cmd *cobra.Command) error {
if err == nil {
pluginsConfigs = append(pluginsConfigs, globalCfg.Plugins...)
}

ensureDefaultPlugins(cmd, globalCfg)

if len(pluginsConfigs) == 0 {
Expand Down
42 changes: 9 additions & 33 deletions ignite/cmd/plugin_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,12 @@
"github.com/ignite/cli/ignite/services/plugin"
)

type defaultPlugin struct {
use string
short string
aliases []string
path string
}

const (
PluginNetworkVersion = "v0.1.1"
PluginNetworkPath = "github.com/ignite/cli-plugin-network@" + PluginNetworkVersion
)

// defaultPlugins holds the plugin that are considered trustable and for which
// a command will added if the plugin is not already installed.
// When the user executes that command, the plugin is automatically installed.
var defaultPlugins = []defaultPlugin{
{
use: "network",
short: "Launch a blockchain in production",
aliases: []string{"n"},
path: PluginNetworkPath,
},
}

// ensureDefaultPlugins ensures that all defaultPlugins are wether registered
// in cfg OR have an install command added to rootCmd.
func ensureDefaultPlugins(rootCmd *cobra.Command, cfg *pluginsconfig.Config) {
for _, dp := range defaultPlugins {
for _, dp := range plugin.GetDefaultPlugins() {
// Check if plugin is declared in global config
if cfg.HasPlugin(dp.path) {
if cfg.HasPlugin(dp.Path) {
// plugin found nothing to do
continue
}
Expand All @@ -52,27 +28,27 @@
// - register the config in the global config
// - load the plugin
// - execute the command thanks to the loaded plugin.
func newPluginInstallCmd(dp defaultPlugin) *cobra.Command {
func newPluginInstallCmd(dp plugin.DefaultPlugin) *cobra.Command {
return &cobra.Command{
Use: dp.use,
Short: dp.short,
Aliases: dp.aliases,
Use: dp.Use,
Short: dp.Short,
Aliases: dp.Aliases,
DisableFlagParsing: true, // Avoid -h to skip command run
RunE: func(cmd *cobra.Command, _ []string) error {
cfg, err := parseGlobalPlugins()
if err != nil {
return err
}
if cfg.HasPlugin(dp.path) {
if cfg.HasPlugin(dp.Path) {

Check warning on line 42 in ignite/cmd/plugin_default.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin_default.go#L42

Added line #L42 was not covered by tests
// plugin already declared in global plugins, this shouldn't happen
// because this is actually why this command has been added, so let's
// break violently
panic(fmt.Sprintf("plugin %q unexpected in global config", dp.path))
panic(fmt.Sprintf("plugin %q unexpected in global config", dp.Path))

Check warning on line 46 in ignite/cmd/plugin_default.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin_default.go#L46

Added line #L46 was not covered by tests
}

// add plugin to config
pluginCfg := pluginsconfig.Plugin{
Path: dp.path,
Path: dp.Path,

Check warning on line 51 in ignite/cmd/plugin_default.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin_default.go#L51

Added line #L51 was not covered by tests
}
cfg.Plugins = append(cfg.Plugins, pluginCfg)
if err := cfg.Save(); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions ignite/cmd/plugin_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func TestEnsureDefaultPlugins(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
cmd := &cobra.Command{Use: "ignite"}

// Act
ensureDefaultPlugins(cmd, tt.cfg)

// Assert
expectedCmd := findCommandByPath(cmd, "ignite network")
if tt.expectAddedInCommand {
assert.NotNil(t, expectedCmd)
Expand Down
10 changes: 8 additions & 2 deletions ignite/internal/tools/gen-cli-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ func run(outPath string) error {
if err != nil {
return err
}

p, err := plugin.GetDefaultNetworkPlugin()
if err != nil {
return err
}

// Add network plugin
cfg.Plugins = append(cfg.Plugins, pluginsconfig.Plugin{
// Add network plugin
Path: ignitecmd.PluginNetworkPath,
Path: p.Path,
})
if err := cfg.Save(); err != nil {
return err
Expand Down
38 changes: 38 additions & 0 deletions ignite/services/plugin/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package plugin

import (
"errors"
)

var defaultPlugins = []DefaultPlugin{
{
Use: "network",
Short: "Launch a blockchain in production",
Aliases: []string{"n"},
Path: "github.com/ignite/[email protected]",
},
}

// DefaultPlugin defines a default Ignite plugin.
type DefaultPlugin struct {
Use string
Short string
Aliases []string
Path string
}

// GetDefaultPlugins returns the list of default Ignite plugins.
func GetDefaultPlugins() []DefaultPlugin {
return defaultPlugins
}

// GetDefaultNetworkPlugin returns the default network plugin.
func GetDefaultNetworkPlugin() (DefaultPlugin, error) {
for _, p := range defaultPlugins {
if p.Use == "network" {
return p, nil
}
}

return DefaultPlugin{}, errors.New("default network plugin not found")

Check warning on line 37 in ignite/services/plugin/default.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/plugin/default.go#L37

Added line #L37 was not covered by tests
}
28 changes: 28 additions & 0 deletions ignite/services/plugin/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package plugin

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetDefaultPlugins(t *testing.T) {
// Act
plugins := GetDefaultPlugins()

// Assert
require.Greater(t, len(plugins), 0)
}

func TestGetDefaultNetworkPlugin(t *testing.T) {
// Act
p, err := GetDefaultNetworkPlugin()

// Assert
require.NoError(t, err)
require.True(t, strings.HasPrefix(p.Path, "github.com/ignite/cli-plugin-network@"))
require.Equal(t, "network", p.Use)
require.Equal(t, []string{"n"}, p.Aliases)
require.NotEmpty(t, p.Short)
}
17 changes: 10 additions & 7 deletions integration/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network_test
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -13,19 +14,18 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/require"

ignitecmd "github.com/ignite/cli/ignite/cmd"
chainconfig "github.com/ignite/cli/ignite/config/chain"
"github.com/ignite/cli/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/ignite/pkg/gomodule"
"github.com/ignite/cli/ignite/pkg/xgit"
"github.com/ignite/cli/ignite/services/plugin"
envtest "github.com/ignite/cli/integration"
)

const (
spnModule = "github.com/tendermint/spn"
spnRepoURL = "https://" + spnModule
spnConfigFile = "config_2.yml"
pluginNetworkRepoURL = "https://" + ignitecmd.PluginNetworkPath
spnModule = "github.com/tendermint/spn"
spnRepoURL = "https://" + spnModule
spnConfigFile = "config_2.yml"
)

// setupSPN executes the following tasks:
Expand All @@ -43,9 +43,12 @@ func setupSPN(env envtest.Env) string {
spnVersion string
)
// Clone the cli-plugin-network with the expected version
err := xgit.Clone(context.Background(), pluginNetworkRepoURL, pluginPath)
p, err := plugin.GetDefaultNetworkPlugin()
require.NoError(err)
t.Logf("Checkout cli-plugin-revision to ref %q", ignitecmd.PluginNetworkPath)
pluginNetworkRepoURL := fmt.Sprintf("https://%s", p.Path)
err = xgit.Clone(context.Background(), pluginNetworkRepoURL, pluginPath)
require.NoError(err)
t.Logf("Checkout cli-plugin-revision to ref %q", p.Path)
// Add plugin to config
env.Must(env.Exec("add plugin network",
step.NewSteps(step.New(
Expand Down
Loading