-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtoken.go
More file actions
46 lines (39 loc) · 1.22 KB
/
token.go
File metadata and controls
46 lines (39 loc) · 1.22 KB
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
package auth
import (
"fmt"
"io"
"github.com/fastly/cli/pkg/argparser"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/lookup"
"github.com/fastly/cli/pkg/text"
)
// TokenCommand prints the active API token to non-terminal stdout.
type TokenCommand struct {
argparser.Base
}
// NewTokenCommand returns a new command registered under the parent.
func NewTokenCommand(parent argparser.Registerer, g *global.Data) *TokenCommand {
var c TokenCommand
c.Globals = g
c.CmdClause = parent.Command("token", "Output the active API token (for use in shell substitutions)")
return &c
}
// Exec implements the command interface.
func (c *TokenCommand) Exec(_ io.Reader, out io.Writer) error {
if text.IsTTY(out) {
return fsterr.RemediationError{
Inner: fmt.Errorf("refusing to print token to a terminal"),
Remediation: "Use this command in a shell substitution or pipe, e.g. $(fastly auth token).",
}
}
token, src := c.Globals.Token()
if src == lookup.SourceUndefined || token == "" {
return fsterr.RemediationError{
Inner: fmt.Errorf("no API token configured"),
Remediation: fsterr.ProfileRemediation(),
}
}
fmt.Fprint(out, token)
return nil
}