forked from itchio/butler
/
context.go
93 lines (73 loc) · 1.86 KB
/
context.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
package mansion
import (
"context"
"fmt"
"github.com/go-errors/errors"
"github.com/itchio/butler/comm"
"github.com/itchio/wharf/pwr"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type DoCommand func(ctx *Context)
type Context struct {
App *kingpin.Application
Commands map[string]DoCommand
// Identity is the path to the credentials file
Identity string
// Address is the URL of the itch.io API server we're talking to
Address string
// VersionString is the complete version string
VersionString string
// Version is just the version number, as a string
Version string
// Quiet silences all output
Quiet bool
// Verbose enables chatty output
Verbose bool
CompressionAlgorithm string
CompressionQuality int
Cancelled bool
}
func NewContext(app *kingpin.Application) *Context {
return &Context{
App: app,
Commands: make(map[string]DoCommand),
}
}
func (ctx *Context) Register(clause *kingpin.CmdClause, do DoCommand) {
ctx.Commands[clause.FullCommand()] = do
}
func (ctx *Context) Must(err error) {
if err != nil {
switch err := err.(type) {
case *errors.Error:
comm.Die(err.ErrorStack())
default:
comm.Die(err.Error())
}
}
}
func (ctx *Context) UserAgent() string {
return fmt.Sprintf("butler/%s", ctx.VersionString)
}
func (ctx *Context) CompressionSettings() pwr.CompressionSettings {
var algo pwr.CompressionAlgorithm
switch ctx.CompressionAlgorithm {
case "none":
algo = pwr.CompressionAlgorithm_NONE
case "brotli":
algo = pwr.CompressionAlgorithm_BROTLI
case "gzip":
algo = pwr.CompressionAlgorithm_GZIP
case "zstd":
algo = pwr.CompressionAlgorithm_ZSTD
default:
panic(fmt.Errorf("Unknown compression algorithm: %s", algo))
}
return pwr.CompressionSettings{
Algorithm: algo,
Quality: int32(ctx.CompressionQuality),
}
}
func (ctx *Context) Context() context.Context {
return context.Background()
}