-
Notifications
You must be signed in to change notification settings - Fork 223
/
logout.go
69 lines (53 loc) · 1.59 KB
/
logout.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
package auth
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/env"
"github.com/superfly/flyctl/internal/state"
)
func newLogout() *cobra.Command {
const (
long = `Log the currently logged-in user out of the Fly platform.
To continue interacting with Fly, the user will need to log in again.
`
short = "Logs out the currently logged in user"
)
return command.New("logout", short, long, runLogout,
command.RequireSession)
}
func runLogout(ctx context.Context) (err error) {
var ac *agent.Client
if ac, err = agent.DefaultClient(ctx); err == nil {
if err = ac.Kill(ctx); err != nil {
err = fmt.Errorf("failed stopping agent: %w", err)
return
}
}
path := state.ConfigFile(ctx)
if err = config.Clear(path); err != nil {
err = fmt.Errorf("failed clearing config file at %s: %w\n", path, err)
return
}
out := iostreams.FromContext(ctx).ErrOut
single := func(key string) {
fmt.Fprintf(out,
"$%s is set in your environment; don't forget to remove it.", key)
}
keyExists := env.IsSet(config.APITokenEnvKey)
tokenExists := env.IsSet(config.AccessTokenEnvKey)
switch {
case keyExists && tokenExists:
const msg = "$%s & $%s are set in your environment; don't forget to remove them.\n"
fmt.Fprintf(out, msg, config.APITokenEnvKey, config.AccessTokenEnvKey)
case keyExists:
single(config.APITokenEnvKey)
case tokenExists:
single(config.AccessTokenEnvKey)
}
return
}