-
Notifications
You must be signed in to change notification settings - Fork 9
/
circlecitoken.go
54 lines (45 loc) · 1.44 KB
/
circlecitoken.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package auth
import (
"context"
"fmt"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/auth"
"namespacelabs.dev/foundation/internal/circleci"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
)
func NewExchangeCircleCITokenCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "exchange-circleci-token",
Short: "Generate a Namespace Cloud token from a CircleCI JWT.",
Args: cobra.NoArgs,
Hidden: true,
}
return fncobra.Cmd(cmd).Do(func(ctx context.Context) error {
if !circleci.IsRunningInCircleci() {
return fnerrors.New("not running in CircleCI")
}
token, err := circleci.GetOidcTokenV2()
if err != nil {
return err
}
res, err := fnapi.ExchangeCircleciToken(ctx, token)
if err != nil {
return err
}
if res.Tenant != nil {
if res.Tenant.Name != "" {
fmt.Fprintf(console.Stdout(ctx), "You are now logged into workspace %q, have a nice day.\n", res.Tenant.Name)
}
if res.Tenant.AppUrl != "" {
fmt.Fprintf(console.Stdout(ctx), "You can inspect you clusters at %s\n", res.Tenant.AppUrl)
}
}
return auth.StoreTenantToken(res.TenantToken)
})
}