Skip to content

Commit 4edd220

Browse files
committed
refactor(server): use single logline also with --wait flag
1 parent 8934e97 commit 4edd220

3 files changed

Lines changed: 14 additions & 22 deletions

File tree

internal/commands/server/create.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,11 @@ func (s *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.
369369
return nil, err
370370
}
371371

372-
logline.SetMessage(fmt.Sprintf("%s: request sent", msg))
373-
logline.MarkDone()
374-
375372
if s.wait.Value() {
376-
logline := exec.NewLogEntry(msg)
377-
if err := waitForServerState(res.UUID, upcloud.ServerStateStarted, serverSvc, logline); err != nil {
378-
return nil, err
379-
}
373+
waitForServerState(res.UUID, upcloud.ServerStateStarted, serverSvc, logline, msg)
374+
} else {
375+
logline.SetMessage(fmt.Sprintf("%s: request sent", msg))
376+
logline.MarkDone()
380377
}
381378

382379
return output.MarshaledWithHumanDetails{Value: res, Details: []output.DetailRow{

internal/commands/server/server.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,21 @@ type serverCommand struct {
3131
*commands.BaseCommand
3232
}
3333

34-
func waitForServerState(uuid, state string, service service.Server, logline *ui.LogEntry) error {
35-
msg := fmt.Sprintf("Waiting for server %s to be in %s state", uuid, state)
36-
logline.SetMessage(fmt.Sprintf("%s: polling", msg))
37-
logline.StartedNow()
34+
// waitForServerState waits for server to reach given state and updates given logline with wait progress. Finally, logline is updated with given msg and either done state or timeout warning.
35+
func waitForServerState(uuid, state string, service service.Server, logline *ui.LogEntry, msg string) {
36+
logline.SetMessage(fmt.Sprintf("Waiting for server %s to be in %s state: polling", uuid, state))
3837

3938
if _, err := service.WaitForServerState(&request.WaitForServerStateRequest{
4039
UUID: uuid,
4140
DesiredState: state,
4241
Timeout: 5 * time.Minute,
4342
}); err != nil {
44-
logline.SetMessage(ui.LiveLogEntryErrorColours.Sprintf("%s: failed (%v)", msg, err.Error()))
43+
logline.SetMessage(ui.LiveLogEntryWarningColours.Sprintf("%s: partially done (%v)", msg, err.Error()))
4544
logline.SetDetails(err.Error(), "error: ")
46-
return err
45+
46+
return
4747
}
4848

4949
logline.SetMessage(fmt.Sprintf("%s: done", msg))
5050
logline.MarkDone()
51-
52-
return nil
5351
}

internal/commands/server/stop.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,11 @@ func (s *stopCommand) Execute(exec commands.Executor, uuid string) (output.Outpu
6565
return nil, err
6666
}
6767

68-
logline.SetMessage(fmt.Sprintf("%s: request sent", msg))
69-
logline.MarkDone()
70-
7168
if s.wait.Value() {
72-
logline := exec.NewLogEntry(msg)
73-
if err := waitForServerState(uuid, upcloud.ServerStateStopped, svc, logline); err != nil {
74-
return nil, err
75-
}
69+
waitForServerState(uuid, upcloud.ServerStateStopped, svc, logline, msg)
70+
} else {
71+
logline.SetMessage(fmt.Sprintf("%s: request sent", msg))
72+
logline.MarkDone()
7673
}
7774

7875
return output.OnlyMarshaled{Value: res}, nil

0 commit comments

Comments
 (0)