-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: separate
plugins.yml
file (#3184)
* base * base refactor * refactor * rename * rename * fix comment * rename file * format * refactor base * format * refactor some imports * imports refactor * fix * fix import * refactor: * changelog * changelog * Update ignite/services/network/networkchain/init.go Co-authored-by: Jerónimo Albi <[email protected]> * base -> baseconfig * refactoring for clarity * v12 -> v1 * fix tests * fix integration * fix name * fix integration * format * Update ignite/cmd/cmd.go Co-authored-by: Jerónimo Albi <[email protected]> * imports * LocateDefault logic * lint fix * refactor and test * finish refactor * revert * address review * testdata Co-authored-by: Jerónimo Albi <[email protected]>
- Loading branch information
1 parent
1d2517f
commit 1317723
Showing
16 changed files
with
267 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package plugins | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/imdario/mergo" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// PluginsConfigFilenames is a list of recognized names as Ignite's plugins config file. | ||
var PluginsConfigFilenames = []string{"plugins.yml", "plugins.yaml"} | ||
|
||
// DefaultConfig returns a config with default values. | ||
func DefaultConfig() *Config { | ||
c := Config{} | ||
return &c | ||
} | ||
|
||
// LocateDefault locates the default path for the config file. | ||
// Returns ErrConfigNotFound when no config file found. | ||
func LocateDefault(root string) (path string, err error) { | ||
for _, name := range PluginsConfigFilenames { | ||
path = filepath.Join(root, name) | ||
if _, err := os.Stat(path); err == nil { | ||
return path, nil | ||
} else if !os.IsNotExist(err) { | ||
return "", err | ||
} | ||
} | ||
|
||
return "", ErrConfigNotFound | ||
} | ||
|
||
type Config struct { | ||
Plugins []Plugin `yaml:"plugins,omitempty"` | ||
} | ||
|
||
// Plugin keeps plugin name and location. | ||
type Plugin struct { | ||
// Path holds the location of the plugin. | ||
// A path can be local, in that case it must start with a `/`. | ||
// A remote path on the other hand, is an URL to a public remote git | ||
// repository. For example: | ||
// | ||
// path: github.com/foo/bar | ||
// | ||
// It can contain a path inside that repository, if for instance the repo | ||
// contains multiple plugins, For example: | ||
// | ||
// path: github.com/foo/bar/plugin1 | ||
// | ||
// It can also specify a tag or a branch, by adding a `@` and the branch/tag | ||
// name at the end of the path. For example: | ||
// | ||
// path: github.com/foo/bar/plugin1@v42 | ||
Path string `yaml:"path"` | ||
// With holds arguments passed to the plugin interface | ||
With map[string]string `yaml:"with"` | ||
} | ||
|
||
// Clone returns an identical copy of the instance | ||
func (c *Config) Clone() (*Config, error) { | ||
copy := Config{} | ||
if err := mergo.Merge(©, c, mergo.WithAppendSlice); err != nil { | ||
return nil, err | ||
} | ||
|
||
return ©, nil | ||
} | ||
|
||
// Decode decodes the config file values from YAML. | ||
func (c *Config) Decode(r io.Reader) error { | ||
return yaml.NewDecoder(r).Decode(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package plugins_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ignite/cli/ignite/config/plugins" | ||
) | ||
|
||
func TestConfigDecode(t *testing.T) { | ||
assert := assert.New(t) | ||
require := require.New(t) | ||
f, err := os.Open("testdata/plugins.yml") | ||
require.NoError(err) | ||
defer f.Close() | ||
var cfg plugins.Config | ||
|
||
err = cfg.Decode(f) | ||
|
||
require.NoError(err) | ||
expected := plugins.Config{ | ||
Plugins: []plugins.Plugin{ | ||
{ | ||
Path: "/path/to/plugin1", | ||
}, | ||
{ | ||
Path: "/path/to/plugin2", | ||
With: map[string]string{"foo": "bar", "bar": "baz"}, | ||
}, | ||
}, | ||
} | ||
assert.Equal(expected, cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package plugins | ||
|
||
import "errors" | ||
|
||
// ErrConfigNotFound indicates that the plugins.yml can't be found. | ||
var ErrConfigNotFound = errors.New("could not locate a plugins.yml") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package plugins | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// ParseFile parses a plugins config. | ||
func ParseFile(path string) (*Config, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return DefaultConfig(), err | ||
} | ||
|
||
defer file.Close() | ||
|
||
return Parse(file) | ||
} | ||
|
||
// Parse reads a config file for ignite binary plugins | ||
func Parse(configFile io.Reader) (*Config, error) { | ||
return parse(configFile) | ||
} | ||
|
||
func parse(configFile io.Reader) (*Config, error) { | ||
var c Config | ||
|
||
err := yaml.NewDecoder(configFile).Decode(&c) | ||
|
||
return &c, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package plugins_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
pluginsconfig "github.com/ignite/cli/ignite/config/plugins" | ||
"github.com/ignite/cli/ignite/config/plugins/testdata" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
// Arrange: Initialize a reader with the previous version | ||
r := bytes.NewReader(testdata.ConfigYAML) | ||
|
||
// Act | ||
cfg, err := pluginsconfig.Parse(r) | ||
|
||
// Assert | ||
require.NoError(t, err) | ||
|
||
// Assert: Parse must return the latest version | ||
require.Equal(t, testdata.GetConfig(t), cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
plugins: | ||
- name: plugin1 | ||
path: /path/to/plugin1 | ||
- name: plugin2 | ||
path: /path/to/plugin2 | ||
with: | ||
foo: bar | ||
bar: baz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package testdata | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v2" | ||
|
||
pluginsconfig "github.com/ignite/cli/ignite/config/plugins" | ||
) | ||
|
||
//go:embed plugins.yml | ||
var ConfigYAML []byte | ||
|
||
func GetConfig(t *testing.T) *pluginsconfig.Config { | ||
c := &pluginsconfig.Config{} | ||
|
||
err := yaml.NewDecoder(bytes.NewReader(ConfigYAML)).Decode(c) | ||
require.NoError(t, err) | ||
|
||
return c | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.