From 00118de5a0ea64180ee46ffd8126fdf9df44becb Mon Sep 17 00:00:00 2001 From: jon-stewart Date: Tue, 20 Feb 2024 11:49:37 +0000 Subject: [PATCH] fix: reduce component API usage --- cli/cmd/component.go | 43 ++++++++++++++++++++++++++--------- cli/cmd/component_dev.go | 2 +- lwcomponent/catalog.go | 7 ------ lwcomponent/catalog_test.go | 44 ++++++++++++++++++------------------ lwcomponent/cdk_component.go | 4 ++++ 5 files changed, 59 insertions(+), 41 deletions(-) diff --git a/cli/cmd/component.go b/cli/cmd/component.go index 54966f680..e00cb78cd 100644 --- a/cli/cmd/component.go +++ b/cli/cmd/component.go @@ -223,7 +223,7 @@ func v1ComponentCommand(c *cliState, cmd *cobra.Command) error { go startGrpcServer(c) - catalog, err := LoadCatalog() + catalog, err := LoadCatalog(cmd.Use, false) if err != nil { return errors.Wrap(err, "unable to load component Catalog") } @@ -360,7 +360,7 @@ func runComponentsList(_ *cobra.Command, _ []string) (err error) { } func listComponents() error { - catalog, err := LoadCatalog() + catalog, err := LoadCatalog("", false) if err != nil { return errors.Wrap(err, "unable to load component Catalog") } @@ -406,10 +406,10 @@ func runComponentsInstall(cmd *cobra.Command, args []string) (err error) { return prototypeRunComponentsInstall(cmd, args) } - return installComponent(cmd, args) + return installComponent(args) } -func installComponent(cmd *cobra.Command, args []string) (err error) { +func installComponent(args []string) (err error) { var ( componentName string = args[0] downloadComplete = make(chan int8) @@ -421,7 +421,7 @@ func installComponent(cmd *cobra.Command, args []string) (err error) { cli.Event.Feature = "install_component" defer cli.SendHoneyvent() - catalog, err := LoadCatalog() + catalog, err := LoadCatalog(componentName, false) if err != nil { err = errors.Wrap(err, "unable to load component Catalog") return @@ -517,7 +517,7 @@ func showComponent(args []string) error { componentName string = args[0] ) - catalog, err := LoadCatalog() + catalog, err := LoadCatalog(componentName, true) if err != nil { return errors.Wrap(err, "unable to load component Catalog") } @@ -586,7 +586,7 @@ func updateComponent(args []string) (err error) { targetVersion *semver.Version ) - catalog, err := LoadCatalog() + catalog, err := LoadCatalog(componentName, false) if err != nil { return errors.Wrap(err, "unable to load component Catalog") } @@ -707,7 +707,7 @@ func deleteComponent(args []string) (err error) { componentName string = args[0] ) - catalog, err := LoadCatalog() + catalog, err := LoadCatalog(componentName, false) if err != nil { return errors.Wrap(err, "unable to load component Catalog") } @@ -1194,7 +1194,7 @@ func downloadProgress(complete chan int8, path string, sizeB int64) { } } -func LoadCatalog() (*lwcomponent.Catalog, error) { +func LoadCatalog(componentName string, getAllVersions bool) (*lwcomponent.Catalog, error) { cli.StartProgress("Loading component catalog...") defer cli.StopProgress() @@ -1203,18 +1203,39 @@ func LoadCatalog() (*lwcomponent.Catalog, error) { // try to load components Catalog from cache if !cli.noCache { expired := cli.ReadCachedAsset(componentsCacheKey, &componentsApiInfo) - if !expired { + if !expired && !getAllVersions { cli.Log.Infow("loaded components from cache", "components", componentsApiInfo) return lwcomponent.NewCachedCatalog(cli.LwApi, lwcomponent.NewStageTarGz, componentsApiInfo) } } // load components Catalog from API - catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz, true) + catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz) if err != nil { return nil, err } + // Retrieve the list of all available versions for a single component + if getAllVersions { + component, err := catalog.GetComponent(componentName) + if err != nil { + return nil, err + } + + vers, err := catalog.ListComponentVersions(component) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("unable to fetch component '%s' versions", componentName)) + } + + component.ApiInfo.AllVersions = vers + catalog.Components[componentName] = lwcomponent.NewCDKComponent( + component.Name, + component.Description, + component.Type, + component.ApiInfo, + component.HostInfo) + } + componentsApiInfo = make(map[string]*lwcomponent.ApiInfo, len(catalog.Components)) for _, c := range catalog.Components { diff --git a/cli/cmd/component_dev.go b/cli/cmd/component_dev.go index f6e149235..20b7b175c 100644 --- a/cli/cmd/component_dev.go +++ b/cli/cmd/component_dev.go @@ -92,7 +92,7 @@ func devModeComponent(args []string) error { cli.StartProgress("Loading components Catalog...") - catalog, err := LoadCatalog() + catalog, err := LoadCatalog(componentName, false) cli.StopProgress() if err != nil { diff --git a/lwcomponent/catalog.go b/lwcomponent/catalog.go index ce8bdea37..43c580db9 100644 --- a/lwcomponent/catalog.go +++ b/lwcomponent/catalog.go @@ -245,7 +245,6 @@ func (c *Catalog) Delete(component *CDKComponent) (err error) { func NewCatalog( client *api.Client, stageConstructor StageConstructor, - includeComponentVersions bool, ) (*Catalog, error) { if stageConstructor == nil { return nil, errors.New("StageConstructor is not specified to create new catalog") @@ -271,12 +270,6 @@ func NewCatalog( } var allVersions []*semver.Version - if includeComponentVersions { - allVersions, err = listComponentVersions(client, c.Id) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("unable to fetch component '%s' versions", c.Name)) - } - } apiInfo := NewAPIInfo(c.Id, c.Name, ver, allVersions, c.Description, c.Size, c.Deprecated, Type(c.ComponentType)) cdkComponents[c.Name] = NewCDKComponent(c.Name, c.Description, Type(c.ComponentType), apiInfo, nil) diff --git a/lwcomponent/catalog_test.go b/lwcomponent/catalog_test.go index f7156359f..1bfa480ab 100644 --- a/lwcomponent/catalog_test.go +++ b/lwcomponent/catalog_test.go @@ -52,7 +52,7 @@ func TestCatalogNewCatalog(t *testing.T) { api.WithURL(fakeServer.URL()), ) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) }) @@ -90,7 +90,7 @@ func TestCatalogNewCatalog(t *testing.T) { CreateLocalComponent(name, version, false) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, true) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -117,7 +117,7 @@ func TestCatalogNewCatalog(t *testing.T) { api.WithURL(fakeServer.URL()), ) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, catalog) assert.NotNil(t, err) }) @@ -149,7 +149,7 @@ func TestCatalogNewCatalog(t *testing.T) { CreateLocalComponent(name, "invalid-version", false) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -274,7 +274,7 @@ func TestCatalogComponentCount(t *testing.T) { _, home := FakeHome() defer ResetHome(home) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) assert.Equal(t, apiCount, catalog.ComponentCount()) @@ -288,7 +288,7 @@ func TestCatalogComponentCount(t *testing.T) { CreateLocalComponent(fmt.Sprintf("%s-%d", prefix, i), fmt.Sprintf("%d.0.0", i), false) } - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) assert.Equal(t, apiCount, catalog.ComponentCount()) @@ -302,7 +302,7 @@ func TestCatalogComponentCount(t *testing.T) { CreateLocalComponent(fmt.Sprintf("deprecated-%d", i), fmt.Sprintf("%d.0.0", i), false) } - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) assert.Equal(t, apiCount+deprecatedCount, catalog.ComponentCount()) @@ -316,7 +316,7 @@ func TestCatalogComponentCount(t *testing.T) { CreateLocalComponent(fmt.Sprintf("dev-%d", i), fmt.Sprintf("%d.0.0", i), true) } - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) assert.Equal(t, apiCount+developmentCount, catalog.ComponentCount()) @@ -334,7 +334,7 @@ func TestCatalogComponentCount(t *testing.T) { CreateLocalComponent(fmt.Sprintf("all-dev-%d", i), fmt.Sprintf("%d.0.0", i), true) } - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) assert.Equal(t, apiCount+deprecatedCount+developmentCount, catalog.ComponentCount()) @@ -382,7 +382,7 @@ func TestCatalogGetComponent(t *testing.T) { CreateLocalComponent(name, version, false) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, true) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, err) component, err := catalog.GetComponent(name) @@ -395,7 +395,7 @@ func TestCatalogGetComponent(t *testing.T) { _, home := FakeHome() defer ResetHome(home) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, err) component, err := catalog.GetComponent("component-example") @@ -414,7 +414,7 @@ func TestCatalogGetComponent(t *testing.T) { CreateLocalComponent(name, version, true) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, err) component, err := catalog.GetComponent(name) @@ -437,7 +437,7 @@ func TestCatalogGetComponent(t *testing.T) { CreateLocalComponent(name, version, false) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, err) component, err := catalog.GetComponent(name) @@ -478,7 +478,7 @@ func TestCatalogListComponentVersions(t *testing.T) { api.WithURL(fakeServer.URL()), ) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -516,7 +516,7 @@ func TestCatalogListComponentVersions(t *testing.T) { api.WithURL(fakeServer.URL()), ) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -575,7 +575,7 @@ func TestCatalogStage(t *testing.T) { api.WithURL(fakeServer.URL()), ) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -592,7 +592,7 @@ func TestCatalogStage(t *testing.T) { t.Run("already installed", func(t *testing.T) { CreateLocalComponent(name, version, false) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -733,7 +733,7 @@ func TestCatalogInstall(t *testing.T) { api.WithToken("TOKEN"), api.WithURL(fakeServer.URL())) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -808,7 +808,7 @@ func TestCatalogDelete(t *testing.T) { CreateLocalComponent(name, version, false) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -832,7 +832,7 @@ func TestCatalogDelete(t *testing.T) { CreateLocalComponent(name, version, true) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -854,7 +854,7 @@ func TestCatalogDelete(t *testing.T) { t.Run("delete-not-installed", func(t *testing.T) { name := fmt.Sprintf("%s-1", prefix) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) @@ -871,7 +871,7 @@ func TestCatalogDelete(t *testing.T) { CreateLocalComponent(name, version, false) - catalog, err := lwcomponent.NewCatalog(client, newTestStage, false) + catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) assert.Nil(t, err) diff --git a/lwcomponent/cdk_component.go b/lwcomponent/cdk_component.go index aeadfdcc6..cf3212ac5 100644 --- a/lwcomponent/cdk_component.go +++ b/lwcomponent/cdk_component.go @@ -212,6 +212,10 @@ func status(apiInfo *ApiInfo, hostInfo *HostInfo) Status { } func isTainted(apiInfo *ApiInfo, installedVer *semver.Version) bool { + if len(apiInfo.AllVersions) == 0 { + return false + } + for _, ver := range apiInfo.AllVersions { if ver.Equal(installedVer) { return false