-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.02 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
package main
import (
"fmt"
"os"
"path"
"sort"
)
func main() {
args := os.Args[1:]
if len(args) == 0 {
usage()
}
for _, dir := range args {
handle(dir)
}
}
func check(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "err: %v\n", err)
usage()
}
}
func onlyfiles(dir string) ([]os.DirEntry, error) {
all, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var ret []os.DirEntry
for _, entry := range all {
if entry.Type().IsRegular() {
ret = append(ret, entry)
}
}
return ret, nil
}
func handle(dir string) {
all, err := onlyfiles(dir)
check(err)
if len(all) == 0 {
fmt.Fprintf(os.Stderr, "no files in %s\n", dir)
return
}
sort.Slice(all, func(i, j int) bool {
lhs, err := all[i].Info()
check(err)
rhs, err := all[j].Info()
check(err)
return lhs.ModTime().Before(rhs.ModTime())
})
fmt.Printf("%s\n", path.Join(dir, all[len(all)-1].Name()))
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s directory [ directory [ ... directory] ]\n", os.Args[0])
os.Exit(1)
}