-
Notifications
You must be signed in to change notification settings - Fork 0
/
abspath.go
87 lines (75 loc) · 2.04 KB
/
abspath.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
)
// printAbspath prints the absolute path of the given (relative) path.
// It prints an error message if the path can't be parsed.
func printAbspath(path string) {
abspath, err := filepath.Abs(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't parse file or directory path '%s': %v\n", path, err)
return
}
fmt.Println(abspath)
}
// printPathsFromStdin reads all paths from STDIN (one per line)
// and prints their absolute paths.
func printPathsFromStdin() {
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
printAbspath(input.Text())
}
}
// visit takes a relative path and prints its absolute path.
// It is a helper function used for printing absolute paths recursively
// with filepath.Walk().
func visit(path string, f os.FileInfo, err error) error {
printAbspath(path)
return nil
}
// printAbspathsRecursively prints the absolute path for the given relative
// path. If the given path is a directory, recursively print the absolute
// paths of all files and directories in it.
func printAbspathsRecursively(path string) {
fileInfo, err := os.Lstat(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Path '%s' is not a file/folder: %v\n", path, err)
} else if fileInfo.IsDir() {
filepath.Walk(path, visit)
} else {
printAbspath(path)
}
}
func printUsage() {
fmt.Printf("Usage of %s:\n\n", os.Args[0])
fmt.Printf("\tabspath file1.txt path/to/file2.pdf\n")
fmt.Printf("\tabspath Desktop/*\n")
fmt.Printf("\tabspath -r Desktop\n")
fmt.Printf("\tfind . -name *.pdf | abspath\n\n")
flag.PrintDefaults()
}
func main() {
flag.Usage = printUsage
var recursive bool
flag.BoolVar(&recursive, "r", false,
"recursive: print absolute paths of all files in the directory structure")
flag.Parse()
paths := flag.Args()
if len(paths) == 0 { // no paths given as command-line arguments
printPathsFromStdin()
os.Exit(0)
}
if recursive {
for _, path := range paths {
printAbspathsRecursively(path)
}
} else {
for _, path := range paths {
printAbspath(path)
}
}
}