Skip to content

Commit

Permalink
fix: reduce component API usage (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-stewart authored Feb 20, 2024
1 parent 4fca224 commit 5455e17
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
43 changes: 32 additions & 11 deletions cli/cmd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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()

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/component_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 0 additions & 7 deletions lwcomponent/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
44 changes: 22 additions & 22 deletions lwcomponent/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)

Expand All @@ -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)
})
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions lwcomponent/cdk_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5455e17

Please sign in to comment.