/
main.go
102 lines (88 loc) · 2.7 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
99
100
101
102
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/alphauslabs/bluectl/pkg/logger"
"github.com/alphauslabs/tucp/cmds/curmx"
"github.com/alphauslabs/tucp/cmds/invoice"
"github.com/alphauslabs/tucp/cmds/runtime"
"github.com/alphauslabs/tucp/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: "tucp",
Short: bold("tucp") + " - Command line interface for tucpd",
Long: bold("tucp") + ` - Command line interface for our TrueUnblended Control Plane.
Copyright (c) 2023-` + year() + ` Alphaus Cloud, Inc. All rights reserved.
The general form is ` + bold("tucp <resource[ subresource...]> <action> [flags]") + `.
To authenticate, either set GOOGLE_APPLICATION_CREDENTIALS env var or
set the --creds-file flag. You can use the 'iam' tool to request access
to the 'tucpd-[next|prod]' service, like so:
$ iam allow-me tucpd-prod
You only need to do this once. See https://github.com/alphauslabs/iam
for more information about the 'iam' tool.`,
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.AddCommand(
invoice.InvoiceCmd(),
runtime.RuntimeCmd(),
curmx.CurmxCmd(),
)
}
func main() {
cobra.EnableCommandSorting = false
log.SetOutput(os.Stdout)
rootCmd.Execute()
}