Rev 28 | Rev 38 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
28 | - | 1 | //usr/bin/env go run $0 $@; exit $? |
27 | - | 2 | package main |
3 | |||
4 | import ( |
||
5 | "fmt" |
||
6 | "io/ioutil" |
||
7 | "log" |
||
8 | "os" |
||
9 | ) |
||
10 | |||
28 | - | 11 | // ============================================================================= |
12 | // Functions |
||
13 | // ============================================================================= |
||
14 | |||
37 | - | 15 | func Usage(progname, mesg string) { |
16 | log.Print ("Usage: " + progname + " directory") |
||
17 | log.Fatal (mesg) |
||
18 | } |
||
19 | |||
27 | - | 20 | func traverse(s string) { |
21 | curdir, err := os.Getwd() |
||
22 | if err := os.Chdir(s); err != nil { |
||
23 | log.Fatal(err) |
||
24 | } |
||
25 | if err != nil { |
||
26 | log.Fatal(err) |
||
27 | } |
||
28 | curdir2, err := os.Getwd() |
||
29 | files, err := ioutil.ReadDir(curdir2) |
||
30 | if err != nil { |
||
31 | log.Fatal(err) |
||
32 | } |
||
33 | for _, f := range files { |
||
34 | fmt.Println (curdir2 + "/" + f.Name()) |
||
35 | //fmt.Println(f.Name()) |
||
36 | fi, err := os.Stat(f.Name()) |
||
37 | if err != nil { |
||
38 | log.Fatal(err) |
||
39 | } |
||
40 | mode := fi.Mode(); |
||
41 | if mode.IsDir() { |
||
42 | traverse(fi.Name()) |
||
43 | } |
||
44 | } |
||
45 | os.Chdir(curdir) |
||
46 | } |
||
47 | |||
48 | func start_traverse(s string) { |
||
49 | fmt.Println(s) |
||
50 | traverse(s) |
||
51 | } |
||
52 | |||
28 | - | 53 | // ============================================================================= |
54 | // MAIN |
||
55 | // ============================================================================= |
||
56 | |||
27 | - | 57 | func main() { |
37 | - | 58 | progname := os.Args[0] |
28 | - | 59 | if len(os.Args) < 2 { |
37 | - | 60 | Usage(progname, "ERROR: 1 argument expected") |
28 | - | 61 | } |
37 | - | 62 | directory := os.Args[1] |
63 | fi, err := os.Stat(directory) |
||
64 | if err != nil { |
||
65 | _ = err // ugly hack to allow not using the err variable |
||
66 | Usage(progname, "ERROR: Not a directory : " + directory) |
||
27 | - | 67 | } |
68 | if fi.IsDir() { |
||
37 | - | 69 | start_traverse(directory) |
27 | - | 70 | } else { |
37 | - | 71 | Usage(progname, "ERROR: Not a directory : " + directory) |
27 | - | 72 | } |
73 | |||
74 | } |