-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (121 loc) · 3.07 KB
/
main.go
File metadata and controls
164 lines (121 loc) · 3.07 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
_ "embed"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var version = "1.2.0"
// ================= EMBED =================
//go:embed flutter-tool.sh
var scriptContent []byte
//go:embed internal/busybox
var busyboxBinary []byte
// ================= MAIN =================
func main() {
if err := run(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}
func run() error {
if isVersionRequest() {
printVersion()
return nil
}
if runtime.GOOS == "windows" {
return errors.New("windows is not supported")
}
tmpDir, err := os.MkdirTemp("", "flutter-tool-")
if err != nil {
return fmt.Errorf("create temp dir: %w", err)
}
defer os.RemoveAll(tmpDir)
scriptPath, err := writeScript(tmpDir)
if err != nil {
return err
}
busyboxBinDir, err := extractBusybox(tmpDir)
if err != nil {
return err
}
cmd := buildCommand(scriptPath, busyboxBinDir)
return cmd.Run()
}
// ================= VERSION =================
func isVersionRequest() bool {
return len(os.Args) > 1 &&
(os.Args[1] == "--version" || os.Args[1] == "-v")
}
func printVersion() {
fmt.Printf(
"flutter-tool (community-distro) v%s (%s/%s)\n",
version,
runtime.GOOS,
runtime.GOARCH,
)
}
// ================= SCRIPT =================
func writeScript(tmpDir string) (string, error) {
path := filepath.Join(tmpDir, "flutter-tool.sh")
if err := os.WriteFile(path, scriptContent, 0755); err != nil {
return "", fmt.Errorf("write script: %w", err)
}
return path, nil
}
// ================= BUSYBOX =================
func extractBusybox(tmpDir string) (string, error) {
binDir := filepath.Join(tmpDir, "bin")
if err := os.MkdirAll(binDir, 0755); err != nil {
return "", fmt.Errorf("create busybox bin dir: %w", err)
}
busyboxPath := filepath.Join(binDir, "busybox")
// Write busybox binary
if err := os.WriteFile(busyboxPath, busyboxBinary, 0755); err != nil {
return "", fmt.Errorf("write busybox binary: %w", err)
}
// Install symlinks for applets
cmd := exec.Command(busyboxPath, "--install", "-s", binDir)
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("busybox --install failed: %w", err)
}
return binDir, nil
}
// ================= COMMAND =================
func buildCommand(scriptPath, busyboxBinDir string) *exec.Cmd {
cmd := exec.Command(
"/bin/bash",
append([]string{scriptPath}, os.Args[1:]...)...,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = buildEnvWithPrependedPath(busyboxBinDir)
return cmd
}
func buildEnvWithPrependedPath(prepend string) []string {
env := os.Environ()
newEnv := make([]string, 0, len(env))
var pathHandled bool
for _, e := range env {
if strings.HasPrefix(e, "PATH=") {
oldPath := strings.TrimPrefix(e, "PATH=")
newEnv = append(newEnv, "PATH="+prepend+":"+oldPath)
pathHandled = true
} else {
newEnv = append(newEnv, e)
}
}
if !pathHandled {
newEnv = append(newEnv, "PATH="+prepend)
}
return newEnv
}