@@ -26,6 +26,7 @@ import (
26
26
"time"
27
27
28
28
"github.com/codefresh-io/cli-v2/pkg/log"
29
+ "github.com/codefresh-io/cli-v2/pkg/reporter"
29
30
"github.com/codefresh-io/cli-v2/pkg/runtime"
30
31
"github.com/codefresh-io/cli-v2/pkg/store"
31
32
"github.com/codefresh-io/cli-v2/pkg/util"
@@ -174,6 +175,8 @@ func NewRuntimeInstallCommand() *cobra.Command {
174
175
installationOpts .RuntimeName = args [0 ]
175
176
}
176
177
178
+ createAnalyticsReporter (cmd .Context ())
179
+
177
180
if err := runtimeInstallCommandPreRunHandler (cmd , & installationOpts ); err != nil {
178
181
return fmt .Errorf ("Pre installation error: %w" , err )
179
182
}
@@ -348,15 +351,16 @@ func createRuntimeOnPlatform(ctx context.Context, opts *model.RuntimeInstallatio
348
351
349
352
func RunRuntimeInstall (ctx context.Context , opts * RuntimeInstallOptions ) error {
350
353
var err error
354
+ r := reporter .G ()
351
355
352
356
err = preInstallationChecks (ctx , opts )
353
- appendLogToSummary ( "Pre installation checks" , err )
357
+ handleCliStep ( reporter . InstallStepPreChecks , "Pre installation checks" , err )
354
358
if err != nil {
355
359
return fmt .Errorf ("pre installation checks failed: %w" , err )
356
360
}
357
361
358
362
rt , err := runtime .Download (opts .Version , opts .RuntimeName )
359
- appendLogToSummary ( "Downloading runtime definition" , err )
363
+ handleCliStep ( reporter . InstallStepDownloadRuntimeDefinitions , "Downloading runtime definition" , err )
360
364
if err != nil {
361
365
return fmt .Errorf ("failed to download runtime definition: %w" , err )
362
366
}
@@ -367,14 +371,14 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
367
371
}
368
372
369
373
server , err := util .CurrentServer ()
370
- appendLogToSummary ( "Getting current server address" , err )
374
+ handleCliStep ( reporter . InstallStepGetServerAddress , "Getting current server address" , err )
371
375
if err != nil {
372
376
return fmt .Errorf ("failed to get current server address: %w" , err )
373
377
}
374
378
375
379
componentNames := getComponents (rt , opts )
376
380
377
- defer postInstallationHandler (ctx , opts , & err )
381
+ defer postInstallationHandler (ctx , opts , & err , r )
378
382
379
383
token , iv , err := createRuntimeOnPlatform (ctx , & model.RuntimeInstallationArgs {
380
384
RuntimeName : opts .RuntimeName ,
@@ -384,7 +388,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
384
388
ComponentNames : componentNames ,
385
389
Repo : & opts .InsCloneOpts .Repo ,
386
390
})
387
- appendLogToSummary ( "Creating runtime on platform" , err )
391
+ handleCliStep ( reporter . InstallStepCreateRuntimeOnPlatform , "Creating runtime on platform" , err )
388
392
if err != nil {
389
393
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to create a new runtime: %w" , err ))
390
394
}
@@ -407,7 +411,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
407
411
store .Get ().LabelKeyCFType : store .Get ().CFComponentType ,
408
412
},
409
413
})
410
- appendLogToSummary ( "Bootstrapping repository" , err )
414
+ handleCliStep ( reporter . InstallStepBootstrapRepo , "Bootstrapping repository" , err )
411
415
if err != nil {
412
416
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to bootstrap repository: %w" , err ))
413
417
}
@@ -419,15 +423,15 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
419
423
store .Get ().LabelKeyCFType : fmt .Sprintf ("{{ labels.%s }}" , util .EscapeAppsetFieldName (store .Get ().LabelKeyCFType )),
420
424
},
421
425
})
422
- appendLogToSummary ( "Creating Project" , err )
426
+ handleCliStep ( reporter . InstallStepCreateProject , "Creating Project" , err )
423
427
if err != nil {
424
428
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to create project: %w" , err ))
425
429
}
426
430
427
431
// persists codefresh-cm, this must be created before events-reporter eventsource
428
432
// otherwise it will not start and no events will get to the platform.
429
433
err = persistRuntime (ctx , opts .InsCloneOpts , rt , opts .CommonConfig )
430
- appendLogToSummary ( "Creating codefresh-cm" , err )
434
+ handleCliStep ( reporter . InstallStepCreateConfigMap , "Creating codefresh-cm" , err )
431
435
if err != nil {
432
436
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to create codefresh-cm: %w" , err ))
433
437
}
@@ -436,14 +440,14 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
436
440
infoStr := fmt .Sprintf ("Creating component '%s'" , component .Name )
437
441
log .G (ctx ).Infof (infoStr )
438
442
err = component .CreateApp (ctx , opts .KubeFactory , opts .InsCloneOpts , opts .RuntimeName , store .Get ().CFComponentType , "" , "" )
439
- appendLogToSummary ( infoStr , err )
443
+ handleCliStep ( reporter . InstallStepCreateComponent , infoStr , err )
440
444
if err != nil {
441
445
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to create '%s' application: %w" , component .Name , err ))
442
446
}
443
447
}
444
448
445
449
err = installComponents (ctx , opts , rt )
446
- appendLogToSummary ( "Installing components" , err )
450
+ handleCliStep ( reporter . InstallStepInstallComponenets , "Installing components" , err )
447
451
if err != nil {
448
452
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to install components: %s" , err ))
449
453
}
@@ -457,7 +461,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
457
461
CreateDemoResources : opts .InstallDemoResources ,
458
462
IngressHost : opts .IngressHost ,
459
463
})
460
- appendLogToSummary ( gitSrcMessage , err )
464
+ handleCliStep ( reporter . InstallStepCreateGitsource , gitSrcMessage , err )
461
465
if err != nil {
462
466
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to create `%s`: %w" , store .Get ().GitSourceName , err ))
463
467
}
@@ -478,19 +482,19 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
478
482
Exclude : "**/images/**/*" ,
479
483
Include : "workflows/**/*.yaml" ,
480
484
})
481
- appendLogToSummary ( createGitSrcMessgae , err )
485
+ handleCliStep ( reporter . InstallStepCreateMarketplaceGitsource , createGitSrcMessgae , err )
482
486
if err != nil {
483
487
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to create `%s`: %w" , store .Get ().MarketplaceGitSourceName , err ))
484
488
}
485
489
486
490
timeoutErr := intervalCheckIsRuntimePersisted (ctx , opts .RuntimeName )
487
- appendLogToSummary ( "Completing runtime installation" , timeoutErr )
491
+ handleCliStep ( reporter . InstallStepCompleteRuntimeInstallation , "Completing runtime installation" , timeoutErr )
488
492
if timeoutErr != nil {
489
493
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to complete installation: %w" , timeoutErr ))
490
494
}
491
495
492
496
gitIntgErr := addDefaultGitIntegration (ctx , opts .RuntimeName , opts .GitIntegrationOpts )
493
- appendLogToSummary ( "Creating a default git integration" , gitIntgErr )
497
+ handleCliStep ( reporter . InstallStepCreateDefaultGitIntegration , "Creating a default git integration" , gitIntgErr )
494
498
if gitIntgErr != nil {
495
499
return util .DecorateErrorWithDocsLink (fmt .Errorf ("failed to create default git integration: %w" , gitIntgErr ))
496
500
}
@@ -789,9 +793,12 @@ func NewRuntimeUninstallCommand() *cobra.Command {
789
793
<BIN> runtime uninstall runtime-name --repo gitops_repo
790
794
` ),
791
795
PreRunE : func (cmd * cobra.Command , args []string ) error {
796
+ var err error
792
797
ctx := cmd .Context ()
793
798
794
- err := getKubeContextNameFromUserSelect (cmd , & kubeContextName )
799
+ createAnalyticsReporter (ctx )
800
+
801
+ err = getKubeContextNameFromUserSelect (cmd , & kubeContextName )
795
802
if err != nil {
796
803
return fmt .Errorf ("%w" , err )
797
804
}
@@ -827,7 +834,6 @@ func NewRuntimeUninstallCommand() *cobra.Command {
827
834
},
828
835
RunE : func (cmd * cobra.Command , args []string ) error {
829
836
ctx := cmd .Context ()
830
-
831
837
return RunRuntimeUninstall (ctx , & RuntimeUninstallOptions {
832
838
RuntimeName : runtimeName ,
833
839
Timeout : store .Get ().WaitTimeout ,
@@ -857,7 +863,7 @@ func RunRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
857
863
// check whether the runtime exists
858
864
if ! opts .SkipChecks {
859
865
_ , err := cfConfig .NewClient ().V2 ().Runtime ().Get (ctx , opts .RuntimeName )
860
- appendLogToSummary ( "Checking if runtime exists" , err )
866
+ handleCliStep ( reporter . UninstallStepCheckRuntimeExists , "Checking if runtime exists" , err )
861
867
if err != nil {
862
868
summaryArr = append (summaryArr , summaryLog {"you can attempt to uninstall again with the \" --skip-checks\" flag" , Info })
863
869
return err
@@ -874,7 +880,7 @@ func RunRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
874
880
Force : opts .Force ,
875
881
FastExit : opts .FastExit ,
876
882
})
877
- appendLogToSummary ( "Uninstalling repo" , err )
883
+ handleCliStep ( reporter . UninstallStepUninstallRepo , "Uninstalling repo" , err )
878
884
if err != nil {
879
885
if ! opts .Force {
880
886
summaryArr = append (summaryArr , summaryLog {"you can attempt to uninstall again with the \" --force\" flag" , Info })
@@ -883,7 +889,7 @@ func RunRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
883
889
}
884
890
885
891
err = deleteRuntimeFromPlatform (ctx , opts )
886
- appendLogToSummary ( "Deleting runtime from platform" , err )
892
+ handleCliStep ( reporter . UninstallStepDeleteRuntimeFromPlatform , "Deleting runtime from platform" , err )
887
893
if err != nil {
888
894
return fmt .Errorf ("failed to delete runtime from the platform: %w" , err )
889
895
}
@@ -893,7 +899,7 @@ func RunRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
893
899
}
894
900
895
901
uninstallDoneStr := fmt .Sprintf ("Done uninstalling runtime '%s'" , opts .RuntimeName )
896
- appendLogToSummary ( uninstallDoneStr , nil )
902
+ handleCliStep ( reporter . UninstallStepCompleteRuntimeUninstallation , uninstallDoneStr , nil )
897
903
898
904
return nil
899
905
}
@@ -1533,7 +1539,7 @@ func inferAPIURLForGitProvider(provider apmodel.GitProviders) (string, error) {
1533
1539
return "" , fmt .Errorf ("cannot infer api-url for git provider %s, %s" , provider , suggest )
1534
1540
}
1535
1541
1536
- func postInstallationHandler (ctx context.Context , opts * RuntimeInstallOptions , err * error ) {
1542
+ func postInstallationHandler (ctx context.Context , opts * RuntimeInstallOptions , err * error , r reporter. AnalyticsReporter ) {
1537
1543
if * err != nil {
1538
1544
summaryArr = append (summaryArr , summaryLog {"----------Uninstalling runtime----------" , Info })
1539
1545
log .G (ctx ).Warn ("installation failed, performing installation rollback" )
@@ -1554,6 +1560,23 @@ func postInstallationHandler(ctx context.Context, opts *RuntimeInstallOptions, e
1554
1560
printSummaryToUser ()
1555
1561
}
1556
1562
1563
+ func handleCliStep (event reporter.CliEventType , message string , err error ) {
1564
+ r := reporter .G ()
1565
+ status := reporter .SUCCESS
1566
+ if err != nil {
1567
+ status = reporter .FAILURE
1568
+ }
1569
+
1570
+ r .ReportStep (reporter.CliStepData {
1571
+ Event : event ,
1572
+ Status : status ,
1573
+ Description : message ,
1574
+ Err : err ,
1575
+ })
1576
+
1577
+ appendLogToSummary (message , err )
1578
+ }
1579
+
1557
1580
func appendLogToSummary (message string , err error ) {
1558
1581
if err != nil {
1559
1582
summaryArr = append (summaryArr , summaryLog {message , Failed })
@@ -1575,3 +1598,13 @@ func printSummaryToUser() {
1575
1598
//clear array to avoid double printing
1576
1599
summaryArr = []summaryLog {}
1577
1600
}
1601
+
1602
+ func createAnalyticsReporter (ctx context.Context ) {
1603
+ user , err := cfConfig .GetCurrentContext ().GetUser (ctx )
1604
+ // If error, it will default to noop reporter
1605
+ if err != nil {
1606
+ log .G ().Debug ("Failed to get user from context" )
1607
+ return
1608
+ }
1609
+ reporter .Init (user )
1610
+ }
0 commit comments