Some improvements to audiobooker
[cmccabe-bin] / pickrand.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6         "path/filepath"
7         "math/rand"
8         "time"
9 )
10
11 func main() {
12         root := "."
13         if len(os.Args) > 1 {
14                 root = os.Args[1]
15         }
16         files := make([]string, 0, 32)
17         err := filepath.Walk(root, func(p string, f os.FileInfo, err error) error {
18                 if err != nil {
19                         return err
20                 }
21                 if !f.IsDir() {
22                         files = append(files, p)
23                 }
24                 return nil
25         })
26         if err != nil {
27                 fmt.Fprintf(os.Stderr, "** Error: %s\n", err.Error())
28                 os.Exit(1)
29         }
30         rand.Seed(time.Now().UTC().UnixNano())
31         i := rand.Int31n(int32(len(files)))
32         fmt.Printf("%s\n", root + "/" + files[i])
33 }