1616package app
1717
1818import (
19+ "context"
20+ "fmt"
21+
1922 "github.com/spf13/cobra"
23+ "golang.org/x/text/cases"
24+ "golang.org/x/text/language"
2025
2126 "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion"
27+ "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator"
2228 "github.com/arduino/arduino-app-cli/cmd/feedback"
29+ "github.com/arduino/arduino-app-cli/internal/orchestrator"
30+ "github.com/arduino/arduino-app-cli/internal/orchestrator/app"
2331 "github.com/arduino/arduino-app-cli/internal/orchestrator/config"
2432)
2533
@@ -32,17 +40,63 @@ func newRestartCmd(cfg config.Configuration) *cobra.Command {
3240 if len (args ) == 0 {
3341 return cmd .Help ()
3442 }
35- app , err := Load (args [0 ])
43+ appToStart , err := Load (args [0 ])
3644 if err != nil {
3745 feedback .Fatal (err .Error (), feedback .ErrBadArgument )
38- return nil
39- }
40- if err := stopHandler (cmd .Context (), app ); err != nil {
41- feedback .Warnf ("failed to stop app: %s" , err .Error ())
4246 }
43- return startHandler (cmd .Context (), cfg , app )
47+ return restartHandler (cmd .Context (), cfg , appToStart )
4448 },
4549 ValidArgsFunction : completion .ApplicationNames (cfg ),
4650 }
4751 return cmd
4852}
53+
54+ func restartHandler (ctx context.Context , cfg config.Configuration , app app.ArduinoApp ) error {
55+ out , _ , getResult := feedback .OutputStreams ()
56+
57+ stream := orchestrator .RestartApp (
58+ ctx ,
59+ servicelocator .GetDockerClient (),
60+ servicelocator .GetProvisioner (),
61+ servicelocator .GetModelsIndex (),
62+ servicelocator .GetBricksIndex (),
63+ app ,
64+ cfg ,
65+ servicelocator .GetStaticStore (),
66+ )
67+ for message := range stream {
68+ switch message .GetType () {
69+ case orchestrator .ProgressType :
70+ fmt .Fprintf (out , "Progress[%s]: %.0f%%\n " , message .GetProgress ().Name , message .GetProgress ().Progress )
71+ case orchestrator .InfoType :
72+ fmt .Fprintln (out , "[INFO]" , message .GetData ())
73+ case orchestrator .ErrorType :
74+ errMesg := cases .Title (language .AmericanEnglish ).String (message .GetError ().Error ())
75+ feedback .Fatal (fmt .Sprintf ("[ERROR] %s" , errMesg ), feedback .ErrGeneric )
76+ return nil
77+ }
78+ }
79+
80+ outputResult := getResult ()
81+ feedback .PrintResult (restartAppResult {
82+ AppName : app .Name ,
83+ Status : "restarted" ,
84+ Output : outputResult ,
85+ })
86+
87+ return nil
88+ }
89+
90+ type restartAppResult struct {
91+ AppName string `json:"app_name"`
92+ Status string `json:"status"`
93+ Output * feedback.OutputStreamsResult `json:"output,omitempty"`
94+ }
95+
96+ func (r restartAppResult ) String () string {
97+ return fmt .Sprintf ("✓ App %q restarted successfully" , r .AppName )
98+ }
99+
100+ func (r restartAppResult ) Data () interface {} {
101+ return r
102+ }
0 commit comments