Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: common/dirwalk/tests/tools/gendir/main.go

Issue 2054763004: luci-go/common/dirwalk: Code for walking a directory tree efficiently Base URL: https://github.com/luci/luci-go@master
Patch Set: Major rewrite of the code. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "encoding/json"
9 "errors"
10 "flag"
11 "fmt"
12 "io/ioutil"
13 "log"
14 "math/rand"
15 "os"
16 )
17
18 type testSettings struct {
19 Name string
20 Comment string
21 Seed int64
22 Tree treeSettings
23 }
24
25 func mainImpl() error {
26 config := flag.String("config", "", "JSON config file for generating tes t file.")
27 outdir := flag.String("outdir", "", "Where to write the output.")
28 seed := flag.Int64("seed", 4, "Seed for random.")
29 verbose := flag.Bool("v", false, "verbose mode")
30
31 flag.Parse()
32
33 if !*verbose {
34 log.SetOutput(ioutil.Discard)
35 }
36
37 var settings testSettings
38 settings.Seed = *seed
39
40 configdata, err := ioutil.ReadFile(*config)
41 if err != nil {
42 return err
43 }
44
45 if err := json.Unmarshal(configdata, &settings); err != nil {
46 return err
47 }
48
49 fmt.Printf("%s: %s\n", settings.Name, settings.Comment)
50 fmt.Println("===============================================")
51 fmt.Println()
52
53 if len(*outdir) == 0 {
54 return errors.New("no output directory supplied")
55 }
56
57 if _, err := os.Stat(*outdir); !os.IsNotExist(err) {
58 return errors.New("directory exists")
59 }
60
61 if *seed != 4 && settings.Seed != *seed {
62 return errors.New("seed supplied by test config")
63 }
64
65 r := rand.New(rand.NewSource(settings.Seed))
66
67 // Create the root directory
68 if err := os.MkdirAll(*outdir, 0700); err != nil {
69 return err
70 }
71
72 generateTree(r, *outdir, &settings.Tree)
73 return nil
74 }
75
76 func main() {
77 if err := mainImpl(); err != nil {
78 fmt.Fprintf(os.Stderr, "gendir: %s.\n", err)
79 os.Exit(1)
80 }
81 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698