feat: add latest version and outdated flag to plugin list command

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
pull/13514/head
Suleiman Dibirov 10 months ago
parent 0199b748aa
commit 437ddbbbd7

@ -18,14 +18,22 @@ package cmd
import (
"fmt"
"io"
"path/filepath"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/plugin/installer"
"helm.sh/helm/v4/pkg/plugin"
)
type pluginListOptions struct {
showOutdated bool
}
func newPluginListCmd(out io.Writer) *cobra.Command {
o := &pluginListOptions{}
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
@ -39,14 +47,24 @@ func newPluginListCmd(out io.Writer) *cobra.Command {
}
table := uitable.New()
table.AddRow("NAME", "VERSION", "DESCRIPTION")
table.AddRow("NAME", "VERSION", "LATEST", "DESCRIPTION")
for _, p := range plugins {
table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description)
latest, err := getLatestVersion(p)
if err != nil {
latest = "unknown"
}
if !o.showOutdated || (latest != "unknown" && latest != p.Metadata.Version) {
table.AddRow(p.Metadata.Name, p.Metadata.Version, latest, p.Metadata.Description)
}
}
fmt.Fprintln(out, table)
return nil
},
}
cmd.Flags().BoolVar(&o.showOutdated, "outdated", false, "show only outdated plugins")
return cmd
}
@ -86,3 +104,22 @@ func compListPlugins(_ string, ignoredPluginNames []string) []string {
}
return pNames
}
// getLatestVersion returns the latest version of a plugin
func getLatestVersion(p *plugin.Plugin) (string, error) {
exactLocation, err := filepath.EvalSymlinks(p.Dir)
if err != nil {
return "", err
}
absExactLocation, err := filepath.Abs(exactLocation)
if err != nil {
return "", err
}
i, err := installer.FindSource(absExactLocation)
if err != nil {
return "", err
}
return i.GetLatestVersion()
}

@ -155,6 +155,11 @@ func (i *HTTPInstaller) Update() error {
return errors.Errorf("method Update() not implemented for HttpInstaller")
}
// GetLatestVersion fetches the latest version of the plugin.
func (i *HTTPInstaller) GetLatestVersion() (string, error) {
return "", errors.New("not supported")
}
// Path is overridden because we want to join on the plugin name not the file name
func (i HTTPInstaller) Path() string {
if i.base.Source == "" {

@ -40,6 +40,8 @@ type Installer interface {
Path() string
// Update updates a plugin.
Update() error
// GetLatestVersion fetches the latest version of the plugin.
GetLatestVersion() (string, error)
}
// Install installs a plugin.

@ -67,3 +67,7 @@ func (i *LocalInstaller) Update() error {
slog.Debug("local repository is auto-updated")
return nil
}
func (i *LocalInstaller) GetLatestVersion() (string, error) {
return "", errors.New("not supported")
}

@ -108,6 +108,26 @@ func (i *VCSInstaller) Update() error {
return nil
}
func (i *VCSInstaller) GetLatestVersion() (string, error) {
_, err := i.Repo.RunFromDir("git", "fetch", "-q", "--tags")
if err != nil {
return "", fmt.Errorf("failed to fetch tags: %w", err)
}
refs, err := i.Repo.Tags()
if err != nil {
return "", fmt.Errorf("failed to get tags: %w", err)
}
semvers := getSemVers(refs)
if len(semvers) == 0 {
return "", fmt.Errorf("no valid semver tags found in repository")
}
sort.Sort(sort.Reverse(semver.Collection(semvers)))
return semvers[0].Original(), nil
}
func (i *VCSInstaller) solveVersion(repo vcs.Repo) (string, error) {
if i.Version == "" {
return "", nil

Loading…
Cancel
Save