forked from itchio/butler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
130 lines (104 loc) · 2.62 KB
/
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"os"
"github.com/dustin/go-humanize"
"github.com/go-errors/errors"
"github.com/itchio/butler/comm"
"github.com/itchio/go-itchio"
"github.com/olekukonko/tablewriter"
)
func status(specStr string) {
go versionCheck()
must(doStatus(specStr))
}
func doStatus(specStr string) error {
spec, err := itchio.ParseSpec(specStr)
if err != nil {
return errors.Wrap(err, 1)
}
client, err := authenticateViaOauth()
if err != nil {
return errors.Wrap(err, 1)
}
listChannelsResp, err := client.ListChannels(spec.Target)
if err != nil {
return errors.Wrap(err, 1)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Channel", "Upload", "Build", "State"})
found := false
for _, ch := range listChannelsResp.Channels {
if spec.Channel != "" && ch.Name != spec.Channel {
continue
}
found = true
if ch.Head != nil {
files := ch.Head.Files
line := []string{ch.Name, fmt.Sprintf("#%d", ch.Upload.ID), buildState(ch.Head), filesState(files)}
table.Append(line)
} else {
line := []string{ch.Name, fmt.Sprintf("#%d", ch.Upload.ID), "No builds yet"}
table.Append(line)
}
if ch.Pending != nil {
files := ch.Pending.Files
line := []string{"", "", buildState(ch.Pending), filesState(files)}
table.Append(line)
}
}
if found {
table.Render()
} else {
comm.Logf("No channel %s found for %s", spec.Channel, spec.Target)
}
return nil
}
func buildState(build *itchio.BuildInfo) string {
theme := comm.GetTheme()
var s string
switch build.State {
case itchio.BuildState_COMPLETED:
s = fmt.Sprintf("%s #%d", theme.StatSign, build.ID)
case itchio.BuildState_PROCESSING:
s = fmt.Sprintf("%s #%d", theme.OpSign, build.ID)
default:
s = fmt.Sprintf(" #%d (%s)", build.ID, build.State)
}
if build.ParentBuildID != -1 {
s += fmt.Sprintf(" (from #%d)", build.ParentBuildID)
}
return s
}
func buildParent(build *itchio.BuildInfo) string {
if build.ParentBuildID == -1 {
return ""
}
return fmt.Sprintf("#%d", build.ParentBuildID)
}
func filesState(files []*itchio.BuildFileInfo) string {
if len(files) == 0 {
return "(no files)"
}
s := ""
for _, file := range files {
if s != "" {
s += ", "
}
s += fileState(file)
}
return s
}
func fileState(file *itchio.BuildFileInfo) string {
theme := comm.GetTheme()
fType := string(file.Type)
if file.SubType != itchio.BuildFileSubType_DEFAULT {
fType += fmt.Sprintf(" (%s)", file.SubType)
}
sign := theme.StatSign
if file.State != itchio.BuildFileState_UPLOADED {
sign = theme.OpSign
}
fSize := humanize.IBytes(uint64(file.Size))
return fmt.Sprintf("%s %s %s", sign, fSize, fType)
}