Skip to content

Commit cd4beef

Browse files
authored
Jon stewart/grow 2329/cdk update (#1403)
1 parent 2a56eee commit cd4beef

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

cli/cmd/component.go

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ func installComponent(cmd *cobra.Command, args []string) (err error) {
332332
return
333333
}
334334

335+
cli.OutputChecklist(successIcon, fmt.Sprintf("Component %s found\n", component.Name))
336+
335337
cli.StartProgress(fmt.Sprintf("Staging component %s...", componentName))
336338

337339
start = time.Now()
@@ -457,7 +459,107 @@ func runComponentsUpdate(_ *cobra.Command, args []string) (err error) {
457459
return prototypeRunComponentsUpdate(args)
458460
}
459461

460-
return nil
462+
return updateComponent(args)
463+
}
464+
465+
func updateComponent(args []string) (err error) {
466+
var (
467+
componentName string = args[0]
468+
params map[string]interface{} = make(map[string]interface{})
469+
start time.Time
470+
targetVersion *semver.Version
471+
)
472+
473+
cli.StartProgress("Loading components Catalog...")
474+
475+
catalog, err := lwcomponent.NewCatalog(cli.LwApi, lwcomponent.NewStageTarGz)
476+
defer catalog.Cache()
477+
478+
cli.StopProgress()
479+
if err != nil {
480+
return errors.Wrap(err, "unable to load component Catalog")
481+
}
482+
483+
component, err := catalog.GetComponent(componentName)
484+
if err != nil {
485+
return err
486+
}
487+
488+
cli.OutputChecklist(successIcon, fmt.Sprintf("Component %s found\n", component.Name))
489+
490+
installedVersion := component.InstalledVersion()
491+
if installedVersion == nil {
492+
return errors.Errorf("component %s not installed", color.HiYellowString(componentName))
493+
}
494+
495+
latestVersion := component.LatestVersion()
496+
if latestVersion == nil {
497+
return errors.Errorf("component %s not available in API", color.HiYellowString(componentName))
498+
}
499+
500+
if versionArg == "" {
501+
targetVersion = latestVersion
502+
} else {
503+
targetVersion, err = semver.NewVersion(versionArg)
504+
if err != nil {
505+
return errors.Errorf("invalid semantic version %s", versionArg)
506+
}
507+
}
508+
509+
if installedVersion.Equal(targetVersion) {
510+
return errors.Errorf("You are already running version %s of this component",
511+
color.HiYellowString(installedVersion.String()))
512+
}
513+
514+
cli.StartProgress(fmt.Sprintf("Staging component %s...", color.HiYellowString(componentName)))
515+
516+
start = time.Now()
517+
518+
stageClose, err := catalog.Stage(component, versionArg)
519+
if err != nil {
520+
return
521+
}
522+
defer stageClose()
523+
524+
params["stage_duration_ms"] = time.Since(start).Milliseconds()
525+
cli.Event.FeatureData = params
526+
527+
cli.StopProgress()
528+
if err != nil {
529+
return
530+
}
531+
cli.OutputChecklist(successIcon, "Component %s staged\n", color.HiYellowString(componentName))
532+
533+
cli.StartProgress("Verifing component signature...")
534+
535+
err = catalog.Verify(component)
536+
537+
cli.StopProgress()
538+
if err != nil {
539+
err = errors.Wrap(err, "verification of component signature failed")
540+
return
541+
}
542+
cli.OutputChecklist(successIcon, "Component signature verified\n")
543+
544+
cli.StartProgress(fmt.Sprintf("Updating component %s to version %s...", component.Name, targetVersion.String()))
545+
546+
err = catalog.Install(component)
547+
548+
cli.StopProgress()
549+
if err != nil {
550+
err = errors.Wrap(err, "Update of component failed")
551+
return
552+
}
553+
554+
cli.OutputChecklist(successIcon, "Component %s updated to %s\n",
555+
color.HiYellowString(component.Name),
556+
color.HiCyanString(targetVersion.String()))
557+
558+
// @jon-stewart: TODO: component lifecycle event
559+
560+
// @jon-stewart: TODO: component update message
561+
562+
return
461563
}
462564

463565
func runComponentsDelete(_ *cobra.Command, args []string) (err error) {
@@ -488,6 +590,8 @@ func deleteComponent(args []string) (err error) {
488590
return err
489591
}
490592

593+
cli.OutputChecklist(successIcon, fmt.Sprintf("Component %s found\n", component.Name))
594+
491595
// @jon-stewart: TODO: component life cycle: cleanup
492596

493597
cli.StartProgress("Deleting component...")

lwcomponent/cdk_component.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ func (c *CDKComponent) InstalledVersion() (version *semver.Version) {
6868
return
6969
}
7070

71+
func (c *CDKComponent) LatestVersion() (version *semver.Version) {
72+
if c.apiInfo != nil {
73+
version = c.apiInfo.LatestVersion()
74+
}
75+
76+
return
77+
}
78+
7179
func (c *CDKComponent) PrintSummary() []string {
7280
var (
7381
colorize *color.Color

0 commit comments

Comments
 (0)