Skip to content

Commit eac217c

Browse files
authored
feat: Implement API to get latest plugin version. (#415)
Prerequisite to fully implement: cloudquery/cloudquery#19286 In order to let CLI users when the plugins they are using are not up to date, we need to now what the latest version is. This PR only adds this functionality within `plugin-pb-go` within the `managedplugin` module, but it isn't yet used, nor compared to the user's plugin version. Sample check on CLI to see if it works: ``` $ go run . test-connection /Users/mariano.gappa/Code/test-aws-sync/all.yml Loading spec(s) from /Users/mariano.gappa/Code/test-aws-sync/all.yml You chose version v27.23.0. Latest version is v27.23.1. ``` Also note that this implementation errors in a lot of cases, but it is not the intention to actually error to the client; since the purpose is to show a warning, we'll probably elide any warnings (it's debatable) if there's no sufficient information to show a warning.
1 parent e02834b commit eac217c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

managedplugin/plugin.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818

1919
"github.com/avast/retry-go/v4"
20+
cloudquery_api "github.com/cloudquery/cloudquery-api-go"
2021
pbBase "github.com/cloudquery/plugin-pb-go/pb/base/v0"
2122
pbDiscovery "github.com/cloudquery/plugin-pb-go/pb/discovery/v0"
2223
pbDiscoveryV1 "github.com/cloudquery/plugin-pb-go/pb/discovery/v1"
@@ -686,6 +687,55 @@ func (c *Client) Versions(ctx context.Context) ([]int, error) {
686687
return res, nil
687688
}
688689

690+
func (c *Client) FindLatestPluginVersion(ctx context.Context, typ PluginType) (string, error) {
691+
if c.config.Registry != RegistryCloudQuery {
692+
return "", fmt.Errorf("plugin registry is not cloudquery; cannot find latest plugin version")
693+
}
694+
695+
if c.teamName == "" {
696+
return "", fmt.Errorf("team name is required to find the latest plugin version")
697+
}
698+
699+
pathSplit := strings.Split(c.config.Path, "/")
700+
if len(pathSplit) != 2 {
701+
return "", fmt.Errorf("invalid cloudquery plugin path: %s. format should be team/name", c.config.Path)
702+
}
703+
org, name := pathSplit[0], pathSplit[1]
704+
705+
if org != "cloudquery" {
706+
return "", fmt.Errorf("plugin org is not cloudquery; cannot find latest plugin version")
707+
}
708+
709+
ops := HubDownloadOptions{
710+
AuthToken: c.authToken,
711+
TeamName: c.teamName,
712+
LocalPath: c.LocalPath,
713+
PluginTeam: org,
714+
PluginKind: typ.String(),
715+
PluginName: name,
716+
PluginVersion: c.config.Version,
717+
}
718+
hubClient, err := getHubClient(c.logger, ops)
719+
if err != nil {
720+
return "", fmt.Errorf("failed to get hub client: %w", err)
721+
}
722+
723+
resp, err := hubClient.GetPluginWithResponse(ctx, ops.PluginTeam, cloudquery_api.PluginKind(ops.PluginKind), ops.PluginName)
724+
if err != nil {
725+
return "", fmt.Errorf("failed to get plugin: %w", err)
726+
}
727+
728+
if resp.JSON200 == nil {
729+
return "", fmt.Errorf("failed to get latest plugin version: %w", err)
730+
}
731+
732+
if resp.JSON200.LatestVersion == nil {
733+
return "", nil // It's possible to have no latest version (unpublished plugins)
734+
}
735+
736+
return *resp.JSON200.LatestVersion, nil
737+
}
738+
689739
func (c *Client) MaxVersion(ctx context.Context) (int, error) {
690740
discoveryClient := pbDiscovery.NewDiscoveryClient(c.Conn)
691741
versionsRes, err := discoveryClient.GetVersions(ctx, &pbDiscovery.GetVersions_Request{})

0 commit comments

Comments
 (0)