-
Notifications
You must be signed in to change notification settings - Fork 223
/
debug.go
66 lines (55 loc) · 1.37 KB
/
debug.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
package tokens
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/macaroon"
)
func newDebug() *cobra.Command {
const (
short = "Debug Fly.io API tokens"
long = `Decode and print a Fly.io API token. The token to be
debugged may either be passed in the -t argument or in FLY_API_TOKEN.
See https://github.com/superfly/macaroon for details Fly.io macaroon
tokens.`
usage = "debug"
)
cmd := command.New(usage, short, long, runDebug)
flag.Add(cmd,
flag.String{
Name: "file",
Shorthand: "f",
Description: "Filename to read caveats from. Defaults to stdin",
},
)
return cmd
}
func runDebug(ctx context.Context) error {
toks, err := getTokens(ctx)
if err != nil {
return err
}
macs := make([]*macaroon.Macaroon, 0, len(toks))
for i, tok := range toks {
m, err := macaroon.Decode(tok)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to decode token at position %d: %s\n", i, err)
continue
}
macs = append(macs, m)
}
// encode to buffer to avoid failing halfway through
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
if err := enc.Encode(macs); err != nil {
return fmt.Errorf("unable to encode tokens: %w", err)
}
fmt.Println(buf.String())
return nil
}