Skip to content

Commit

Permalink
fix: v1 component JSON (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-stewart authored Mar 1, 2024
1 parent 9740f91 commit ac5fa63
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 125 deletions.
73 changes: 60 additions & 13 deletions cli/cmd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func listComponents() error {
}

if cli.JSONOutput() {
return cli.OutputJSON(catalog)
return CDKComponentsJSON(catalog)
}

if catalog.ComponentCount() == 0 {
Expand Down Expand Up @@ -529,7 +529,7 @@ func showComponent(args []string) error {
}

if cli.JSONOutput() {
return cli.OutputJSON(component)
return CDKComponentJSON(component)
}

printComponent(component.PrintSummary())
Expand Down Expand Up @@ -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))
Expand All @@ -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))
}
41 changes: 29 additions & 12 deletions cli/cmd/component_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")

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

Expand Down
26 changes: 10 additions & 16 deletions lwcomponent/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down
Loading

0 comments on commit ac5fa63

Please sign in to comment.