-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (118 loc) · 2.74 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
package main
import (
"cmp"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"sync"
)
type program struct {
args []string
stdout io.Writer
stderr io.Writer
}
func newProgram(args []string, stdout, stderr io.Writer) *program {
return &program{args: args, stdout: stdout, stderr: stderr}
}
func main() {
program := newProgram(os.Args, os.Stdout, os.Stderr)
status := program.main()
os.Exit(status)
}
func (p *program) main() int {
uncmd, cmd, status := p.parse()
if status > 0 {
return status
}
path, status := p.unpath(uncmd)
if status > 0 {
return status
}
return p.run(cmd, path)
}
func (p *program) parse() (string, []string, int) {
if len(p.args) < 3 {
err := `Usage: {PROGRAM} UNCMD CMD
unpath runs CMD with a modified PATH that does not contain UNCMD.
Arguments:
UNCMD the command to hide from PATH
CMD the command to run with the modified PATH
Examples:
unpath cat ./script script-arg
unpath cat CMD subcmd-arg
unpath cat unpath env CMD`
err = strings.ReplaceAll(err, "{PROGRAM}", p.args[0])
fmt.Fprintf(p.stderr, fmt.Sprintln(err))
return "", nil, 1
}
return p.args[1], p.args[2:], 0
}
type result struct {
dir string
status int
}
func (p *program) unpath(cmd string) (string, int) {
path, _ := os.LookupEnv("PATH")
dirs := strings.Split(path, ":")
newDirs := make([]result, len(dirs))
var wg sync.WaitGroup
for i, dir := range dirs {
wg.Add(1)
go func(i int, dir string) {
entries, _ := os.ReadDir(dir) // ignore errors caused by empty dirs in PATH
n, found := slices.BinarySearchFunc(entries, cmd, func(a fs.DirEntry, b string) int {
return cmp.Compare(a.Name(), b)
})
if found {
dir, status := p.unpathEntry(dir, entries, n)
newDirs[i] = result{dir, status}
} else {
newDirs[i] = result{dir, 0}
}
wg.Done()
}(i, dir)
}
wg.Wait()
for i, result := range newDirs {
if result.status > 0 {
return "", result.status
}
dirs[i] = result.dir
}
return strings.Join(dirs, ":"), 0
}
func (p *program) unpathEntry(dir string, entries []fs.DirEntry, entriesIndex int) (string, int) {
tmpdir, err := os.MkdirTemp("", filepath.Base(dir))
if err != nil {
fmt.Fprintln(p.stderr, err)
return "", 1
}
for i, entry := range entries {
if i == entriesIndex {
continue
}
err := os.Symlink(filepath.Join(dir, entry.Name()), filepath.Join(tmpdir, entry.Name()))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return "", 1
}
}
return tmpdir, 0
}
func (p *program) run(cmd []string, path string) int {
subcmd := exec.Command(cmd[0], cmd[1:]...)
subcmd.Env = append(subcmd.Environ(), fmt.Sprintf("PATH=%s", path))
subcmd.Stdout = p.stdout
subcmd.Stderr = p.stderr
err := subcmd.Run()
if err == nil {
return 0
} else {
return 1
}
}