Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for installing all referenced plugins when no arguments are given to 'plugin install'. Closes #3451 #3842

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
37 changes: 29 additions & 8 deletions pkg/ociinstaller/imageref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
kaidaguerre marked this conversation as resolved.
Show resolved Hide resolved
ref = strings.TrimSuffix(ref, fmt.Sprintf("@%s", DefaultImageTag))

return ref
}

// possible formats include
Expand Down
26 changes: 26 additions & 0 deletions pkg/ociinstaller/imageref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions pkg/steampipeconfig/modconfig/rate_limiter.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
7 changes: 0 additions & 7 deletions tests/acceptance/test_files/exit_codes.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading