-
Notifications
You must be signed in to change notification settings - Fork 2
/
azl.go
64 lines (53 loc) · 1.41 KB
/
azl.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
package main
import (
"bytes"
_ "embed"
"fmt"
"os"
"os/exec"
"strings"
)
// Embed the Python script at compile time
//
//go:embed packages/azure-local-cli/azure_local_cli/__main__.py
var embeddedScript string
func runAzureLocalCli(args ...string) error {
azPath, err := exec.LookPath("az")
if err != nil || azPath == "" {
fmt.Fprintln(os.Stderr, "Could not find az cli in PATH")
os.Exit(1)
}
cmd := exec.Command(azPath, "--version")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, stderr.String())
return err
}
pythonLocationMatch := ""
for _, line := range strings.Split(strings.ReplaceAll(stdout.String(), "\r\n", "\n"), "\n") {
if strings.HasPrefix(line, "Python location '") {
pythonLocationMatch = strings.TrimPrefix(line, "Python location '")
pythonLocationMatch = strings.TrimSuffix(pythonLocationMatch, "'")
break
}
}
if pythonLocationMatch == "" {
fmt.Fprintln(os.Stderr, "Could not find python location from az cli")
os.Exit(1)
}
pythonCmd := exec.Command(pythonLocationMatch, append([]string{"-c", embeddedScript}, args...)...)
pythonCmd.Stdout = os.Stdout
pythonCmd.Stderr = os.Stderr
if err := pythonCmd.Run(); err != nil {
return err
}
return nil
}
func main() {
if err := runAzureLocalCli(os.Args[1:]...); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}