lib/xexec: rm -rf

This commit is contained in:
Anmol Sethi 2022-12-01 11:05:00 -08:00
parent 6139c35af2
commit 820226c72b
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
4 changed files with 4 additions and 59 deletions

View file

@ -10,7 +10,7 @@ import (
"os/exec"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/lib/xexec"
"oss.terrastruct.com/util-go/xexec"
)
// plugins contains the bundled d2 plugins.

2
go.mod generated
View file

@ -23,7 +23,7 @@ require (
nhooyr.io/websocket v1.8.7
oss.terrastruct.com/cmdlog v0.0.0-20221201100934-012c01b3431c
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541
oss.terrastruct.com/util-go v0.0.0-20221201185848-8cc30ca56bbe
oss.terrastruct.com/util-go v0.0.0-20221201190418-569dcbf6dc3f
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f
oss.terrastruct.com/xdefer v0.0.0-20221017222355-6f3b6e4d1557
oss.terrastruct.com/xjson v0.0.0-20221018000420-4986731c4c4a

2
go.sum generated
View file

@ -804,6 +804,8 @@ oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541 h1:I9B1O1IJ6spivIQ
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541/go.mod h1:ags2QDy/T6jr69hT6bpmAmhr2H98n9o8Atf3QlUJPiU=
oss.terrastruct.com/util-go v0.0.0-20221201185848-8cc30ca56bbe h1:1CTXmBqea1vbVhYsyZ3NiCYUFZgURIQF/ItjrdlhwlE=
oss.terrastruct.com/util-go v0.0.0-20221201185848-8cc30ca56bbe/go.mod h1:bL3CBn27CtTm++1iqRh2p6f8AIWeTdtlTN199Kg9JYM=
oss.terrastruct.com/util-go v0.0.0-20221201190418-569dcbf6dc3f h1:ADVDj5TeMMxXMEKAxC9RKLCQq4gaW6GKynAeojB9VQQ=
oss.terrastruct.com/util-go v0.0.0-20221201190418-569dcbf6dc3f/go.mod h1:bL3CBn27CtTm++1iqRh2p6f8AIWeTdtlTN199Kg9JYM=
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f h1:7voRCwKM7TZkTo9u7hj+uV/zXoVB8czWrTq6MVIh3dg=
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f/go.mod h1:Y0coTLsWwX0q3a+/Ndq797t+vWyxm42T49Ik3bzaDKY=
oss.terrastruct.com/xdefer v0.0.0-20221017222355-6f3b6e4d1557 h1:rPbhJbN1q7B4tnppSPoAMwq0t6Pk5SrQDQ5S6uoNNHg=

View file

@ -1,57 +0,0 @@
package xexec
import (
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
)
// findExecutable is from package exec
func findExecutable(file string) error {
d, err := os.Stat(file)
if err != nil {
return err
}
if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
return nil
}
return fs.ErrPermission
}
// SearchPath searches for all executables that have prefix in their names in
// the directories named by the PATH environment variable.
func SearchPath(prefix string) ([]string, error) {
var matches []string
envPath := os.Getenv("PATH")
dirSet := make(map[string]struct{})
for _, dir := range filepath.SplitList(envPath) {
if dir == "" {
// From exec package:
// Unix shell semantics: path element "" means "."
dir = "."
}
if _, ok := dirSet[dir]; ok {
continue
}
dirSet[dir] = struct{}{}
files, err := os.ReadDir(dir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
return nil, err
}
for _, f := range files {
if strings.HasPrefix(f.Name(), prefix) {
match := filepath.Join(dir, f.Name())
if err := findExecutable(match); err == nil {
matches = append(matches, match)
}
}
}
}
return matches, nil
}