-
Notifications
You must be signed in to change notification settings - Fork 0
/
executions_status.go
112 lines (95 loc) · 2.79 KB
/
executions_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/eflows4hpc/hpcwaas-api/api"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
func init() {
var follow bool
executionsStatusCmd := &cobra.Command{
Use: "status <execution-id>",
Aliases: []string{"stat", "get", "g", "s"},
Short: "Get status of an execution",
Long: `Get status of an execution given its ID:`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := api.GetClient(*clientConfig)
if err != nil {
errExit(err)
}
err = executionStatus(client, args[0], output, follow)
return handleUsageError(cmd, err)
},
}
executionsStatusCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow execution status, this implies --output=text.")
executionsCmd.AddCommand(executionsStatusCmd)
}
func executionStatus(httpClient api.HTTPClient, executionsID, output string, follow bool) error {
if follow {
output = TextOutput
}
if output != JSONOutput && output != TextOutput {
return usageError{Mes: "Only \"json\" and \"text\" output formats are supported."}
}
var executionResult api.Execution
var err error
ctx := context.Background()
executionResult, err = httpClient.Executions().Status(ctx, executionsID)
if err != nil {
return err
}
if output == JSONOutput {
data, err := json.Marshal(executionResult)
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
return displayExecution(httpClient, executionResult, follow)
}
func displayExecution(httpClient api.HTTPClient, execution api.Execution, follow bool) error {
prefix := pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.InfoMessageStyle,
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.InfoPrefixStyle,
Text: "Execution",
},
}
prefix.Printf("ID: %s, Status: %s", execution.ID, execution.Status)
if follow {
// Clear line
pterm.Printo(strings.Repeat(" ", pterm.GetTerminalWidth()))
return followExecution(httpClient, execution)
}
pterm.Println()
return nil
}
func followExecution(httpClient api.HTTPClient, execution api.Execution) error {
text := fmt.Sprintf("Execution ID: %s, Status: %s", execution.ID, execution.Status)
spinner, _ := pterm.DefaultSpinner.Start(text)
for strings.HasSuffix(strings.ToLower(execution.Status), "ing") {
time.Sleep(5 * time.Second)
var err error
ctx := context.Background()
execution, err = httpClient.Executions().Status(ctx, execution.ID)
if err != nil {
spinner.Fail("Fail to get execution status")
return err
}
text = fmt.Sprintf("Execution ID: %s, Status: %s", execution.ID, execution.Status)
spinner.UpdateText(text)
}
switch strings.ToLower(execution.Status) {
case "failed", "cancelled":
spinner.Fail(text)
default:
spinner.Success(text)
}
return nil
}