-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (76 loc) · 1.58 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
)
const code = 111
func usage() {
fmt.Println("envdir: usage: envdir dir child")
os.Exit(code)
}
func die(err error) {
fmt.Println(err)
os.Exit(code)
}
func envVar(name, val string) string {
return name + "=" + val
}
func envv(environ []string, dir string) ([]string, error) {
files, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("unable to read directory: %w", err)
}
var extra [][2]string
for _, file := range files {
contents, err := os.ReadFile(filepath.Join(dir, file.Name()))
if err != nil {
return nil, fmt.Errorf("unable to read file: %w", err)
}
name := file.Name()
trimmed := strings.TrimRight(string(contents), " \t\n")
extra = append(extra, [2]string{name, trimmed})
}
out := make([]string, 0, len(environ)+len(extra))
for _, e := range environ {
found := false
for _, v := range extra {
if strings.HasPrefix(e, v[0]) {
found = true
}
}
if !found {
out = append(out, e)
}
}
for _, e := range extra {
if e[1] != "" {
out = append(out, envVar(e[0], e[1]))
}
}
return out, nil
}
func main() {
runtime.LockOSThread()
args := os.Args
if len(args) < 3 {
usage()
}
dir, exe, argv := args[1], args[2], args[2:]
ev, err := envv(os.Environ(), dir)
if err != nil {
die(err)
return
}
pathExe, err := exec.LookPath(exe)
if err != nil {
die(fmt.Errorf("unable to find executable in path: %w", err))
}
if err := syscall.Exec(pathExe, argv, ev); err != nil {
die(fmt.Errorf("unable to exec: %w", err))
}
}