Rev 37 | Rev 43 | 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) { |
38 | - | 16 | log.Print("Usage: " + progname + " directory") |
17 | log.Fatal(mesg) |
||
37 | - | 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] |
38 | - | 59 | if len(os.Args) == 2 { |
60 | directory := os.Args[1] |
||
61 | fi, err := os.Stat(directory) |
||
62 | if err != nil { |
||
63 | _ = err // ugly hack to allow not using the err variable |
||
64 | Usage(progname, "ERROR: Not a directory : " + directory) |
||
65 | } |
||
66 | if fi.IsDir() { |
||
67 | start_traverse(directory) |
||
68 | } else { |
||
69 | Usage(progname, "ERROR: Not a directory : " + directory) |
||
70 | } |
||
71 | } else { |
||
37 | - | 72 | Usage(progname, "ERROR: 1 argument expected") |
28 | - | 73 | } |
27 | - | 74 | } |