/
status.go
122 lines (99 loc) 路 2.9 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
package status
import (
"fmt"
"os"
"sort"
"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"
"github.com/pkg/errors"
)
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.Wrapf(err, "parsing spec %s", spec)
}
client, err := ctx.AuthenticateViaOauth()
if err != nil {
return errors.Wrap(err, "authenticating")
}
listChannelsResp, err := client.ListChannels(spec.Target)
if err != nil {
return errors.Wrap(err, "listing channels")
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Channel", "Upload", "Build", "Version"})
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 {
line := []string{ch.Name, fmt.Sprintf("#%d", ch.Upload.ID), buildState(ch.Head), versionState(ch.Head)}
table.Append(line)
} else {
line := []string{ch.Name, fmt.Sprintf("#%d", ch.Upload.ID), "No builds yet"}
table.Append(line)
}
if ch.Pending != nil {
line := []string{"", "", buildState(ch.Pending), versionState(ch.Pending)}
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 ""
}
}