diff --git a/cmd/plugin.go b/cmd/plugin.go index b0d1e92657..8295d72f04 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 9e0caae89c..2c02d6d20c 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 0000000000..e08f938391 --- /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 5e62a51da3..f16ca495db 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 7eecbb9228..6384d33667 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 873d130223..c92e3805df 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