Skip to content

Commit dc17dbb

Browse files
authored
feat: CDK v1 component lifecycle events (#1461)
1 parent 5c7c5ee commit dc17dbb

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

cli/cmd/component.go

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func listComponents() error {
347347
cli.StartProgress("Loading component Catalog...")
348348

349349
catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz)
350-
defer catalog.Cache()
350+
defer catalog.Persist()
351351

352352
cli.StopProgress()
353353
if err != nil {
@@ -414,7 +414,7 @@ func installComponent(cmd *cobra.Command, args []string) (err error) {
414414
cli.StartProgress("Loading component Catalog...")
415415

416416
catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz)
417-
defer catalog.Cache()
417+
defer catalog.Persist()
418418

419419
cli.StopProgress()
420420
if err != nil {
@@ -470,9 +470,25 @@ func installComponent(cmd *cobra.Command, args []string) (err error) {
470470
}
471471
cli.OutputChecklist(successIcon, "Component version %s installed\n", component.InstalledVersion())
472472

473-
// @jon-stewart: TODO: Component lifecycle `cdk-init` command
473+
cli.StartProgress("Configuring component...")
474474

475-
// @jon-stewart: TODO: print install message
475+
stdout, stderr, errCmd := component.Exec.Execute([]string{"cdk-init"}, cli.envs()...)
476+
if errCmd != nil {
477+
if errCmd != lwcomponent.ErrNonExecutable {
478+
cli.Log.Warnw("component life cycle",
479+
"error", errCmd.Error(), "stdout", stdout, "stderr", stderr)
480+
}
481+
} else {
482+
cli.Log.Infow("component life cycle", "stdout", stdout, "stderr", stderr)
483+
}
484+
cli.StopProgress()
485+
486+
cli.OutputChecklist(successIcon, "Component configured\n")
487+
cli.OutputHuman("\nInstallation completed.\n")
488+
489+
if component.InstallMessage != "" {
490+
cli.OutputHuman(fmt.Sprintf("\n%s\n", component.InstallMessage))
491+
}
476492

477493
return
478494
}
@@ -493,7 +509,7 @@ func showComponent(args []string) error {
493509
cli.StartProgress("Loading components Catalog...")
494510

495511
catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz)
496-
defer catalog.Cache()
512+
defer catalog.Persist()
497513

498514
cli.StopProgress()
499515
if err != nil {
@@ -561,7 +577,7 @@ func updateComponent(args []string) (err error) {
561577
cli.StartProgress("Loading components Catalog...")
562578

563579
catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz)
564-
defer catalog.Cache()
580+
defer catalog.Persist()
565581

566582
cli.StopProgress()
567583
if err != nil {
@@ -643,9 +659,24 @@ func updateComponent(args []string) (err error) {
643659
color.HiYellowString(component.Name),
644660
color.HiCyanString(targetVersion.String()))
645661

646-
// @jon-stewart: TODO: component lifecycle event
662+
cli.StartProgress("Configuring component...")
647663

648-
// @jon-stewart: TODO: component update message
664+
stdout, stderr, errCmd := component.Exec.Execute([]string{"cdk-reconfigure"}, cli.envs()...)
665+
if errCmd != nil {
666+
if errCmd != lwcomponent.ErrNonExecutable {
667+
cli.Log.Warnw("component life cycle",
668+
"error", errCmd.Error(), "stdout", stdout, "stderr", stderr)
669+
}
670+
} else {
671+
cli.Log.Infow("component life cycle", "stdout", stdout, "stderr", stderr)
672+
}
673+
cli.StopProgress()
674+
675+
cli.OutputChecklist(successIcon, "Component reconfigured\n")
676+
677+
if component.UpdateMessage != "" {
678+
cli.OutputHuman(fmt.Sprintf("\n%s\n", component.UpdateMessage))
679+
}
649680

650681
return
651682
}
@@ -666,7 +697,7 @@ func deleteComponent(args []string) (err error) {
666697
cli.StartProgress("Loading components Catalog...")
667698

668699
catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz)
669-
defer catalog.Cache()
700+
defer catalog.Persist()
670701

671702
cli.StopProgress()
672703
if err != nil {
@@ -680,7 +711,20 @@ func deleteComponent(args []string) (err error) {
680711

681712
cli.OutputChecklist(successIcon, fmt.Sprintf("Component %s found\n", component.Name))
682713

683-
// @jon-stewart: TODO: component life cycle: cleanup
714+
cli.StartProgress("Cleaning component data...")
715+
716+
stdout, stderr, errCmd := component.Exec.Execute([]string{"cdk-cleanup"}, cli.envs()...)
717+
if errCmd != nil {
718+
if errCmd != lwcomponent.ErrNonExecutable {
719+
cli.Log.Warnw("component life cycle",
720+
"error", errCmd.Error(), "stdout", stdout, "stderr", stderr)
721+
}
722+
} else {
723+
cli.Log.Infow("component life cycle", "stdout", stdout, "stderr", stderr)
724+
}
725+
cli.StopProgress()
726+
727+
cli.OutputChecklist(successIcon, "Component data removed\n")
684728

685729
cli.StartProgress("Deleting component...")
686730
defer cli.StopProgress()

lwcomponent/catalog.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func (c *Catalog) ComponentCount() int {
5858
return len(c.Components)
5959
}
6060

61-
func (c *Catalog) Cache() {
62-
// @jon-stewart: TODO: cache catalog
61+
func (c *Catalog) Persist() {
62+
// @jon-stewart: TODO: store catalog on disk
6363
}
6464

6565
// Return a CDKComponent that is present on the host.
@@ -131,7 +131,17 @@ func (c *Catalog) Stage(component *CDKComponent, version string) (stageClose fun
131131
return
132132
}
133133

134-
stage, err := c.stageConstructor(component.Name, response.Data[0].ArtifactUrl)
134+
if len(response.Data) == 0 {
135+
err = errors.New("Invalid API response")
136+
return
137+
}
138+
139+
data := response.Data[0]
140+
141+
component.InstallMessage = data.InstallMessage
142+
component.UpdateMessage = data.UpdateMessage
143+
144+
stage, err := c.stageConstructor(component.Name, data.ArtifactUrl)
135145
if err != nil {
136146
return
137147
}

lwcomponent/cdk_component.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ const (
1010
)
1111

1212
type CDKComponent struct {
13-
Name string
14-
Description string
15-
SizeKB int64
16-
Type Type
17-
Status Status
13+
Name string
14+
Description string
15+
SizeKB int64
16+
Type Type
17+
Status Status
18+
InstallMessage string
19+
UpdateMessage string
1820

1921
Exec Executer
2022

0 commit comments

Comments
 (0)