forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
which.go
70 lines (61 loc) · 1.35 KB
/
which.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
// Copyright 2016-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Which locates a command.
//
// Synopsis:
// which [-a] [COMMAND]...
//
// Options:
// -a: print all matching pathnames of each argument
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)
var (
allPaths = flag.Bool("a", false, "print all matching pathnames of each argument")
verbose = flag.Bool("v", false, "verbose output")
)
func which(writer io.Writer, paths []string, cmds []string, allPaths bool) error {
var foundOne bool
for _, name := range cmds {
for _, p := range paths {
f := filepath.Join(p, name)
if !canExecute(f) {
continue
}
foundOne = true
if _, err := writer.Write([]byte(f + "\n")); err != nil {
return err
}
if !allPaths {
return nil
}
}
}
if !foundOne {
return fmt.Errorf("no suitable executable found")
}
return nil
}
func main() {
flag.Parse()
p := os.Getenv("PATH")
if len(p) == 0 {
// The default bin path of uroot is ubin, fallbacking to it.
log.Print("No path variable found! Fallbacking to /ubin")
p = "/ubin"
}
if err := which(os.Stdout, strings.Split(p, ":"), flag.Args(), *allPaths); err != nil {
if *verbose {
log.Printf("Error: %v", err)
}
os.Exit(1)
}
}