-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (80 loc) · 2.15 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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"github.com/gartnera/gcloud/auth"
"github.com/gartnera/gcloud/config"
"github.com/gartnera/gcloud/container"
"github.com/gartnera/gcloud/helpers"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "gcloud <command> [command-flags] [command-args]",
}
func gcloudFallback() error {
gcloudPath, err := helpers.LookPathPreamble("gcloud", "#!/bin/sh")
if err != nil {
return fmt.Errorf("unable to find gcloud: %w", err)
}
adc, err := auth.ReadApplicationDefaultCredentials()
if err != nil {
return fmt.Errorf("unable to read google application credentials: %w", err)
}
tok, err := adc.Token()
if err != nil {
return fmt.Errorf("unable to get access token: %w", err)
}
tokenFile, err := os.CreateTemp("", "gcloud-token-*")
if err != nil {
return fmt.Errorf("unable to create token file: %w", err)
}
defer os.Remove(tokenFile.Name())
_, err = tokenFile.WriteString(tok.AccessToken)
if err != nil {
return fmt.Errorf("unable to write token to file: %w", err)
}
_ = tokenFile.Close()
accessTokenArg := fmt.Sprintf("--access-token-file=%s", tokenFile.Name())
args := []string{accessTokenArg}
args = append(args, os.Args[1:]...)
cmd := exec.Cmd{
Path: gcloudPath,
Args: args,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
return cmd.Run()
}
func maybeFallback() {
if os.Getenv("GCLOUD_NO_FALLBACK") != "" {
return
}
targetCmd, _, _ := rootCmd.Find(os.Args[1:])
if targetCmd == nil || targetCmd == rootCmd || len(targetCmd.Commands()) > 0 {
err := gcloudFallback()
if err != nil {
var exerr *exec.ExitError
if errors.As(err, &exerr) {
os.Exit(exerr.ExitCode())
}
fmt.Printf("ERROR: %v", err)
}
os.Exit(0)
return
}
}
func main() {
rootCmd.AddCommand(auth.GetRootCmd())
rootCmd.AddCommand(config.GetRootCmd())
rootCmd.AddCommand(container.GetRootCmd())
// automatically fallback to google provided gcloud if we don't have a matching command
// fallback for unknown commands, root commands, and intermediate commands (commands that have multiple children)
maybeFallback()
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}