-
Notifications
You must be signed in to change notification settings - Fork 54
/
login.go
44 lines (37 loc) · 1.16 KB
/
login.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
package login
import (
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
"github.com/pkg/errors"
)
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("login", "Connect butler to your itch.io account and save credentials locally.")
ctx.Register(cmd, do)
}
func do(ctx *mansion.Context) {
ctx.Must(Do(ctx))
}
func Do(ctx *mansion.Context) error {
if ctx.HasSavedCredentials() {
client, err := ctx.AuthenticateViaOauth()
if err != nil {
return errors.Wrap(err, "authenticating with saved credentials")
}
_, err = client.WharfStatus()
if err != nil {
return errors.Wrap(err, "validating credentials")
}
comm.Logf("Your local credentials are valid!\n")
comm.Logf("If you want to log in as another account, use the `butler logout` command first, or specify a different credentials path with the `-i` flag.")
comm.Result(map[string]string{"status": "success"})
} else {
// this does the full login flow + saves
_, err := ctx.AuthenticateViaOauth()
if err != nil {
return errors.Wrap(err, "authenticating (no saved credentials)")
}
comm.Result(map[string]string{"status": "success"})
return nil
}
return nil
}