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