Skip to content

Commit

Permalink
Merge branch 'v0.22.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidaguerre committed Apr 5, 2024
2 parents b5fffc4 + d4f7304 commit f760871
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 32 deletions.
24 changes: 24 additions & 0 deletions pkg/filepaths/plugin_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package filepaths

import "strings"

func PluginAliasToShortName(pluginName string) string {
// remove prefix
split := strings.Split(pluginName, "/")
pluginName = split[len(split)-1]

if strings.HasPrefix(pluginName, "steampipe-plugin-") {
return strings.TrimPrefix(pluginName, "steampipe-plugin-")
}
return pluginName
}
func PluginAliasToLongName(pluginName string) string {
// remove prefix
split := strings.Split(pluginName, "/")
pluginName = split[len(split)-1]

if !strings.HasPrefix(pluginName, "steampipe-plugin-") {
return "steampipe-plugin-" + pluginName
}
return pluginName
}
22 changes: 22 additions & 0 deletions pkg/filepaths/steampipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ func EnsurePluginDir() string {
return ensureSteampipeSubDir("plugins")
}

func EnsurePluginInstallDir(pluginImageDisplayRef string) string {
installDir := PluginInstallDir(pluginImageDisplayRef)

if _, err := os.Stat(installDir); os.IsNotExist(err) {
err = os.MkdirAll(installDir, 0755)
error_helpers.FailOnErrorWithMessage(err, "could not create plugin install directory")
}

return installDir
}

func PluginInstallDir(pluginImageDisplayRef string) string {
osSafePath := filepath.FromSlash(pluginImageDisplayRef )

fullPath := filepath.Join(EnsurePluginDir(), osSafePath)
return fullPath
}

func PluginBinaryPath(pluginImageDisplayRef, pluginAlias string) string {
return filepath.Join(PluginInstallDir(pluginImageDisplayRef), PluginAliasToLongName(pluginAlias)+".plugin")
}

// EnsureConfigDir returns the path to the config directory (creates if missing)
func EnsureConfigDir() string {
return ensureSteampipeSubDir("config")
Expand Down
17 changes: 2 additions & 15 deletions pkg/ociinstaller/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"
"time"

"github.com/turbot/steampipe/pkg/error_helpers"
"github.com/turbot/steampipe/pkg/filepaths"
versionfile "github.com/turbot/steampipe/pkg/ociinstaller/versionfile"
"github.com/turbot/steampipe/pkg/utils"
Expand Down Expand Up @@ -109,7 +108,7 @@ func updatePluginVersionFiles(ctx context.Context, image *SteampipeImage) error

func installPluginBinary(image *SteampipeImage, tempdir string) error {
sourcePath := filepath.Join(tempdir, image.Plugin.BinaryFile)
destDir := pluginInstallDir(image.ImageRef)
destDir := filepaths.EnsurePluginInstallDir(image.ImageRef.DisplayImageRef())

// check if system is M1 - if so we need some special handling
isM1, err := utils.IsMacM1()
Expand All @@ -136,7 +135,7 @@ func installPluginBinary(image *SteampipeImage, tempdir string) error {
}

func installPluginDocs(image *SteampipeImage, tempdir string) error {
installTo := pluginInstallDir(image.ImageRef)
installTo := filepaths.EnsurePluginInstallDir(image.ImageRef.DisplayImageRef())

// if DocsDir is not set, then there are no docs.
if image.Plugin.DocsDir == "" {
Expand Down Expand Up @@ -230,15 +229,3 @@ func addPluginStreamToConfig(src []byte, ref *SteampipeImageRef) []byte {
return destBuffer.Bytes()
}

func pluginInstallDir(ref *SteampipeImageRef) string {
osSafePath := filepath.FromSlash(ref.DisplayImageRef())

fullPath := filepath.Join(filepaths.EnsurePluginDir(), osSafePath)

if _, err := os.Stat(fullPath); os.IsNotExist(err) {
err = os.MkdirAll(fullPath, 0755)
error_helpers.FailOnErrorWithMessage(err, "could not create plugin install directory")
}

return fullPath
}
24 changes: 18 additions & 6 deletions pkg/ociinstaller/versionfile/plugin_version_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,24 @@ func loadLocalPlugins(ctx context.Context) (map[string]*InstalledVersion, error)
for _, pluginFolder := range pluginFolders {
// check if the folder contains a plugin file
pluginName := filepath.Base(pluginFolder)
pluginFile := filepath.Join(pluginFolder, pluginName+".plugin")
if filehelpers.FileExists(pluginFile) {
localPlugins[pluginName] = &InstalledVersion{
Name: pluginName,
Version: "local",
StructVersion: InstalledVersionStructVersion,

pluginShortName := filepaths.PluginAliasToShortName(pluginName)
pluginLongName := filepaths.PluginAliasToLongName(pluginName)

pluginFiles := []string{
pluginShortName + ".plugin",
pluginLongName + ".plugin",
}
// check both short and long names

for _, pluginFile := range pluginFiles {
pluginPath := filepath.Join(pluginFolder, pluginFile)
if filehelpers.FileExists(pluginPath) {
localPlugins[pluginName] = &InstalledVersion{
Name: pluginPath,
Version: "local",
StructVersion: InstalledVersionStructVersion,
}
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/steampipeconfig/modconfig/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,12 @@ func (l *Plugin) Equals(other *Plugin) bool {
// ResolvePluginImageRef resolves the plugin image ref from the plugin alias
// (this handles the special case of locally developed plugins in the plugins/local folder)
func ResolvePluginImageRef(pluginAlias string) string {
var imageRef string
if strings.HasPrefix(pluginAlias, `local/`) {
imageRef = pluginAlias
} else {
// ok so there is no plugin block reference - build the plugin image ref from the PluginAlias field
imageRef = ociinstaller.NewSteampipeImageRef(pluginAlias).DisplayImageRef()
// if a local plugin is specified, return the plugin alias as the image ref.
// this will be used as the path to the plugin in the local folder
return pluginAlias
}
// are there any instances for this plugin
return imageRef
// ok so there is no plugin block reference - build the plugin image ref from the PluginAlias field
return ociinstaller.NewSteampipeImageRef(pluginAlias).DisplayImageRef()
}

18 changes: 14 additions & 4 deletions pkg/steampipeconfig/steampipeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ func (c *SteampipeConfig) addPlugin(plugin *modconfig.Plugin) error {
log.Printf("[WARN] addPlugin called for plugin '%s' which is not installed", imageRef)
return nil
}
// NOTE: populate the version from the plugin version file data
// populate the version from the plugin version file data
plugin.Version = pluginVersion.Version

plugin.Version = pluginVersion.Version
// add to list of plugin configs for this image ref
c.Plugins[imageRef] = append(c.Plugins[imageRef], plugin)
c.PluginsInstances[plugin.Instance] = plugin
Expand Down Expand Up @@ -399,8 +399,18 @@ func (c *SteampipeConfig) resolvePluginInstanceForConnection(connection *modconf

// verify the plugin is installed - if not return nil
if _, ok := c.PluginVersions[imageRef]; !ok {
log.Printf("[INFO] plugin '%s' is not installed", imageRef)
return nil, nil
// tactical - check if the plugin binary exists
pluginBinaryPath := filepaths.PluginBinaryPath(imageRef, connection.PluginAlias)
if _, err := os.Stat(pluginBinaryPath); err != nil {
log.Printf("[INFO] plugin '%s' is not installed", imageRef)
return nil, nil
}

// so the plugin binary exists but it does not exist in the versions.json
// this is probably because it has been built locally - add a version entry with version set to 'local'
c.PluginVersions[imageRef] = &versionfile.InstalledVersion{
Version: "local",
}
}

// how many plugin instances are there for this image ref?
Expand Down

0 comments on commit f760871

Please sign in to comment.