-
Notifications
You must be signed in to change notification settings - Fork 224
/
client.go
61 lines (47 loc) · 1.11 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
package client
import (
"errors"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flyctl"
"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'")
func New() *Client {
client := &Client{
IO: iostreams.System(),
}
client.InitApi()
return client
}
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 (c *Client) InitApi() bool {
apiToken := flyctl.GetAPIToken()
if apiToken != "" {
apiClient := NewClient(apiToken)
c.api = apiClient
}
return c.Authenticated()
}
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))
}