-
Notifications
You must be signed in to change notification settings - Fork 55
/
status.go
163 lines (131 loc) · 3.72 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package status
import (
"fmt"
"os"
"sort"
humanize "github.com/dustin/go-humanize"
"github.com/go-errors/errors"
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
itchio "github.com/itchio/go-itchio"
"github.com/itchio/wharf/state"
"github.com/olekukonko/tablewriter"
)
var args = struct {
target *string
showAllFiles *bool
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("status", "Show a list of channels and the status of their latest and pending builds.")
ctx.Register(cmd, do)
args.target = cmd.Arg("target", "Which user/project to show the status of, for example 'leafo/x-moon'").Required().String()
args.showAllFiles = cmd.Flag("show-all-files", "Show status of all files, not just archive").Bool()
}
func do(ctx *mansion.Context) {
go ctx.DoVersionCheck()
ctx.Must(Do(ctx, *args.target, *args.showAllFiles))
}
func Do(ctx *mansion.Context, specStr string, showAllFiles bool) error {
spec, err := itchio.ParseSpec(specStr)
if err != nil {
return errors.Wrap(err, 1)
}
client, err := ctx.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", "Version", "State"})
found := false
sortedChannelNames := []string{}
for name := range listChannelsResp.Channels {
sortedChannelNames = append(sortedChannelNames, name)
}
sort.Strings(sortedChannelNames)
for _, channelName := range sortedChannelNames {
ch := listChannelsResp.Channels[channelName]
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), versionState(ch.Head), filesState(files, showAllFiles)}
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), versionState(ch.Pending), filesState(files, showAllFiles)}
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.Build) string {
theme := state.GetTheme()
var s string
switch build.State {
case itchio.BuildStateCompleted:
s = fmt.Sprintf("%s #%d", theme.StatSign, build.ID)
case itchio.BuildStateProcessing:
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 versionState(build *itchio.Build) string {
switch build.State {
case itchio.BuildStateCompleted:
if build.UserVersion != "" {
return build.UserVersion
}
return fmt.Sprintf("%d", build.Version)
default:
return ""
}
}
func filesState(files []*itchio.BuildFile, showAllFiles bool) string {
if len(files) == 0 {
return "(no files)"
}
s := ""
for _, file := range files {
if !(showAllFiles || file.Type == itchio.BuildFileTypeArchive) {
continue
}
if s != "" {
s += ", "
}
s += fileState(file)
}
return s
}
func fileState(file *itchio.BuildFile) string {
theme := state.GetTheme()
fType := string(file.Type)
if file.SubType != itchio.BuildFileSubTypeDefault {
fType += fmt.Sprintf(" (%s)", file.SubType)
}
sign := theme.StatSign
if file.State != itchio.BuildFileStateUploaded {
sign = theme.OpSign
}
fSize := humanize.IBytes(uint64(file.Size))
return fmt.Sprintf("%s %s %s", sign, fSize, fType)
}