/
custom.go
78 lines (63 loc) · 2.16 KB
/
custom.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
package diskerase
import (
"fmt"
"strings"
"time"
"github.com/fatih/color"
"github.com/rodaine/table"
)
// CLISummary() provides the StatusResp in a summary format that is useful for
// viewing in a CLI application. It summarizes all blocks into single lines except
// for the block that is currently running.
func (x *StatusResp) CLISummary(id string) string {
if len(x.Blocks) == 0 {
return "no blocks defined"
}
blockTitle := color.New(color.FgCyan).Add(color.Underline)
name := color.New(color.FgGreen)
desc := color.New(color.FgYellow)
buff := strings.Builder{}
buff.WriteString(fmt.Sprintf("Time: %s\n", time.Now().Format(time.RFC1123)))
buff.WriteString(fmt.Sprintf("Workflow: %s\n", id))
name.Fprintln(&buff, "Name: "+x.Name)
desc.Fprintln(&buff, "Description: "+x.Desc)
if i, block := x.findRunning(x.Blocks); i != -1 {
blockTitle.Fprintln(&buff, fmt.Sprintf("\nRunning Block(%d): %s", i, block.Desc))
x.writeRunning(&buff, block)
}
blockTitle.Fprintln(&buff, "\nBlock Summaries")
x.writeOthers(&buff, x.Blocks)
return buff.String()
}
func (x *StatusResp) writeOthers(buff *strings.Builder, blocks []*BlockStatus) {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("Block Number", "Desc", "Status").WithWriter(buff)
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for i, block := range blocks {
tbl.AddRow(i, block.Desc, block.Status)
if block.Status == Status_StatusRunning {
continue
}
}
tbl.Print()
}
func (x *StatusResp) findRunning(blocks []*BlockStatus) (int, *BlockStatus) {
for i, b := range blocks {
if b.Status == Status_StatusRunning {
return i, b
}
}
return -1, nil
}
func (x *StatusResp) writeRunning(buff *strings.Builder, block *BlockStatus) {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("Job Number", "Desc", "Status").WithWriter(buff)
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for i, job := range block.Jobs {
tbl.AddRow(i, job.Desc, job.Status)
}
tbl.Print()
return
}