-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (108 loc) · 2.93 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"time"
"github.com/jessevdk/go-flags"
"github.com/webdevops/go-common/azuresdk/azidentity"
"github.com/webdevops/helm-azure-tpl/config"
)
const (
Author = "webdevops.io"
UserAgent = "helm-azure-tpl/"
TermColumns = 80
)
var (
argparser *flags.Parser
azAccountInfo map[string]interface{}
startTime time.Time
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
)
var (
opts config.Opts
)
func main() {
startTime = time.Now()
initArgparser()
initLogger()
initAzureEnvironment()
run()
}
func initArgparser() {
var err error
argparser = flags.NewParser(&opts, flags.Default)
// check if run by helm
if helmCmd := os.Getenv("HELM_BIN"); helmCmd != "" {
if pluginName := os.Getenv("HELM_PLUGIN_NAME"); pluginName != "" {
argparser.Command.Name = fmt.Sprintf(`%v %v`, helmCmd, pluginName)
}
}
_, err = argparser.Parse()
// check if there is a parse error
if err != nil {
var flagsErr *flags.Error
if ok := errors.As(err, &flagsErr); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
}
func fetchAzAccountInfo() {
cmd := exec.Command("az", "account", "show", "-o", "json")
cmd.Stderr = os.Stderr
accountInfo, err := cmd.Output()
if err != nil {
logger.Fatalf(`unable to detect Azure TenantID via 'az account show': %v`, err)
}
err = json.Unmarshal(accountInfo, &azAccountInfo)
if err != nil {
logger.Fatalf(`unable to parse 'az account show' output: %v`, err)
}
// auto set azure tenant id
if opts.Azure.Environment == nil || *opts.Azure.Environment == "" {
// autodetect tenant
if val, ok := azAccountInfo["environmentName"].(string); ok {
logger.Infof(`use Azure Environment '%v' from 'az account show'`, val)
opts.Azure.Environment = &val
}
}
// auto set azure tenant id
if opts.Azure.Tenant == nil || *opts.Azure.Tenant == "" {
// autodetect tenant
if val, ok := azAccountInfo["tenantId"].(string); ok {
logger.Infof(`use Azure TenantID '%v' from 'az account show'`, val)
opts.Azure.Tenant = &val
}
}
setOsEnvIfUnset(azidentity.EnvAzureEnvironment, *opts.Azure.Environment)
setOsEnvIfUnset(azidentity.EnvAzureTenantID, *opts.Azure.Tenant)
}
func initAzureEnvironment() {
if opts.Azure.Environment == nil || *opts.Azure.Environment == "" {
// autodetect tenant
if val, ok := azAccountInfo["environmentName"].(string); ok {
logger.Infof(`use Azure Environment '%v' from 'az account show'`, val)
opts.Azure.Environment = &val
}
}
if opts.Azure.Environment != nil {
if err := os.Setenv(azidentity.EnvAzureEnvironment, *opts.Azure.Environment); err != nil {
logger.Warnf(`unable to set envvar AZURE_ENVIRONMENT: %v`, err.Error())
}
}
}
func setOsEnvIfUnset(name, value string) {
if envVal := os.Getenv(name); envVal == "" {
if err := os.Setenv(name, value); err != nil {
panic(err)
}
}
}