-
Notifications
You must be signed in to change notification settings - Fork 223
/
create.go
104 lines (88 loc) · 2.4 KB
/
create.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package tokens
import (
"context"
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newCreate() *cobra.Command {
const (
short = "Create Fly.io API tokens"
long = "Create Fly.io API tokens"
usage = "create"
)
cmd := command.New(usage, short, long, nil)
cmd.AddCommand(
newDeploy(),
)
return cmd
}
func newDeploy() *cobra.Command {
const (
short = "Create deploy tokens"
long = "Create an API token limited to managing a single app and its resources. Also available as TOKENS DEPLOY. Tokens are valid for 20 years by default. We recommend using a shorter expiry if practical."
usage = "deploy"
)
cmd := command.New(usage, short, long, runDeploy,
command.RequireSession,
command.RequireAppName,
)
flag.Add(cmd,
flag.App(),
flag.JSONOutput(),
flag.String{
Name: "name",
Shorthand: "n",
Description: "Token name",
Default: "flyctl deploy token",
},
flag.Duration{
Name: "expiry",
Shorthand: "x",
Description: "The duration that the token will be valid",
Default: time.Hour * 24 * 365 * 20,
},
)
return cmd
}
func runDeploy(ctx context.Context) (err error) {
appName := appconfig.NameFromContext(ctx)
apiClient := client.FromContext(ctx).API()
app, err := apiClient.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("failed retrieving app %s: %w", appName, err)
}
expiry := ""
if expiryDuration := flag.GetDuration(ctx, "expiry"); expiryDuration != 0 {
expiry = expiryDuration.String()
}
resp, err := gql.CreateLimitedAccessToken(
ctx,
apiClient.GenqClient,
flag.GetString(ctx, "name"),
app.Organization.ID,
"deploy",
&gql.LimitedAccessTokenOptions{
"app_id": app.ID,
},
expiry,
)
if err != nil {
return fmt.Errorf("failed creating deploy token: %w", err)
}
io := iostreams.FromContext(ctx)
if config.FromContext(ctx).JSONOutput {
render.JSON(io.Out, map[string]string{"token": resp.CreateLimitedAccessToken.LimitedAccessToken.TokenHeader})
} else {
fmt.Fprintln(io.Out, resp.CreateLimitedAccessToken.LimitedAccessToken.TokenHeader)
}
return nil
}