-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (122 loc) · 3.39 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"context"
_ "embed"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"syscall"
"github.com/davidmdm/x/xcontext"
)
var (
Version = "v0.0.7"
binaryName = os.Args[0]
)
//go:embed usage.txt
var usage string
func init() {
usage = fmt.Sprintf(usage, binaryName, binaryName)
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", binaryName, err)
os.Exit(1)
}
}
func run() error {
args, subargs := cut(os.Args[1:], "--")
flag.CommandLine.Usage = func() { fmt.Fprintln(os.Stderr, usage) }
flag.CommandLine.Parse(args)
if flag.Arg(0) == "version" {
fmt.Println(Version)
return nil
}
if len(flag.Args()) < 2 {
return errors.New("need two positional arguments: [path] [name]")
}
ctx, cancel := xcontext.WithSignalCancelation(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
path, name := flag.Arg(0), flag.Arg(1)
outputFile, err := func() (string, error) {
segments := strings.Split(name, string([]byte{os.PathSeparator}))
if len(segments) != 1 {
return filepath.Abs(name)
}
gobin, err := GetGoVar(ctx, "GOBIN")
if err != nil {
return "", fmt.Errorf("failed to get GOBIN: %v", err)
}
return filepath.Abs(filepath.Join(gobin, name))
}()
if err != nil {
return fmt.Errorf("failed to determine outputfile for binary: %w", err)
}
build := func() BuildFunc {
if _, err := os.Stat(path); err == nil {
return buildLocalPath
}
return buildRemotePath
}()
return build(ctx, path, outputFile, subargs)
}
type BuildFunc = func(ctx context.Context, path, out string, args []string) error
func buildLocalPath(ctx context.Context, path, out string, args []string) error {
args = append([]string{"build"}, args...)
args = append(args, "-o", out, path)
build := exec.CommandContext(ctx, "go", args...)
build.Stdout, build.Stderr, build.Stdin = os.Stdout, os.Stderr, os.Stdin
return build.Run()
}
func buildRemotePath(ctx context.Context, path, out string, args []string) error {
temp, err := os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to create temporary module: %w", err)
}
defer os.RemoveAll(temp)
cmd := func(ctx context.Context, name string, args ...string) error {
c := exec.CommandContext(ctx, name, args...)
c.Dir = temp
c.Stdout, c.Stderr, c.Stdin = os.Stdout, os.Stderr, os.Stdin
return c.Run()
}
if err := cmd(ctx, "go", "mod", "init", "builder"); err != nil {
return fmt.Errorf("failed to init temporary builder module: %w", err)
}
if err := cmd(ctx, "go", "get", path); err != nil {
return fmt.Errorf("failed to get %s: %w", path, err)
}
base, _, _ := strings.Cut(path, "@")
args = append([]string{"build"}, args...)
args = append(args, "-o", out, base)
if err := cmd(ctx, "go", args...); err != nil {
return fmt.Errorf("failed to install %s: %w", base, err)
}
return nil
}
func GetGoVar(ctx context.Context, name string) (string, error) {
if value := os.Getenv(name); value != "" {
return value, nil
}
output, err := exec.CommandContext(ctx, "go", "env", name).Output()
if err != nil {
return "", err
}
output = bytes.TrimSpace(output)
if len(output) == 0 {
return "", fmt.Errorf("%s not set", name)
}
return string(output), err
}
func cut(args []string, sep string) (before, after []string) {
idx := slices.Index(args, sep)
if idx == -1 {
return args, nil
}
return args[:idx], args[idx+1:]
}