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

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: Small updates. Created 4 years, 3 months 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 // Quick tool for generating directories to walk.
M-A Ruel 2016/09/15 14:31:02 Move this to a real package docstring.
mithro 2016/09/20 12:41:45 This docstring is only for the tool though? (The w
M-A Ruel 2016/09/20 16:37:27 Yes, but tools still show up in godoc.
mithro 2016/09/22 11:19:59 The part I didn't understand was that you wanted t
8
9 import (
10 "encoding/json"
11 "flag"
12 "fmt"
13 "io/ioutil"
14 "log"
15 "math/rand"
16 "os"
17 )
18
19 type TestSettings struct {
20 Name string
21 Comment string
22 Seed int64
23 Tree TreeSettings
24 }
25
26 var config = flag.String("config", "", "JSON config file for generating test fil e.")
M-A Ruel 2016/09/15 14:31:03 I'm a big fan of putting these inside the main fun
mithro 2016/09/20 12:41:44 Do you have an example? I'm not sure I understand.
M-A Ruel 2016/09/20 16:37:27 Here's the simplest example I could find: https://
mithro 2016/09/22 11:19:58 Done for both tools.
27 var outdir = flag.String("outdir", "", "Where to write the output.")
28 var remove = flag.Bool("remove", false, "Remove the directory if it exists.")
29 var seed = flag.Int64("seed", 4, "Seed for random.")
30
31 func main() {
32 flag.Parse()
33
34 var settings TestSettings
35 settings.Seed = *seed
36
37 configdata, err := ioutil.ReadFile(*config)
M-A Ruel 2016/09/15 14:31:02 I don't think we need that much configurability. A
mithro 2016/09/20 12:41:44 We have 6 configs so far, it is likely we'll end u
M-A Ruel 2016/09/20 16:37:27 Why? - layout tests - compile step - small test I
mithro 2016/09/22 11:19:59 These tests are designed to provide a set of diffe
38 if err != nil {
39 log.Fatal(err)
40 }
41
42 if err := json.Unmarshal(configdata, &settings); err != nil {
43 log.Fatal(err)
44 }
45
46 fmt.Printf("%s: %s\n", settings.Name, settings.Comment)
47 fmt.Println("===============================================")
48 fmt.Println()
49
50 if len(*outdir) == 0 {
51 log.Fatal("No output directory supplied.")
52 }
53
54 if _, err := os.Stat(*outdir); !os.IsNotExist(err) {
55 if !*remove {
56 log.Fatal("directory exists!")
57 } else {
58 if err := os.RemoveAll(*outdir); err != nil {
M-A Ruel 2016/09/15 14:31:02 Remove the remove support. Caller can handle that.
mithro 2016/09/20 12:41:44 Done.
59 log.Fatal(err)
60 }
61 }
62 }
63
64 if *seed != 4 && settings.Seed != *seed {
M-A Ruel 2016/09/15 14:31:02 I don't see why we put settings.Seed. There's no n
mithro 2016/09/20 12:41:45 A config which wants to produce identical output n
M-A Ruel 2016/09/20 16:37:27 Yes but why make it configurable and why not make
mithro 2016/09/22 11:19:58 Seed of 0 is special cased as "no-seed" in most sy
65 log.Fatal("Seed supplied by test config.")
66 }
67
68 r := rand.New(rand.NewSource(settings.Seed))
69
70 // Create the root directory
71 if err := os.MkdirAll(*outdir, 0755); err != nil {
M-A Ruel 2016/09/15 14:31:02 0700
mithro 2016/09/20 12:41:44 Why 0700 rather than 0755?
M-A Ruel 2016/09/20 16:37:27 Because there's no point in other users to read th
mithro 2016/09/22 11:19:59 Done.
72 log.Fatal(err)
73 }
74
75 GenerateTree(r, *outdir, &settings.Tree)
M-A Ruel 2016/09/15 14:31:02 something like: return settings.GenerateTree(*outd
76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698