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

This commit is contained in:
Binaek Sarkar
2023-09-18 20:57:17 +05:30
committed by GitHub
parent 671baeb36a
commit c85b6c336f
5 changed files with 70 additions and 24 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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",

View File

@@ -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 (

View File

@@ -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"