From ce2bc0cb5b9d3d2f1fbec8db1982a4453f8d2e76 Mon Sep 17 00:00:00 2001 From: Meet Rajesh Gor <40317114+Mr-Destructive@users.noreply.github.com> Date: Fri, 28 Jul 2023 14:45:37 +0530 Subject: [PATCH] Add flag for disabling writing of default plugin config during plugin installation. Closes #3531. Closes #2206 --- cmd/plugin.go | 8 ++- pkg/constants/args.go | 1 + pkg/ociinstaller/config.go | 13 +++++ pkg/ociinstaller/plugin.go | 13 +++-- pkg/plugin/actions.go | 4 +- .../test_files/service_and_plugin.bats | 55 +++++++++++++++++++ 6 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 pkg/ociinstaller/config.go diff --git a/cmd/plugin.go b/cmd/plugin.go index b0d1e9265..8295d72f0 100644 --- a/cmd/plugin.go +++ b/cmd/plugin.go @@ -109,12 +109,16 @@ Examples: steampipe plugin install turbot/azure@0.1.0 # Hide progress bars during installation - steampipe plugin install --progress=false aws`, + steampipe plugin install --progress=false aws + + # Skip creation of default plugin config file + steampipe plugin install --skip-config aws`, } cmdconfig. OnCmd(cmd). AddBoolFlag(constants.ArgProgress, true, "Display installation progress"). + AddBoolFlag(constants.ArgSkipConfig, false, "Skip creating the default config file for plugin"). AddBoolFlag(constants.ArgHelp, false, "Help for plugin install", cmdconfig.FlagOptions.WithShortHand("h")) return cmd } @@ -542,7 +546,7 @@ func installPlugin(ctx context.Context, pluginName string, isUpdate bool, bar *u } }() - image, err := plugin.Install(ctx, pluginName, progress) + image, err := plugin.Install(ctx, pluginName, progress, ociinstaller.WithSkipConfig(viper.GetBool(constants.ArgSkipConfig))) if err != nil { msg := "" _, name, stream := ociinstaller.NewSteampipeImageRef(pluginName).GetOrgNameAndStream() diff --git a/pkg/constants/args.go b/pkg/constants/args.go index 9e0caae89..2c02d6d20 100644 --- a/pkg/constants/args.go +++ b/pkg/constants/args.go @@ -18,6 +18,7 @@ const ( ArgDashboard = "dashboard" ArgDashboardListen = "dashboard-listen" ArgDashboardPort = "dashboard-port" + ArgSkipConfig = "skip-config" ArgForeground = "foreground" ArgInvoker = "invoker" ArgUpdateCheck = "update-check" diff --git a/pkg/ociinstaller/config.go b/pkg/ociinstaller/config.go new file mode 100644 index 000000000..e08f93839 --- /dev/null +++ b/pkg/ociinstaller/config.go @@ -0,0 +1,13 @@ +package ociinstaller + +type pluginInstallConfig struct { + skipConfigFile bool +} + +type PluginInstallOption = func(config *pluginInstallConfig) + +func WithSkipConfig(skipConfigFile bool) PluginInstallOption { + return func(o *pluginInstallConfig) { + o.skipConfigFile = skipConfigFile + } +} diff --git a/pkg/ociinstaller/plugin.go b/pkg/ociinstaller/plugin.go index 5e62a51da..f16ca495d 100644 --- a/pkg/ociinstaller/plugin.go +++ b/pkg/ociinstaller/plugin.go @@ -22,7 +22,11 @@ import ( var versionFileUpdateLock = &sync.Mutex{} // InstallPlugin installs a plugin from an OCI Image -func InstallPlugin(ctx context.Context, imageRef string, sub chan struct{}) (*SteampipeImage, error) { +func InstallPlugin(ctx context.Context, imageRef string, sub chan struct{}, opts ...PluginInstallOption) (*SteampipeImage, error) { + config := &pluginInstallConfig{} + for _, opt := range opts { + opt(config) + } tempDir := NewTempDir(filepaths.EnsurePluginDir()) defer func() { // send a last beacon to signal completion @@ -49,9 +53,10 @@ func InstallPlugin(ctx context.Context, imageRef string, sub chan struct{}) (*St if err = installPluginDocs(image, tempDir.Path); err != nil { return nil, fmt.Errorf("plugin installation failed: %s", err) } - sub <- struct{}{} - if err = installPluginConfigFiles(image, tempDir.Path); err != nil { - return nil, fmt.Errorf("plugin installation failed: %s", err) + if !config.skipConfigFile { + if err = installPluginConfigFiles(image, tempDir.Path); err != nil { + return nil, fmt.Errorf("plugin installation failed: %s", err) + } } sub <- struct{}{} if err := updatePluginVersionFiles(image); err != nil { diff --git a/pkg/plugin/actions.go b/pkg/plugin/actions.go index 7eecbb922..6384d3366 100644 --- a/pkg/plugin/actions.go +++ b/pkg/plugin/actions.go @@ -71,8 +71,8 @@ func Exists(plugin string) (bool, error) { } // Install installs a plugin in the local file system -func Install(ctx context.Context, plugin string, sub chan struct{}) (*ociinstaller.SteampipeImage, error) { - image, err := ociinstaller.InstallPlugin(ctx, plugin, sub) +func Install(ctx context.Context, plugin string, sub chan struct{}, opts ...ociinstaller.PluginInstallOption) (*ociinstaller.SteampipeImage, error) { + image, err := ociinstaller.InstallPlugin(ctx, plugin, sub, opts...) return image, err } diff --git a/tests/acceptance/test_files/service_and_plugin.bats b/tests/acceptance/test_files/service_and_plugin.bats index 873d13022..c92e3805d 100644 --- a/tests/acceptance/test_files/service_and_plugin.bats +++ b/tests/acceptance/test_files/service_and_plugin.bats @@ -721,6 +721,61 @@ load "$LIB_BATS_SUPPORT/load.bash" rm -rf $tmpdir } +@test "verify that plugin installed with --skip-config as true, should not have create a default config .spc file in config folder" { + tmpdir=$(mktemp -d) + run steampipe plugin install aws --skip-config --install-dir $tmpdir + assert_success + + run test -f $tmpdir/config/aws.spc + assert_failure + + rm -rf $tmpdir +} + +@test "verify that plugin installed with --skip-config as false(default), should have default config .spc file in config folder" { + tmpdir=$(mktemp -d) + run steampipe plugin install aws --install-dir $tmpdir + assert_success + + run test -f $tmpdir/config/aws.spc + assert_success + + rm -rf $tmpdir +} + +@test "verify reinstalling a plugin does not overwrite existing plugin config" { + # check if the default/tweaked config file for a plugin is not deleted after + # re-installation of a plugin + + tmpdir=$(mktemp -d) + + run steampipe plugin install aws --install-dir $tmpdir + + run test -f $tmpdir/config/aws.spc + assert_success + + echo ' + connection "aws" { + plugin = "aws" + endpoint_url = "http://localhost:4566" + } + ' >> $tmpdir/config/aws.spc + cp $tmpdir/config/aws.spc config.spc + + run steampipe plugin uninstall aws --install-dir $tmpdir + + run steampipe plugin install aws --skip-config --install-dir $tmpdir + + run test -f $tmpdir/config/aws.spc + assert_success + + run diff $tmpdir/config/aws.spc config.spc + assert_success + + rm config.spc + rm -rf $tmpdir +} + @test "cleanup" { rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_agg.spc run steampipe plugin uninstall steampipe