From c85b6c336ffcc10141530b5331a01cf4fa14bfc4 Mon Sep 17 00:00:00 2001 From: Binaek Sarkar Date: Mon, 18 Sep 2023 20:57:17 +0530 Subject: [PATCH] Adds support for installing all referenced plugins when no arguments are given to 'plugin install'. Closes #3451 --- cmd/plugin.go | 19 ++++++---- pkg/ociinstaller/imageref.go | 37 +++++++++++++++---- pkg/ociinstaller/imageref_test.go | 26 +++++++++++++ pkg/steampipeconfig/modconfig/rate_limiter.go | 5 ++- tests/acceptance/test_files/exit_codes.bats | 7 ---- 5 files changed, 70 insertions(+), 24 deletions(-) diff --git a/cmd/plugin.go b/cmd/plugin.go index cb9fc1d6c..7d6ad5e3e 100644 --- a/cmd/plugin.go +++ b/cmd/plugin.go @@ -13,6 +13,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/turbot/go-kit/helpers" + "github.com/turbot/steampipe-plugin-sdk/v5/sperr" "github.com/turbot/steampipe/pkg/cmdconfig" "github.com/turbot/steampipe/pkg/constants" "github.com/turbot/steampipe/pkg/contexthelpers" @@ -244,13 +245,17 @@ func runPluginInstallCmd(cmd *cobra.Command, args []string) { installReports := make(display.PluginInstallReports, 0, len(plugins)) if len(plugins) == 0 { - fmt.Println() - error_helpers.ShowError(ctx, fmt.Errorf("you need to provide at least one plugin to install")) - fmt.Println() - _ = cmd.Help() - fmt.Println() - exitCode = constants.ExitCodeInsufficientOrWrongInputs - return + if len(steampipeconfig.GlobalConfig.Plugins) == 0 { + error_helpers.ShowError(ctx, sperr.New("No connections or plugins configured")) + exitCode = constants.ExitCodeInsufficientOrWrongInputs + return + } + + // get the list of plugins to install + for imageRef := range steampipeconfig.GlobalConfig.Plugins { + ref := ociinstaller.NewSteampipeImageRef(imageRef) + plugins = append(plugins, ref.GetFriendlyName()) + } } // a leading blank line - since we always output multiple lines diff --git a/pkg/ociinstaller/imageref.go b/pkg/ociinstaller/imageref.go index 8c5899673..998ddd4df 100644 --- a/pkg/ociinstaller/imageref.go +++ b/pkg/ociinstaller/imageref.go @@ -95,16 +95,37 @@ func (r *SteampipeImageRef) GetOrgNameAndStream() (string, string, string) { return strings.Join(split[0:len(split)-2], "/"), pluginNameAndStream[0], pluginNameAndStream[1] } -// GetFriendlyName returns a friendly name: -// hub.steampipe.io/plugins/turbot/aws@1.0.0 => aws@1.0.0 -// hub.steampipe.io/plugins/turbot/aws@latest => aws +// GetFriendlyName returns the minimum friendly name so that the original name can be rebuilt using preset defaults: +// hub.steampipe.io/plugins/turbot/aws@1.0.0 => aws@1.0.0 +// hub.steampipe.io/plugins/turbot/aws@latest => aws +// hub.steampipe.io/plugins/otherOrg/aws@latest => otherOrg/aws +// hub.steampipe.io/plugins/otherOrg/aws@1.0.0 => otherOrg/aws@1.0.0 +// differentRegistry.com/otherOrg/aws@latest => differentRegistry.com/otherOrg/aws@latest +// differentRegistry.com/otherOrg/aws@1.0.0 => differentRegistry.com/otherOrg/aws@1.0.0 func (r *SteampipeImageRef) GetFriendlyName() string { - _, pluginName, pluginStream := r.GetOrgNameAndStream() - if pluginStream == DefaultImageTag { - return pluginName - } else { - return fmt.Sprintf("%s@%s", pluginName, pluginStream) + return getCondensedImageRef(r.DisplayImageRef()) +} + +func getCondensedImageRef(imageRef string) string { + // if this is not from the default steampipe registry - DO NOT CONDENSE - return as is + // (we are not aware of any conventions in the registry) + if !strings.HasPrefix(imageRef, DefaultImageRepoDisplayURL) { + return imageRef } + + // So this is an image reference from the Steampipe HUB registry + // remove the registry URL + ref := strings.TrimPrefix(imageRef, DefaultImageRepoDisplayURL) + // remove the 'plugins' namespace where steampipe hub keeps the images + ref = strings.TrimPrefix(ref, "/plugins/") + // remove the default organization - "turbot" + ref = strings.TrimPrefix(ref, DefaultImageOrg) + // remove any leading '/' + ref = strings.TrimPrefix(ref, "/") + // remove the '@latest' tag (not others) + ref = strings.TrimSuffix(ref, fmt.Sprintf("@%s", DefaultImageTag)) + + return ref } // possible formats include diff --git a/pkg/ociinstaller/imageref_test.go b/pkg/ociinstaller/imageref_test.go index 90a7d1321..269700e43 100644 --- a/pkg/ociinstaller/imageref_test.go +++ b/pkg/ociinstaller/imageref_test.go @@ -4,6 +4,32 @@ import ( "testing" ) +func TestFriendlyImageRef(t *testing.T) { + cases := map[string]string{ + "hub.steampipe.io/plugins/turbot/aws@latest": "aws", + "turbot/aws@latest": "aws", + "aws@latest": "aws", + "hub.steampipe.io/plugins/turbot/aws@1.0.0": "aws@1.0.0", + "hub.steampipe.io/plugins/otherOrg/aws@latest": "otherOrg/aws", + "otherOrg/aws@latest": "otherOrg/aws", + "hub.steampipe.io/plugins/otherOrg/aws@1.0.0": "otherOrg/aws@1.0.0", + "otherOrg/aws@1.0.0": "otherOrg/aws@1.0.0", + "differentRegistry.com/otherOrg/aws@latest": "differentRegistry.com/otherOrg/aws@latest", + "differentRegistry.com/otherOrg/aws@1.0.0": "differentRegistry.com/otherOrg/aws@1.0.0", + } + + for testCase, want := range cases { + t.Run(testCase, func(t *testing.T) { + r := NewSteampipeImageRef(testCase) + + if got := r.GetFriendlyName(); got != want { + t.Errorf("TestFriendlyImageRef failed for case '%s': expected %s, got %s", testCase, want, got) + } + }) + } + +} + func TestActualImageRef(t *testing.T) { cases := map[string]string{ "us-docker.pkg.dev/steampipe/plugin/turbot/aws:1.0.0": "us-docker.pkg.dev/steampipe/plugin/turbot/aws:1.0.0", diff --git a/pkg/steampipeconfig/modconfig/rate_limiter.go b/pkg/steampipeconfig/modconfig/rate_limiter.go index 2f177bd2a..d46cf454e 100644 --- a/pkg/steampipeconfig/modconfig/rate_limiter.go +++ b/pkg/steampipeconfig/modconfig/rate_limiter.go @@ -1,12 +1,13 @@ package modconfig import ( + "sort" + "strings" + "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" "github.com/turbot/steampipe/pkg/ociinstaller" - "sort" - "strings" ) const ( diff --git a/tests/acceptance/test_files/exit_codes.bats b/tests/acceptance/test_files/exit_codes.bats index c4eb1ca1e..942bab4ce 100644 --- a/tests/acceptance/test_files/exit_codes.bats +++ b/tests/acceptance/test_files/exit_codes.bats @@ -16,13 +16,6 @@ load "$LIB_BATS_SUPPORT/load.bash" [ $status -ne 0 ] } -@test "steampipe plugin command fail with insufficient arguments" { - # this should return a non 0 exit code, due to insufficient args - run steampipe plugin install - echo $status - [ $status -ne 0 ] -} - @test "steampipe query pass with 0 exit code" { # this query should pass and return a 0 exit code run steampipe query "select 1"