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

feat(config): global plugins config #3214

Merged
merged 107 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 106 commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
d4ca56b
base
Nov 28, 2022
93c1ad5
base refactor
Nov 28, 2022
ee162da
refactor
Nov 28, 2022
8303068
rename
Nov 28, 2022
6565fc5
rename
Nov 28, 2022
863876f
fix comment
Nov 28, 2022
eaccb71
rename file
Nov 28, 2022
c490fa5
format
Nov 28, 2022
844c83b
refactor base
Nov 28, 2022
82e9ac0
format
Nov 28, 2022
a8592a5
refactor some imports
Nov 28, 2022
1d5bd3e
imports refactor
Nov 28, 2022
15e543a
fix
Nov 28, 2022
2cf58e4
fix import
Nov 28, 2022
4d986c3
Merge branch 'main' into feat/plugins-refacotr
Nov 28, 2022
5f8c837
refactor:
Nov 28, 2022
1459f1c
changelog
Nov 28, 2022
365df2d
Merge branch 'main' into feat/separate-plugins-config
Nov 28, 2022
ea5434a
changelog
Nov 28, 2022
1a3167e
Update ignite/services/network/networkchain/init.go
Nov 28, 2022
0ad9f3d
base -> baseconfig
Nov 28, 2022
360bc17
refactoring for clarity
Nov 28, 2022
71a75aa
v12 -> v1
Nov 28, 2022
9b89846
fix tests
Nov 28, 2022
a69a758
fix integration
Nov 28, 2022
f74180a
fix name
Nov 28, 2022
b57ac11
fix integration
Nov 28, 2022
6d081b6
Merge branch 'main' into feat/plugins-refacotr
Nov 28, 2022
7408d51
merge
Nov 28, 2022
db2ed00
format
Nov 28, 2022
1620580
merge main
Nov 28, 2022
8d24282
Merge branch 'main' into feat/separate-plugins-config
Nov 28, 2022
a40d3dc
Update ignite/cmd/cmd.go
Nov 29, 2022
e685899
imports
Nov 29, 2022
aa57e74
Merge branch 'main' into feat/separate-plugins-config
Nov 29, 2022
5c429be
LocateDefault logic
Nov 29, 2022
a6c78e9
lint fix
Nov 29, 2022
f0bde05
Merge branch 'main' into feat/separate-plugins-config
Nov 29, 2022
54e7fe3
refactor and test
Nov 29, 2022
39e92e7
finish refactor
Nov 29, 2022
9f10308
revert
Nov 29, 2022
5b4e13b
move
Nov 29, 2022
72c3123
move
Nov 29, 2022
c9c9683
rename
Nov 29, 2022
f5c490d
fix tests
Nov 29, 2022
69ca934
fix test
Nov 29, 2022
290d983
rename
Nov 29, 2022
bc69558
add global
Nov 29, 2022
6811592
functionality
Nov 29, 2022
ce5d761
format
Nov 29, 2022
a368b6a
fix error statement for global plugins
Nov 29, 2022
6579c11
typo
Nov 30, 2022
dcdaa2d
chainconfig
Nov 30, 2022
e7b53a7
imports
Nov 30, 2022
d5da513
fix imports
Nov 30, 2022
b710b08
Merge branch 'main' into refactor/config-organization
Nov 30, 2022
b0e3722
Merge branch 'main' into feat/separate-plugins-config
Nov 30, 2022
631d64e
address review
Nov 30, 2022
e2d5f56
testdata
Nov 30, 2022
a943a47
merge
Nov 30, 2022
225c8e9
merge
Nov 30, 2022
33254ee
Merge branch 'main' into refactor/config-organization
Nov 30, 2022
306ce32
Merge branch 'main' into refactor/config-organization
Dec 1, 2022
0d5b474
Merge branch 'main' into refactor/config-organization
Dec 1, 2022
49c6801
better import
Dec 1, 2022
6fc54e1
fix test
Dec 1, 2022
7270f71
Merge branch 'main' into refactor/config-organization
Dec 1, 2022
e187196
Merge branch 'main' into refactor/config-organization
Dec 1, 2022
419134e
Merge branch 'refactor/config-organization' into feat/global-plugins
Dec 1, 2022
1220b44
changelog
Dec 1, 2022
742be4c
refactor
Dec 1, 2022
157022d
Merge branch 'main' into feat/global-plugins
Dec 5, 2022
1e5ec85
simplify
Dec 5, 2022
8b10b7f
Merge branch 'main' into feat/global-plugins
Dec 6, 2022
dec6b0a
merge main
Dec 8, 2022
f825c53
merge main
Dec 12, 2022
b5c967e
RemoveDuplicates
Dec 12, 2022
1ecfc28
refactor LoadPlugins
Dec 12, 2022
f67a43c
Merge branch 'main' into feat/global-plugins
Dec 12, 2022
859d6d3
lint
Dec 12, 2022
9c53c49
add global flag
Dec 13, 2022
0862e8f
add info
Dec 13, 2022
284819c
add error prints
Dec 13, 2022
fa84c3b
simplify return
Dec 13, 2022
bce47a7
fix test
Dec 13, 2022
c5bc6ec
Merge branch 'main' into feat/global-plugins
Dec 13, 2022
aa10107
fix global parse
Dec 13, 2022
1149ae8
add integration test
Dec 13, 2022
054879c
modify test
Dec 13, 2022
f8500bf
fix returns
Dec 13, 2022
bd4b6ef
use ignite example plugin
Dec 13, 2022
58c6829
refactor global flag
Dec 13, 2022
61ae132
Merge branch 'main' into feat/global-plugins
Dec 13, 2022
a3d9d66
Fix plugin integration test
tbruyelle Dec 13, 2022
eb63d31
add removed TODO by mistake
tbruyelle Dec 13, 2022
0bea059
move flags
Dec 13, 2022
36db419
return err
Dec 13, 2022
49dcbc6
Update ignite/cmd/plugin.go
Dec 13, 2022
14ee295
format
Dec 13, 2022
10d9145
fix test
Dec 13, 2022
d1694b7
remove yaml tag
Dec 13, 2022
ae33619
Update ignite/cmd/plugin.go
Dec 13, 2022
d0a1e0c
fix test
Dec 13, 2022
6416303
Merge branch 'main' into feat/global-plugins
Dec 13, 2022
222a061
test: add key/value pairs to plugin add
tbruyelle Dec 13, 2022
edf6f69
Merge branch 'main' into feat/global-plugins
Dec 13, 2022
604dd60
add detailed plugin
Dec 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- [#3214](https://github.com/ignite/cli/pull/3214) Global plugins config.
- [#3142](https://github.com/ignite/cli/pull/3142) Add `ignite network request param-change` command.
- [#3181](https://github.com/ignite/cli/pull/3181) Addition of `add` `remove` commands for `plugins`
- [#3184](https://github.com/ignite/cli/pull/3184) Separate `plugins.yml` config file.
Expand Down
125 changes: 104 additions & 21 deletions ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

pluginsconfig "github.com/ignite/cli/ignite/config/plugins"
"github.com/ignite/cli/ignite/pkg/clictx"
Expand All @@ -19,29 +20,44 @@ import (
"github.com/ignite/cli/ignite/services/plugin"
)

const (
igniteCmdPrefix = "ignite "
flagPluginsGlobal = "global"
)

// plugins hold the list of plugin declared in the config.
// A global variable is used so the list is accessible to the plugin commands.
var plugins []*plugin.Plugin

const (
igniteCmdPrefix = "ignite "
)

// LoadPlugins tries to load all the plugins found in configuration.
// If no configuration found, it returns w/o error.
// LoadPlugins tries to load all the plugins found in configurations.
// If no configurations found, it returns w/o error.
func LoadPlugins(ctx context.Context, rootCmd *cobra.Command) error {
cfg, err := parseLocalPlugins(rootCmd)
if err != nil {
// if binary is run where there is no plugins.yml, don't load
return nil
pluginsConfigs := make([]pluginsconfig.Plugin, 0)

localCfg, err := parseLocalPlugins(rootCmd)
if err != nil && !errors.As(err, &cosmosanalysis.ErrPathNotChain{}) {
return err
} else if err == nil {
pluginsConfigs = append(pluginsConfigs, localCfg.Plugins...)
}

globalCfg, err := parseGlobalPlugins()
if err == nil {
tbruyelle marked this conversation as resolved.
Show resolved Hide resolved
pluginsConfigs = append(pluginsConfigs, globalCfg.Plugins...)
}
aljo242 marked this conversation as resolved.
Show resolved Hide resolved

// TODO: parse global config
if len(pluginsConfigs) == 0 {
return nil
}

plugins, err = plugin.Load(ctx, cfg)
uniquePlugins := plugin.RemoveDuplicates(pluginsConfigs)
plugins, err = plugin.Load(ctx, uniquePlugins)
if err != nil {
return err
} else if len(plugins) == 0 {
return nil
}

return loadPlugins(rootCmd, plugins)
}

Expand All @@ -62,6 +78,23 @@ func parseLocalPlugins(cmd *cobra.Command) (*pluginsconfig.Config, error) {
return pluginsconfig.ParseDir(wd)
}

func parseGlobalPlugins() (cfg *pluginsconfig.Config, err error) {
globalDir, err := plugin.PluginsPath()
if err != nil {
return cfg, err
}

cfg, err = pluginsconfig.ParseDir(globalDir)
if err != nil {
return &pluginsconfig.Config{}, nil
}
aljo242 marked this conversation as resolved.
Show resolved Hide resolved

for i := range cfg.Plugins {
cfg.Plugins[i].Global = true
}
return
}

func loadPlugins(rootCmd *cobra.Command, plugins []*plugin.Plugin) error {
// Link plugins to related commands
var loadErrors []string
Expand Down Expand Up @@ -373,8 +406,8 @@ func NewPluginUpdate() *cobra.Command {
func NewPluginAdd() *cobra.Command {
cmdPluginAdd := &cobra.Command{
Use: "add [path] [key=value]...",
Short: "Adds a plugin declaration to a chain's plugin configuration",
Long: `Adds a plugin declaration to a chain's plugin configuration.
Short: "Adds a plugin declaration to a plugin configuration",
Long: `Adds a plugin declaration to a plugin configuration.
Respects key value pairs declared after the plugin path to be added to the
generated configuration definition.
Example:
Expand All @@ -384,7 +417,17 @@ Example:
session := cliui.New(cliui.WithStdout(os.Stdout))
defer session.End()

conf, err := parseLocalPlugins(cmd)
var (
conf *pluginsconfig.Config
err error
)

global := flagGetPluginsGlobal(cmd)
if global {
conf, err = parseGlobalPlugins()
} else {
conf, err = parseLocalPlugins(cmd)
}
if err != nil {
return err
}
Expand All @@ -396,8 +439,9 @@ Example:
}

p := pluginsconfig.Plugin{
Path: args[0],
With: make(map[string]string),
Path: args[0],
With: make(map[string]string),
Global: global,
}

var pluginArgs []string
Expand Down Expand Up @@ -432,6 +476,9 @@ Example:
return nil
},
}

cmdPluginAdd.Flags().AddFlagSet(flagSetPluginsGlobal())

return cmdPluginAdd
}

Expand All @@ -444,7 +491,17 @@ func NewPluginRemove() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
s := cliui.New(cliui.WithStdout(os.Stdout))

conf, err := parseLocalPlugins(cmd)
var (
conf *pluginsconfig.Config
err error
)

global := flagGetPluginsGlobal(cmd)
if global {
conf, err = parseGlobalPlugins()
} else {
conf, err = parseLocalPlugins(cmd)
}
if err != nil {
return err
}
Expand All @@ -461,10 +518,14 @@ func NewPluginRemove() *cobra.Command {
}

s.Printf("%s %s removed\n", icons.OK, args[0])
s.Printf("\t%s updated\n", conf.Path())

return nil
},
}

cmdPluginRemove.Flags().AddFlagSet(flagSetPluginsGlobal())

return cmdPluginRemove
}

Expand Down Expand Up @@ -552,12 +613,22 @@ func printPlugins(session *cliui.Session) error {
hookCount = len(manifest.Hooks)
cmdCount = len(manifest.Commands)
)
return fmt.Sprintf("%s Loaded 🪝%d 💻%d", icons.OK, hookCount, cmdCount)

return fmt.Sprintf("%s Loaded: 🪝%d 💻%d", icons.OK, hookCount, cmdCount)
}

installedStatus := func(p *plugin.Plugin) string {
installed := "local"
if p.IsGlobal() {
installed = "global"
}
return installed
}

for _, p := range plugins {
entries = append(entries, []string{p.Path, buildStatus(p)})
entries = append(entries, []string{p.Path, buildStatus(p), installedStatus(p)})
}
if err := session.PrintTable([]string{"Path", "Status"}, entries...); err != nil {
if err := session.PrintTable([]string{"Path", "Status", "Config"}, entries...); err != nil {
return fmt.Errorf("error while printing plugins: %w", err)
}
return nil
Expand Down Expand Up @@ -613,3 +684,15 @@ func printPluginHooks(hooks []plugin.Hook, session *cliui.Session) error {
}
return nil
}

func flagSetPluginsGlobal() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.BoolP(flagPluginsGlobal, "g", false, "use global plugins configuration"+
" ($HOME/.ignite/plugins/plugins.yml)")
return fs
}

func flagGetPluginsGlobal(cmd *cobra.Command) bool {
global, _ := cmd.Flags().GetBool(flagPluginsGlobal)
return global
}
15 changes: 2 additions & 13 deletions ignite/config/config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
package config

import (
"os"

"github.com/ignite/cli/ignite/pkg/env"
"github.com/ignite/cli/ignite/pkg/xfilepath"
)

// DirPath returns the path of configuration directory of Ignite.
var DirPath = xfilepath.JoinFromHome(xfilepath.Path(".ignite"))

// CreateConfigDir creates config directory if it is not created yet.
func CreateConfigDir() error {
path, err := DirPath()
if err != nil {
return err
}

return os.MkdirAll(path, 0o755)
}
var DirPath = xfilepath.Mkdir(env.ConfigDir())
3 changes: 3 additions & 0 deletions ignite/config/plugins/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Plugin struct {
Path string `yaml:"path"`
// With holds arguments passed to the plugin interface
With map[string]string `yaml:"with,omitempty"`
// Global holds whether the plugin is installed globally
// (default: $HOME/.ignite/plugins/plugins.yml) or locally for a chain.
Global bool `yaml:"-"`
}

// Path return the path of the config file.
Expand Down
30 changes: 28 additions & 2 deletions ignite/pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package env

import "os"
import (
"fmt"
"os"
"path"

"github.com/ignite/cli/ignite/pkg/xfilepath"
)

const (
debug = "IGNT_DEBUG"
debug = "IGNT_DEBUG"
configDir = "IGNT_CONFIG_DIR"
)

func DebugEnabled() bool {
return os.Getenv(debug) == "1"
}

func ConfigDir() xfilepath.PathRetriever {
return func() (string, error) {
if dir := os.Getenv(configDir); dir != "" {
if !path.IsAbs(dir) {
panic(fmt.Sprintf("%s must be an absolute path", configDir))
}
return dir, nil
}
return xfilepath.JoinFromHome(xfilepath.Path(".ignite"))()
}
}

func SetConfigDir(dir string) {
err := os.Setenv(configDir, dir)
if err != nil {
panic(fmt.Sprintf("set config dir env: %v", err))
}
}
54 changes: 32 additions & 22 deletions ignite/pkg/xfilepath/xfilepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ func PathWithError(path string, err error) PathRetriever {
// The returned path retriever eventually returns the error from the first provided path retrievers
// that returns a non-nil error.
func Join(paths ...PathRetriever) PathRetriever {
var components []string
var err error
for _, path := range paths {
var component string
component, err = path()
if err != nil {
break
}
components = append(components, component)
}
path := filepath.Join(components...)

return func() (string, error) {
var components []string
var err error
for _, path := range paths {
var component string
component, err = path()
if err != nil {
break
}
components = append(components, component)
}
path := filepath.Join(components...)
return path, err
}
}
Expand All @@ -52,18 +51,29 @@ func JoinFromHome(paths ...PathRetriever) PathRetriever {
// List returns a paths retriever from a list of path retrievers.
// The returned paths retriever eventually returns the error from the first provided path retrievers that returns a non-nil error.
func List(paths ...PathRetriever) PathsRetriever {
var list []string
var err error
for _, path := range paths {
var resolved string
resolved, err = path()
if err != nil {
break
return func() ([]string, error) {
var list []string
var err error
for _, path := range paths {
var resolved string
resolved, err = path()
if err != nil {
break
}
list = append(list, resolved)
}
list = append(list, resolved)
}

return func() ([]string, error) {
return list, err
}
}

// Mkdir ensure path exists before returning it.
func Mkdir(path PathRetriever) PathRetriever {
return func() (string, error) {
p, err := path()
if err != nil {
return "", err
}
return p, os.MkdirAll(p, 0o755)
}
}
14 changes: 14 additions & 0 deletions ignite/pkg/xfilepath/xfilepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package xfilepath_test
import (
"errors"
"os"
"path"
"path/filepath"
"testing"

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

"github.com/ignite/cli/ignite/pkg/xfilepath"
Expand Down Expand Up @@ -88,3 +90,15 @@ func TestList(t *testing.T) {
_, err = retriever()
require.Error(t, err)
}

func TestMkdir(t *testing.T) {
require := require.New(t)
assert := assert.New(t)
newdir := path.Join(t.TempDir(), "hey")

dir, err := xfilepath.Mkdir(xfilepath.Path(newdir))()

require.NoError(err)
assert.Equal(newdir, dir)
assert.DirExists(dir)
}
Loading