Refactor document plugin command

AirshipCTLSettings are not used here, so it was removed.

Change-Id: If4bf64b9991b4ac05898632c79bacbacd7872635
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Relates-To: #327
This commit is contained in:
Ruslan Aliev 2020-08-27 17:45:22 -05:00
parent 95d6536df2
commit 78364d7d44
11 changed files with 18 additions and 26 deletions

View File

@ -34,7 +34,7 @@ func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com
} }
documentRootCmd.AddCommand(NewPullCommand(rootSettings)) documentRootCmd.AddCommand(NewPullCommand(rootSettings))
documentRootCmd.AddCommand(NewPluginCommand(rootSettings)) documentRootCmd.AddCommand(NewPluginCommand())
return documentRootCmd return documentRootCmd
} }

View File

@ -31,12 +31,12 @@ func TestDocument(t *testing.T) {
{ {
Name: "document-plugin-with-help", Name: "document-plugin-with-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: document.NewPluginCommand(nil), Cmd: document.NewPluginCommand(),
}, },
{ {
Name: "document-pull-with-help", Name: "document-pull-with-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: document.NewPluginCommand(nil), Cmd: document.NewPluginCommand(),
}, },
} }
for _, tt := range tests { for _, tt := range tests {

View File

@ -20,7 +20,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/document/plugin" "opendev.org/airship/airshipctl/pkg/document/plugin"
"opendev.org/airship/airshipctl/pkg/environment"
) )
const ( const (
@ -60,7 +59,7 @@ airshipctl document plugin /tmp/replacement.yaml
// NewPluginCommand creates a new command which can act as kustomize // NewPluginCommand creates a new command which can act as kustomize
// exec plugin. // exec plugin.
func NewPluginCommand(rootSetting *environment.AirshipCTLSettings) *cobra.Command { func NewPluginCommand() *cobra.Command {
pluginCmd := &cobra.Command{ pluginCmd := &cobra.Command{
Use: "plugin CONFIG [ARGS]", Use: "plugin CONFIG [ARGS]",
Short: "Run as a kustomize exec plugin", Short: "Run as a kustomize exec plugin",
@ -72,7 +71,7 @@ func NewPluginCommand(rootSetting *environment.AirshipCTLSettings) *cobra.Comman
if err != nil { if err != nil {
return err return err
} }
return plugin.ConfigureAndRun(rootSetting, cfg, cmd.InOrStdin(), cmd.OutOrStdout()) return plugin.ConfigureAndRun(cfg, cmd.InOrStdin(), cmd.OutOrStdout())
}, },
} }
return pluginCmd return pluginCmd

View File

@ -27,13 +27,13 @@ func TestPlugin(t *testing.T) {
Name: "document-plugin-cmd-with-empty-args", Name: "document-plugin-cmd-with-empty-args",
CmdLine: "", CmdLine: "",
Error: fmt.Errorf("requires at least 1 arg(s), only received 0"), Error: fmt.Errorf("requires at least 1 arg(s), only received 0"),
Cmd: NewPluginCommand(nil), Cmd: NewPluginCommand(),
}, },
{ {
Name: "document-plugin-cmd-with-nonexistent-config", Name: "document-plugin-cmd-with-nonexistent-config",
CmdLine: "/some/random/path.yaml", CmdLine: "/some/random/path.yaml",
Error: fmt.Errorf("open /some/random/path.yaml: no such file or directory"), Error: fmt.Errorf("open /some/random/path.yaml: no such file or directory"),
Cmd: NewPluginCommand(nil), Cmd: NewPluginCommand(),
}, },
} }

View File

@ -19,7 +19,6 @@ import (
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types" plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
"opendev.org/airship/airshipctl/pkg/environment"
) )
var ( var (
@ -43,7 +42,7 @@ func GetGVK() schema.GroupVersionKind {
} }
// New creates new instance of the plugin // New creates new instance of the plugin
func New(_ *environment.AirshipCTLSettings, cfg []byte) (plugtypes.Plugin, error) { func New(cfg []byte) (plugtypes.Plugin, error) {
p := &plugin{} p := &plugin{}
if err := p.Config(nil, cfg); err != nil { if err := p.Config(nil, cfg); err != nil {
return nil, err return nil, err

View File

@ -16,7 +16,7 @@ import (
) )
func samplePlugin(t *testing.T) plugtypes.Plugin { func samplePlugin(t *testing.T) plugtypes.Plugin {
plugin, err := replv1alpha1.New(nil, []byte(` plugin, err := replv1alpha1.New([]byte(`
apiVersion: airshipit.org/v1alpha1 apiVersion: airshipit.org/v1alpha1
kind: ReplacementTransformer kind: ReplacementTransformer
metadata: metadata:
@ -35,7 +35,7 @@ replacements:
} }
func TestMalformedConfig(t *testing.T) { func TestMalformedConfig(t *testing.T) {
_, err := replv1alpha1.New(nil, []byte("--")) _, err := replv1alpha1.New([]byte("--"))
assert.Error(t, err) assert.Error(t, err)
} }
@ -909,7 +909,7 @@ spec:
} }
for _, tc := range testCases { for _, tc := range testCases {
plugin, err := replv1alpha1.New(nil, []byte(tc.cfg)) plugin, err := replv1alpha1.New([]byte(tc.cfg))
require.NoError(t, err) require.NoError(t, err)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}

View File

@ -25,7 +25,6 @@ import (
"opendev.org/airship/airshipctl/pkg/document/plugin/replacement" "opendev.org/airship/airshipctl/pkg/document/plugin/replacement"
"opendev.org/airship/airshipctl/pkg/document/plugin/templater" "opendev.org/airship/airshipctl/pkg/document/plugin/templater"
"opendev.org/airship/airshipctl/pkg/document/plugin/types" "opendev.org/airship/airshipctl/pkg/document/plugin/types"
"opendev.org/airship/airshipctl/pkg/environment"
) )
// Registry contains factory functions for the available plugins // Registry contains factory functions for the available plugins
@ -39,7 +38,7 @@ func init() {
// ConfigureAndRun executes particular plugin based on group, version, kind // ConfigureAndRun executes particular plugin based on group, version, kind
// which have been specified in configuration file. Config file should be // which have been specified in configuration file. Config file should be
// supplied as a first element of args slice // supplied as a first element of args slice
func ConfigureAndRun(settings *environment.AirshipCTLSettings, pluginCfg []byte, in io.Reader, out io.Writer) error { func ConfigureAndRun(pluginCfg []byte, in io.Reader, out io.Writer) error {
var cfg unstructured.Unstructured var cfg unstructured.Unstructured
if err := yaml.Unmarshal(pluginCfg, &cfg); err != nil { if err := yaml.Unmarshal(pluginCfg, &cfg); err != nil {
return err return err
@ -49,7 +48,7 @@ func ConfigureAndRun(settings *environment.AirshipCTLSettings, pluginCfg []byte,
return ErrPluginNotFound{PluginID: cfg.GroupVersionKind()} return ErrPluginNotFound{PluginID: cfg.GroupVersionKind()}
} }
plugin, err := pluginFactory(settings, pluginCfg) plugin, err := pluginFactory(pluginCfg)
if err != nil { if err != nil {
return err return err
} }

View File

@ -21,13 +21,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"opendev.org/airship/airshipctl/pkg/document/plugin" "opendev.org/airship/airshipctl/pkg/document/plugin"
"opendev.org/airship/airshipctl/pkg/environment"
) )
func TestConfigureAndRun(t *testing.T) { func TestConfigureAndRun(t *testing.T) {
testCases := []struct { testCases := []struct {
pluginCfg []byte pluginCfg []byte
settings *environment.AirshipCTLSettings
expectedError string expectedError string
in io.Reader in io.Reader
out io.Writer out io.Writer
@ -55,7 +53,7 @@ spec: -
} }
for _, tc := range testCases { for _, tc := range testCases {
err := plugin.ConfigureAndRun(tc.settings, tc.pluginCfg, tc.in, tc.out) err := plugin.ConfigureAndRun(tc.pluginCfg, tc.in, tc.out)
assert.EqualError(t, err, tc.expectedError) assert.EqualError(t, err, tc.expectedError)
} }
} }

View File

@ -24,7 +24,6 @@ import (
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types" plugtypes "opendev.org/airship/airshipctl/pkg/document/plugin/types"
"opendev.org/airship/airshipctl/pkg/environment"
) )
// GetGVK returns group, version, kind object used to register version // GetGVK returns group, version, kind object used to register version
@ -38,7 +37,7 @@ func GetGVK() schema.GroupVersionKind {
} }
// New creates new instance of the plugin // New creates new instance of the plugin
func New(_ *environment.AirshipCTLSettings, cfg []byte) (plugtypes.Plugin, error) { func New(cfg []byte) (plugtypes.Plugin, error) {
t := &Templater{} t := &Templater{}
if err := yaml.Unmarshal(cfg, t); err != nil { if err := yaml.Unmarshal(cfg, t); err != nil {
return nil, err return nil, err

View File

@ -25,7 +25,7 @@ import (
) )
func TestMalformedConfig(t *testing.T) { func TestMalformedConfig(t *testing.T) {
_, err := tmplv1alpha1.New(nil, []byte("--")) _, err := tmplv1alpha1.New([]byte("--"))
assert.Error(t, err) assert.Error(t, err)
} }
@ -119,7 +119,7 @@ template: |
} }
for _, tc := range testCases { for _, tc := range testCases {
plugin, err := tmplv1alpha1.New(nil, []byte(tc.cfg)) plugin, err := tmplv1alpha1.New([]byte(tc.cfg))
require.NoError(t, err) require.NoError(t, err)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err = plugin.Run(nil, buf) err = plugin.Run(nil, buf)

View File

@ -16,8 +16,6 @@ package types
import ( import (
"io" "io"
"opendev.org/airship/airshipctl/pkg/environment"
) )
// Plugin interface for airship document plugins // Plugin interface for airship document plugins
@ -27,4 +25,4 @@ type Plugin interface {
// Factory function for plugins. Functions of such type are used in the plugin // Factory function for plugins. Functions of such type are used in the plugin
// registry to instantiate a plugin object // registry to instantiate a plugin object
type Factory func(*environment.AirshipCTLSettings, []byte) (Plugin, error) type Factory func([]byte) (Plugin, error)