Skip to content

Commit

Permalink
Add flag for disabling writing of default plugin config during plugin…
Browse files Browse the repository at this point in the history
… installation. Closes #3531. Closes #2206
  • Loading branch information
Mr-Destructive committed Jul 28, 2023
1 parent 16a59b6 commit ce2bc0c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
8 changes: 6 additions & 2 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
ArgDashboard = "dashboard"
ArgDashboardListen = "dashboard-listen"
ArgDashboardPort = "dashboard-port"
ArgSkipConfig = "skip-config"
ArgForeground = "foreground"
ArgInvoker = "invoker"
ArgUpdateCheck = "update-check"
Expand Down
13 changes: 13 additions & 0 deletions pkg/ociinstaller/config.go
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 9 additions & 4 deletions pkg/ociinstaller/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
55 changes: 55 additions & 0 deletions tests/acceptance/test_files/service_and_plugin.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ce2bc0c

Please sign in to comment.