forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.go
107 lines (93 loc) · 2.4 KB
/
ls.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
// Copyright 2013-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.
// ls prints the contents of a directory.
//
// Synopsis:
// ls [OPTIONS] [DIRS]...
//
// Options:
// -l: long form
// -Q: quoted
// -R: equivalent to findutil's find
//
// Bugs:
// With the `-R` flag, directories are only ever printed once.
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"text/tabwriter"
flag "github.com/spf13/pflag"
"github.com/u-root/u-root/pkg/ls"
)
var (
all = flag.BoolP("all", "a", false, "show hidden files")
human = flag.BoolP("human-readable", "h", false, "human readable sizes")
long = flag.BoolP("long", "l", false, "long form")
quoted = flag.BoolP("quote-name", "Q", false, "quoted")
recurse = flag.BoolP("recursive", "R", false, "equivalent to findutil's find")
)
func listName(stringer ls.Stringer, d string, w io.Writer, prefix bool) error {
return filepath.Walk(d, func(path string, osfi os.FileInfo, err error) error {
// Soft error. Useful when a permissions are insufficient to
// stat one of the files.
if err != nil {
log.Printf("%s: %v\n", path, err)
return nil
}
fi := ls.FromOSFileInfo(path, osfi)
if *recurse {
// Mimic find command
fi.Name = path
} else if path == d {
// Starting directory is a dot when non-recursive
if osfi.IsDir() {
fi.Name = "."
if prefix {
fmt.Printf("%q\n", d)
}
}
}
// Hide .files unless -a was given
if *all || fi.Name[0] != '.' {
// Print the file in the proper format.
fmt.Fprintln(w, stringer.FileString(fi))
}
// Skip directories when non-recursive.
if path != d && fi.Mode.IsDir() && !*recurse {
return filepath.SkipDir
}
return nil
})
}
func main() {
flag.Parse()
// Write output in tabular form.
w := &tabwriter.Writer{}
w.Init(os.Stdout, 0, 0, 1, ' ', 0)
defer w.Flush()
var s ls.Stringer = ls.NameStringer{}
if *quoted {
s = ls.QuotedStringer{}
}
if *long {
s = ls.LongStringer{Human: *human, Name: s}
}
// Array of names to list.
names := flag.Args()
if len(names) == 0 {
names = []string{"."}
}
// Is a name a directory? If so, list it in its own section.
prefix := len(names) > 1
for _, d := range names {
if err := listName(s, d, w, prefix); err != nil {
log.Printf("error while listing %#v: %v", d, err)
}
w.Flush()
}
}