diff --git a/cli/cmd/component.go b/cli/cmd/component.go index 3ae6e7c5d..c8e41f653 100644 --- a/cli/cmd/component.go +++ b/cli/cmd/component.go @@ -366,7 +366,7 @@ func listComponents() error { } if cli.JSONOutput() { - return cli.OutputJSON(catalog) + return CDKComponentsJSON(catalog) } if catalog.ComponentCount() == 0 { @@ -529,7 +529,7 @@ func showComponent(args []string) error { } if cli.JSONOutput() { - return cli.OutputJSON(component) + return CDKComponentJSON(component) } printComponent(component.PrintSummary()) @@ -1224,18 +1224,15 @@ func LoadCatalog(componentName string, getAllVersions bool) (*lwcomponent.Catalo 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)) - } + if component.HostInfo == nil || (component.HostInfo != nil && !component.HostInfo.Development()) { + 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) + component.ApiInfo.AllVersions = vers + catalog.Components[componentName] = lwcomponent.NewCDKComponent(component.ApiInfo, component.HostInfo) + } } componentsApiInfo = make(map[string]*lwcomponent.ApiInfo, len(catalog.Components)) @@ -1250,3 +1247,53 @@ func LoadCatalog(componentName string, getAllVersions bool) (*lwcomponent.Catalo return catalog, nil } + +type componentJSON struct { + Name string `json:"name"` + Description string `json:"description"` + Version string `json:"version"` + LatestVersion string `json:"latest_version"` + ComponentType string `json:"type"` + Status string `json:"status"` +} + +func newComponentJSON(component *lwcomponent.CDKComponent) *componentJSON { + semver := component.InstalledVersion() + version := "" + + if semver != nil { + version = semver.String() + } + + latestVersion := "" + if component.LatestVersion() != nil { + latestVersion = component.LatestVersion().String() + } + + return &componentJSON{ + Name: component.Name, + Description: component.Description, + Version: version, + LatestVersion: latestVersion, + ComponentType: string(component.Type), + Status: component.Status.String(), + } +} + +func CDKComponentsJSON(catalog *lwcomponent.Catalog) error { + type catalogJSON struct { + Components []*componentJSON `json:"components"` + } + + output := catalogJSON{} + + for _, component := range catalog.Components { + output.Components = append(output.Components, newComponentJSON(&component)) + } + + return cli.OutputJSON(output) +} + +func CDKComponentJSON(component *lwcomponent.CDKComponent) error { + return cli.OutputJSON(newComponentJSON(component)) +} diff --git a/cli/cmd/component_dev.go b/cli/cmd/component_dev.go index 3d8d7c894..23007dfe9 100644 --- a/cli/cmd/component_dev.go +++ b/cli/cmd/component_dev.go @@ -88,7 +88,11 @@ func runComponentsDevMode(_ *cobra.Command, args []string) error { } func devModeComponent(args []string) error { - var componentName string = args[0] + var ( + componentName string = args[0] + componentDescription string = "" + componentDir string = "" + ) cli.StartProgress("Loading components Catalog...") @@ -101,11 +105,9 @@ func devModeComponent(args []string) error { component, _ := catalog.GetComponent(componentName) if component == nil { - newComponent := lwcomponent.NewCDKComponent(componentName, "", lwcomponent.EmptyType, nil, nil) - component = &newComponent cli.OutputHuman("Component '%s' not found. Defining a new component.\n", - color.HiYellowString(component.Name)) + color.HiYellowString(componentName)) var ( helpMsg = fmt.Sprintf("What are these component types ?\n"+ @@ -128,28 +130,43 @@ func devModeComponent(args []string) error { } } - component.Type = lwcomponent.Type(cdkDevState.Type) + componentType := lwcomponent.Type(cdkDevState.Type) if cdkDevState.Description == "" { if err := survey.AskOne(&survey.Input{ Message: "What is this component about? (component description):", - }, &component.Description); err != nil { + }, &componentDescription); err != nil { return err } } else { - component.Description = cdkDevState.Description + componentDescription = cdkDevState.Description + } + + dir, err := lwcomponent.CatalogCacheDir() + if err != nil { + return err } + + componentDir = filepath.Join(dir, componentName) + + if err := os.MkdirAll(componentDir, os.ModePerm); err != nil { + return err + } + + hostInfo, err := lwcomponent.NewHostInfo(componentDir, componentDescription, componentType) + if err != nil { + return err + } + + newComponent := lwcomponent.NewCDKComponent(nil, hostInfo) + + component = &newComponent } if err := component.EnterDevMode(); err != nil { return errors.Wrap(err, "unable to enter development mode") } - componentDir, err := component.Dir() - if err != nil { - return errors.New("unable to detect RootPath") - } - cli.OutputHuman("Component '%s' in now in development mode.\n", color.HiYellowString(component.Name)) diff --git a/lwcomponent/catalog.go b/lwcomponent/catalog.go index f973b6d04..0e55af29e 100644 --- a/lwcomponent/catalog.go +++ b/lwcomponent/catalog.go @@ -222,7 +222,7 @@ func (c *Catalog) Install(component *CDKComponent) (err error) { return } - component.HostInfo, err = CreateHostInfo(componentDir, component.Description, component.Type) + component.HostInfo, err = NewHostInfo(componentDir, component.Description, component.Type) if err != nil { return } @@ -293,7 +293,7 @@ func NewCatalog( var allVersions []*semver.Version 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) + cdkComponents[c.Name] = NewCDKComponent(apiInfo, nil) } components, err := mergeComponents(cdkComponents) @@ -315,8 +315,8 @@ func NewCachedCatalog( cachedComponents := make(map[string]CDKComponent, len(cachedComponentsApiInfo)) - for _, a := range cachedComponentsApiInfo { - cachedComponents[a.Name] = NewCDKComponent(a.Name, a.Desc, a.ComponentType, a, nil) + for _, apiInfo := range cachedComponentsApiInfo { + cachedComponents[apiInfo.Name] = NewCDKComponent(apiInfo, nil) } components, err := mergeComponents(cachedComponents) @@ -343,7 +343,7 @@ func mergeComponents(components map[string]CDKComponent) (allComponents map[stri hostInfo = component.HostInfo delete(localComponents, c.Name) } - allComponents[c.Name] = NewCDKComponent(c.Name, c.Description, c.Type, c.ApiInfo, hostInfo) + allComponents[c.Name] = NewCDKComponent(c.ApiInfo, hostInfo) } for _, c := range localComponents { @@ -382,33 +382,27 @@ func LoadLocalComponents() (components map[string]CDKComponent, err error) { continue } - hostInfo, _ := NewHostInfo(filepath.Join(cacheDir, file.Name())) + hostInfo, _ := LoadHostInfo(filepath.Join(cacheDir, file.Name())) if hostInfo == nil { - component, found := prototypeComponents[file.Name()] if !found { continue } - hostInfo, err = CreateHostInfo(filepath.Join(cacheDir, file.Name()), component.Description, component.Type) + hostInfo, err = NewHostInfo(filepath.Join(cacheDir, file.Name()), component.Description, component.Type) if err != nil { return nil, err } } if hostInfo.Development() { - devInfo, err := newDevInfo(hostInfo.Dir) + _, err := newDevInfo(hostInfo.Dir) if err != nil { return nil, err } - components[hostInfo.Name()] = NewCDKComponent(hostInfo.Name(), devInfo.Desc, devInfo.ComponentType, nil, hostInfo) + components[hostInfo.Name] = NewCDKComponent(nil, hostInfo) } else { - components[hostInfo.Name()] = NewCDKComponent( - hostInfo.Name(), - hostInfo.Description, - hostInfo.ComponentType, - nil, - hostInfo) + components[hostInfo.Name] = NewCDKComponent(nil, hostInfo) } } diff --git a/lwcomponent/catalog_test.go b/lwcomponent/catalog_test.go index 57ea16cd3..ac68428c3 100644 --- a/lwcomponent/catalog_test.go +++ b/lwcomponent/catalog_test.go @@ -88,7 +88,7 @@ func TestCatalogNewCatalog(t *testing.T) { name := fmt.Sprintf("%s-%d", prefix, 1) version := fmt.Sprintf("%d.0.0", 1) - CreateLocalComponent(name, version, false) + CreateLocalComponent(name, version, false, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) @@ -147,7 +147,7 @@ func TestCatalogNewCatalog(t *testing.T) { api.WithURL(fakeServer.URL()), ) - CreateLocalComponent(name, "invalid-version", false) + CreateLocalComponent(name, "invalid-version", false, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) @@ -195,11 +195,11 @@ func TestCatalogNewCachedCatalog(t *testing.T) { cachedComponentsApiInfo[name] = lwcomponent.NewAPIInfo(1, name, latestVersion, allVersions, "", 1, false, lwcomponent.BinaryType) } - CreateLocalComponent("testComponentWithApiInfo-0", "5.4.3", false) - CreateLocalComponent("testComponentWithApiInfo-1", "1.0.0", false) - CreateLocalComponent("testComponentWithApiInfo-2", "2.0.1", false) - CreateLocalComponent("testComponentWithApiInfo-3", "3.0.1", true) - CreateLocalComponent("testComponent", "0.0.1-dev", true) + CreateLocalComponent("testComponentWithApiInfo-0", "5.4.3", false, lwcomponent.BinaryType) + CreateLocalComponent("testComponentWithApiInfo-1", "1.0.0", false, lwcomponent.BinaryType) + CreateLocalComponent("testComponentWithApiInfo-2", "2.0.1", false, lwcomponent.BinaryType) + CreateLocalComponent("testComponentWithApiInfo-3", "3.0.1", true, lwcomponent.BinaryType) + CreateLocalComponent("testComponent", "0.0.1-dev", true, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCachedCatalog(client, newTestStage, cachedComponentsApiInfo) assert.NotNil(t, catalog) @@ -285,7 +285,7 @@ func TestCatalogComponentCount(t *testing.T) { defer ResetHome(home) for i := 0; i < apiCount; i++ { - CreateLocalComponent(fmt.Sprintf("%s-%d", prefix, i), fmt.Sprintf("%d.0.0", i), false) + CreateLocalComponent(fmt.Sprintf("%s-%d", prefix, i), fmt.Sprintf("%d.0.0", i), false, lwcomponent.BinaryType) } catalog, err := lwcomponent.NewCatalog(client, newTestStage) @@ -299,7 +299,7 @@ func TestCatalogComponentCount(t *testing.T) { defer ResetHome(home) for i := 0; i < deprecatedCount; i++ { - CreateLocalComponent(fmt.Sprintf("deprecated-%d", i), fmt.Sprintf("%d.0.0", i), false) + CreateLocalComponent(fmt.Sprintf("deprecated-%d", i), fmt.Sprintf("%d.0.0", i), false, lwcomponent.BinaryType) } catalog, err := lwcomponent.NewCatalog(client, newTestStage) @@ -313,7 +313,7 @@ func TestCatalogComponentCount(t *testing.T) { defer ResetHome(home) for i := 0; i < developmentCount; i++ { - CreateLocalComponent(fmt.Sprintf("dev-%d", i), fmt.Sprintf("%d.0.0", i), true) + CreateLocalComponent(fmt.Sprintf("dev-%d", i), fmt.Sprintf("%d.0.0", i), true, lwcomponent.BinaryType) } catalog, err := lwcomponent.NewCatalog(client, newTestStage) @@ -327,11 +327,11 @@ func TestCatalogComponentCount(t *testing.T) { defer ResetHome(home) for i := 0; i < deprecatedCount; i++ { - CreateLocalComponent(fmt.Sprintf("all-deprecated-%d", i), fmt.Sprintf("%d.0.0", i), false) + CreateLocalComponent(fmt.Sprintf("all-deprecated-%d", i), fmt.Sprintf("%d.0.0", i), false, lwcomponent.BinaryType) } for i := 0; i < developmentCount; i++ { - CreateLocalComponent(fmt.Sprintf("all-dev-%d", i), fmt.Sprintf("%d.0.0", i), true) + CreateLocalComponent(fmt.Sprintf("all-dev-%d", i), fmt.Sprintf("%d.0.0", i), true, lwcomponent.BinaryType) } catalog, err := lwcomponent.NewCatalog(client, newTestStage) @@ -380,7 +380,7 @@ func TestCatalogGetComponent(t *testing.T) { _, home := FakeHome() defer ResetHome(home) - CreateLocalComponent(name, version, false) + CreateLocalComponent(name, version, false, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, err) @@ -412,7 +412,7 @@ func TestCatalogGetComponent(t *testing.T) { _, home := FakeHome() defer ResetHome(home) - CreateLocalComponent(name, version, true) + CreateLocalComponent(name, version, true, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, err) @@ -435,7 +435,7 @@ func TestCatalogGetComponent(t *testing.T) { _, home := FakeHome() defer ResetHome(home) - CreateLocalComponent(name, version, false) + CreateLocalComponent(name, version, false, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.Nil(t, err) @@ -657,7 +657,7 @@ func TestCatalogStage(t *testing.T) { }) t.Run("already installed", func(t *testing.T) { - CreateLocalComponent(name, version, false) + CreateLocalComponent(name, version, false, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) @@ -889,7 +889,7 @@ func TestCatalogDelete(t *testing.T) { t.Run("delete-installed", func(t *testing.T) { name := fmt.Sprintf("%s-1", prefix) - CreateLocalComponent(name, version, false) + CreateLocalComponent(name, version, false, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) @@ -913,7 +913,7 @@ func TestCatalogDelete(t *testing.T) { t.Run("delete-development", func(t *testing.T) { name := "delete-dev" - CreateLocalComponent(name, version, true) + CreateLocalComponent(name, version, true, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) @@ -952,7 +952,7 @@ func TestCatalogDelete(t *testing.T) { t.Run("delete-twice", func(t *testing.T) { name := fmt.Sprintf("%s-2", prefix) - CreateLocalComponent(name, version, false) + CreateLocalComponent(name, version, false, lwcomponent.BinaryType) catalog, err := lwcomponent.NewCatalog(client, newTestStage) assert.NotNil(t, catalog) @@ -1054,7 +1054,7 @@ func generateFetchResponse(id int32, name string, version string, url string) st return string(result) } -func CreateLocalComponent(componentName string, version string, development bool) { +func CreateLocalComponent(componentName string, version string, development bool, componentType lwcomponent.Type) { dir, err := lwcomponent.CatalogCacheDir() if err != nil { panic(err) @@ -1082,7 +1082,7 @@ func CreateLocalComponent(componentName string, version string, development bool panic(err) } - info := lwcomponent.HostInfo{Dir: "", Description: "", ComponentType: lwcomponent.BinaryType} + info := lwcomponent.HostInfo{Dir: "", Desc: "", ComponentType: componentType} data, err := json.Marshal(info) if err != nil { panic(err) diff --git a/lwcomponent/cdk_component.go b/lwcomponent/cdk_component.go index cf3212ac5..7ca0b2b6d 100644 --- a/lwcomponent/cdk_component.go +++ b/lwcomponent/cdk_component.go @@ -32,7 +32,7 @@ type CDKComponent struct { stage Stager } -func NewCDKComponent(name string, desc string, componentType Type, apiInfo *ApiInfo, hostInfo *HostInfo) CDKComponent { +func NewCDKComponent(apiInfo *ApiInfo, hostInfo *HostInfo) CDKComponent { var ( exec Executer = &nonExecutable{} ) @@ -44,8 +44,8 @@ func NewCDKComponent(name string, desc string, componentType Type, apiInfo *ApiI { dir := hostInfo.Dir - if componentType == BinaryType || componentType == CommandType { - exec = NewExecuable(name, dir) + if hostInfo.ComponentType == BinaryType || hostInfo.ComponentType == CommandType { + exec = NewExecuable(hostInfo.Name, dir) } } default: @@ -54,15 +54,31 @@ func NewCDKComponent(name string, desc string, componentType Type, apiInfo *ApiI } } - return CDKComponent{ - Name: name, - Description: desc, - Type: componentType, - Status: status, - Exec: exec, - ApiInfo: apiInfo, - HostInfo: hostInfo, + if apiInfo != nil { + return CDKComponent{ + Name: apiInfo.Name, + Description: apiInfo.Desc, + Type: apiInfo.ComponentType, + Status: status, + Exec: exec, + ApiInfo: apiInfo, + HostInfo: hostInfo, + } + } + + if hostInfo != nil { + return CDKComponent{ + Name: hostInfo.Name, + Description: hostInfo.Desc, + Type: hostInfo.ComponentType, + Status: status, + Exec: exec, + ApiInfo: apiInfo, + HostInfo: hostInfo, + } } + + return CDKComponent{} } func (c *CDKComponent) Dir() (string, error) { diff --git a/lwcomponent/cdk_component_test.go b/lwcomponent/cdk_component_test.go index 2b1e63a91..f5ab06d4c 100644 --- a/lwcomponent/cdk_component_test.go +++ b/lwcomponent/cdk_component_test.go @@ -14,7 +14,7 @@ func TestNewCDKComponent(t *testing.T) { defer ResetHome(home) t.Run("UnknownStatus", func(t *testing.T) { - component := lwcomponent.NewCDKComponent("unknown", "", lwcomponent.BinaryType, nil, nil) + component := lwcomponent.NewCDKComponent(nil, &lwcomponent.HostInfo{Name: "unknown"}) assert.Equal(t, lwcomponent.UnknownStatus, component.Status) }) @@ -27,7 +27,7 @@ func TestNewCDKComponent(t *testing.T) { apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, apiInfo, nil) + component := lwcomponent.NewCDKComponent(apiInfo, nil) assert.Equal(t, lwcomponent.NotInstalled, component.Status) }) @@ -43,13 +43,13 @@ func TestNewCDKComponent(t *testing.T) { apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) - CreateLocalComponent(name, ver, false) + CreateLocalComponent(name, ver, false, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) assert.Equal(t, lwcomponent.Installed, component.Status) }) @@ -67,13 +67,13 @@ func TestNewCDKComponent(t *testing.T) { apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) - CreateLocalComponent(name, installVer, false) + CreateLocalComponent(name, installVer, false, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) assert.Equal(t, lwcomponent.UpdateAvailable, component.Status) }) @@ -84,13 +84,13 @@ func TestNewCDKComponent(t *testing.T) { ver string = "1.1.1" ) - CreateLocalComponent(name, ver, false) + CreateLocalComponent(name, ver, false, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, nil, hostInfo) + component := lwcomponent.NewCDKComponent(nil, hostInfo) assert.Equal(t, lwcomponent.InstalledDeprecated, component.Status) }) @@ -104,14 +104,14 @@ func TestNewCDKComponent(t *testing.T) { version, _ := semver.NewVersion(ver) allVersions := []*semver.Version{version} - CreateLocalComponent(name, ver, false) + CreateLocalComponent(name, ver, false, lwcomponent.BinaryType) apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, true, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) assert.Equal(t, lwcomponent.InstalledDeprecated, component.Status) }) @@ -126,7 +126,7 @@ func TestNewCDKComponent(t *testing.T) { apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, true, lwcomponent.BinaryType) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, apiInfo, nil) + component := lwcomponent.NewCDKComponent(apiInfo, nil) assert.Equal(t, lwcomponent.NotInstalledDeprecated, component.Status) }) @@ -143,13 +143,13 @@ func TestNewCDKComponent(t *testing.T) { apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) - CreateLocalComponent(name, installVer, false) + CreateLocalComponent(name, installVer, false, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) assert.Equal(t, lwcomponent.Tainted, component.Status) }) @@ -160,13 +160,13 @@ func TestNewCDKComponent(t *testing.T) { ver string = "1.1.1" ) - CreateLocalComponent(name, ver, true) + CreateLocalComponent(name, ver, true, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, nil, hostInfo) + component := lwcomponent.NewCDKComponent(nil, hostInfo) assert.Equal(t, lwcomponent.Development, component.Status) }) @@ -182,13 +182,13 @@ func TestNewCDKComponent(t *testing.T) { apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) - CreateLocalComponent(name, ver, false) + CreateLocalComponent(name, ver, false, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.BinaryType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) _, _, err := component.Exec.Execute([]string{}, "") assert.Nil(t, err) @@ -205,13 +205,13 @@ func TestNewCDKComponent(t *testing.T) { apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) - CreateLocalComponent(name, ver, false) + CreateLocalComponent(name, ver, false, lwcomponent.BinaryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.CommandType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) _, _, err := component.Exec.Execute([]string{}, "") assert.Nil(t, err) @@ -226,15 +226,15 @@ func TestNewCDKComponent(t *testing.T) { version, _ := semver.NewVersion(ver) allVersions := []*semver.Version{version} - apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) + apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.LibraryType) - CreateLocalComponent(name, ver, false) + CreateLocalComponent(name, ver, false, lwcomponent.LibraryType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.LibraryType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) _, _, err := component.Exec.Execute([]string{}, "") assert.Equal(t, lwcomponent.ErrNonExecutable, err) @@ -249,15 +249,15 @@ func TestNewCDKComponent(t *testing.T) { version, _ := semver.NewVersion(ver) allVersions := []*semver.Version{version} - apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.BinaryType) + apiInfo := lwcomponent.NewAPIInfo(1, name, version, allVersions, "", 1, false, lwcomponent.StandaloneType) - CreateLocalComponent(name, ver, false) + CreateLocalComponent(name, ver, false, lwcomponent.StandaloneType) dir, _ := lwcomponent.CatalogCacheDir() - hostInfo, _ := lwcomponent.NewHostInfo(filepath.Join(dir, name)) + hostInfo, _ := lwcomponent.LoadHostInfo(filepath.Join(dir, name)) - component := lwcomponent.NewCDKComponent(name, "", lwcomponent.StandaloneType, apiInfo, hostInfo) + component := lwcomponent.NewCDKComponent(apiInfo, hostInfo) _, _, err := component.Exec.Execute([]string{}, "") assert.Equal(t, lwcomponent.ErrNonExecutable, err) diff --git a/lwcomponent/host_info.go b/lwcomponent/host_info.go index 172e72eb0..31930a579 100644 --- a/lwcomponent/host_info.go +++ b/lwcomponent/host_info.go @@ -21,12 +21,13 @@ var ( ) type HostInfo struct { + Name string `json:"name"` ComponentType Type `json:"type"` - Description string `json:"description"` + Desc string `json:"description"` Dir string `json:"-"` } -func NewHostInfo(dir string) (*HostInfo, error) { +func LoadHostInfo(dir string) (*HostInfo, error) { path := filepath.Join(dir, InfoFile) data, err := os.ReadFile(path) @@ -34,26 +35,31 @@ func NewHostInfo(dir string) (*HostInfo, error) { return nil, errors.Errorf("unable to read %s file", path) } - info := HostInfo{} + hostInfo := HostInfo{} - err = json.Unmarshal(data, &info) + err = json.Unmarshal(data, &hostInfo) if err != nil { return nil, errors.Errorf("unable to unmarshal %s file", path) } - info.Dir = dir + hostInfo.Dir = dir - return &info, nil + if hostInfo.Name == "" { + hostInfo.Name = filepath.Base(dir) + } + + return &hostInfo, nil } -func CreateHostInfo(dir string, desc string, componentType Type) (*HostInfo, error) { +func NewHostInfo(dir string, desc string, componentType Type) (*HostInfo, error) { path := filepath.Join(dir, InfoFile) if !file.FileExists(path) { info := &HostInfo{ + Name: filepath.Base(dir), Dir: dir, ComponentType: componentType, - Description: desc, + Desc: desc, } buf := new(bytes.Buffer) @@ -64,7 +70,7 @@ func CreateHostInfo(dir string, desc string, componentType Type) (*HostInfo, err return info, os.WriteFile(path, buf.Bytes(), 0644) } - return NewHostInfo(dir) + return LoadHostInfo(dir) } func (h *HostInfo) Delete() error { @@ -75,13 +81,6 @@ func (h *HostInfo) Development() bool { return file.FileExists(filepath.Join(h.Dir, DevelopmentFile)) } -// Returns the Component name -// -// The Component name is the same as the name of the base directory -func (h *HostInfo) Name() string { - return filepath.Base(h.Dir) -} - func (h *HostInfo) Signature() (sig []byte, err error) { _, err = os.Stat(h.Dir) if os.IsNotExist(err) { @@ -133,7 +132,7 @@ func (h *HostInfo) Validate() (err error) { return } - componentName := h.Name() + componentName := h.Name if !file.FileExists(filepath.Join(h.Dir, SignatureFile)) { return errors.New(fmt.Sprintf("missing file '%s'", componentName))