-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (86 loc) · 2.6 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/alphauslabs/bluectl/pkg/logger"
"github.com/alphauslabs/iam/cmds"
"github.com/alphauslabs/iam/cmds/orgs"
"github.com/alphauslabs/iam/params"
"github.com/fatih/color"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
"google.golang.org/api/idtoken"
)
var (
bold = color.New(color.Bold).SprintFunc()
year = func() string {
return fmt.Sprintf("%v", time.Now().Year())
}
rootCmd = &cobra.Command{
Use: "iam",
Short: bold("iam") + " - Command line interface for iamd",
Long: bold("iam") + ` - Command line interface for our internal IAM service.
Copyright (c) 2023-` + year() + ` Alphaus Cloud, Inc. All rights reserved.
The general form is ` + bold("iam <resource[ subresource...]> <action> [flags]") + `.
To authenticate, either set GOOGLE_APPLICATION_CREDENTIALS env var or
set the --creds-file flag. Ask the service owner if your credentials
file doesn't have access to the service itself.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
params.ServiceHost = params.HostProd
switch params.RunEnv {
case "dev":
params.ServiceHost = params.HostDev
case "next":
params.ServiceHost = params.HostNext
}
ctx := context.Background()
var ts oauth2.TokenSource
var err error
switch {
case params.CredentialsFile != "":
opts := idtoken.WithCredentialsFile(params.CredentialsFile)
ts, err = idtoken.NewTokenSource(ctx, "https://"+params.ServiceHost, opts)
default:
ts, err = idtoken.NewTokenSource(ctx, "https://"+params.ServiceHost)
}
if err != nil {
logger.Error(err)
os.Exit(1)
}
token, err := ts.Token()
if err != nil {
logger.Error(err)
os.Exit(1)
}
params.AccessToken = token.AccessToken
if params.Bare {
logger.SetPrefix(logger.PrefixNone)
}
},
Run: func(cmd *cobra.Command, args []string) {
logger.Info("see -h for more information")
},
}
)
func init() {
rootCmd.Flags().SortFlags = false
rootCmd.PersistentFlags().SortFlags = false
rootCmd.PersistentFlags().BoolVar(¶ms.Bare, "bare", params.Bare, "minimal log output")
rootCmd.PersistentFlags().StringVar(¶ms.CredentialsFile, "creds-file", "", "optional, GCP service account file")
rootCmd.PersistentFlags().StringVar(¶ms.RunEnv, "env", "prod", "dev, next, or prod")
rootCmd.PersistentFlags().StringVar(¶ms.OutFmt, "fmt", "", "json, csv (depending on support)")
rootCmd.AddCommand(
cmds.WhoAmICmd(),
cmds.WhoIsCmd(),
cmds.AllowMeCmd(),
orgs.OrgsCmd(),
)
}
func main() {
cobra.EnableCommandSorting = false
log.SetOutput(os.Stdout)
rootCmd.Execute()
}