-
Notifications
You must be signed in to change notification settings - Fork 224
/
client.go
73 lines (61 loc) · 1.56 KB
/
client.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
package client
import (
"errors"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/logger"
"github.com/superfly/flyctl/iostreams"
)
var ErrNoAuthToken = errors.New("No access token available. Please login with 'flyctl auth login'")
type Client struct {
IO *iostreams.IOStreams // TODO: remove
api *api.Client
}
func (c *Client) API() *api.Client {
return c.api
}
func (c *Client) Authenticated() bool {
return c.api != nil
}
func FromToken(token string) *Client {
var ac *api.Client
if token != "" {
ac = NewClient(token)
}
return &Client{
api: ac,
}
}
func NewClient(token string) *api.Client {
return api.NewClient(token, buildinfo.Name(), buildinfo.Version().String(), logger.FromEnv(iostreams.System().ErrOut))
}
type NewClientOpts struct {
Token string
ClientName string
ClientVersion string
Logger api.Logger
}
// non-flyctl libraries use this when needing to specify logger, client name, and client version
func NewClientWithOptions(opts *NewClientOpts) *Client {
var log api.Logger
if opts.Logger != nil {
log = opts.Logger
} else {
log = logger.FromEnv(iostreams.System().ErrOut)
}
clientName := buildinfo.Name()
if opts.ClientName != "" {
clientName = opts.ClientName
}
clientVersion := buildinfo.Version().String()
if opts.ClientVersion != "" {
clientVersion = opts.ClientVersion
}
var apiClient *api.Client
if opts.Token != "" {
apiClient = api.NewClient(opts.Token, clientName, clientVersion, log)
}
return &Client{
api: apiClient,
}
}