Skip to content

Commit b378376

Browse files
authored
Merge pull request #31 from gocsaf/verbose
Implement a --verbose flag
2 parents b69289f + 599ea47 commit b378376

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

cmd/fakedoc/main.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Options struct {
4545
NumOutputs int `long:"num-outputs" short:"n" default:"1" description:"How many documents to generate . If greater than 1, the output filename must be given. It is treated as a template for filenames in which {{$}} will be replaced with the number of the file, starting with 0."`
4646
Formatted bool `long:"format" short:"f" description:"Output JSON should be formatted."`
4747
RequireRegex string `long:"require" description:"Specifies with a regular expression what fields to force as required."`
48+
Verbose bool `long:"verbose" short:"v" description:"Verbose output"`
4849
}
4950

5051
func main() {
@@ -80,7 +81,7 @@ func main() {
8081
opts.OutputFile, opts.LimitsFile,
8182
opts.SizeFactor, opts.ForceMaxSize,
8283
opts.NumOutputs, opts.Formatted,
83-
opts.RequireRegex)
84+
opts.RequireRegex, opts.Verbose)
8485
}))
8586
}
8687

@@ -92,13 +93,17 @@ func generate(
9293
numOutputs int,
9394
formatted bool,
9495
requireFlag string,
96+
verbose bool,
9597
) error {
9698
templ, err := fakedoc.FromCSAFSchema()
9799
if err != nil {
98100
return err
99101
}
100102

101103
if templatefile != "" {
104+
if verbose {
105+
fmt.Printf("Loading template %q\n", templatefile)
106+
}
102107
overrides, err := fakedoc.LoadTemplate(templatefile)
103108
if err != nil {
104109
return err
@@ -108,6 +113,9 @@ func generate(
108113

109114
var limits *fakedoc.Limits
110115
if limitsfile != "" {
116+
if verbose {
117+
fmt.Printf("Loading limits %q\n", limitsfile)
118+
}
111119
if limits, err = fakedoc.LoadLimitsFromFile(limitsfile); err != nil {
112120
return err
113121
}
@@ -121,10 +129,14 @@ func generate(
121129
}
122130

123131
generator := fakedoc.NewGenerator(
124-
templ, limits, sizeFactor, forceMaxSize, rng, requireRegex)
132+
templ, limits, sizeFactor, forceMaxSize, rng, requireRegex, verbose)
125133

126134
if numOutputs == 1 {
127-
return generateToFile(generator, outputfile, formatted)
135+
err := generateToFile(generator, outputfile, formatted)
136+
if outputfile != "" {
137+
generator.Verbosef("\n")
138+
}
139+
return err
128140
}
129141

130142
tmplFilename, err := template.New("filename").Parse(outputfile)
@@ -143,6 +155,7 @@ func generate(
143155
return err
144156
}
145157
}
158+
generator.Verbosef("\n")
146159

147160
return nil
148161
}
@@ -162,6 +175,9 @@ func generateToFile(
162175
outputfile string,
163176
formatted bool,
164177
) error {
178+
if outputfile != "" {
179+
generator.Verbosef("generating %q", outputfile)
180+
}
165181
csaf, err := generator.Generate()
166182
if err != nil {
167183
return err

pkg/fakedoc/generator.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Generator struct {
6363
SizeFactor float64
6464
ForceMaxSize bool
6565
RequireRegex *regexp.Regexp
66+
Verbose bool
6667
Rand *rand.Rand
6768
FileCache map[string]string
6869
NameSpaces map[string]*NameSpace
@@ -186,6 +187,7 @@ func NewGenerator(
186187
forceMaxSize bool,
187188
rng *rand.Rand,
188189
requireRegex *regexp.Regexp,
190+
verbose bool,
189191
) *Generator {
190192
if rng == nil {
191193
seed1, seed2 := rand.Uint64(), rand.Uint64()
@@ -199,12 +201,21 @@ func NewGenerator(
199201
ForceMaxSize: forceMaxSize,
200202
Rand: rng,
201203
RequireRegex: requireRegex,
204+
Verbose: verbose,
202205
FileCache: map[string]string{},
203206
NameSpaces: map[string]*NameSpace{},
204207
namespaceIDErrs: map[string]error{},
205208
}
206209
}
207210

211+
// Verbosef prints a message to stdout if the verbose flag is true. The
212+
// parameters are passed through to fmt.Printf.
213+
func (gen *Generator) Verbosef(format string, a ...any) {
214+
if gen.Verbose {
215+
fmt.Printf(format, a...)
216+
}
217+
}
218+
208219
func (gen *Generator) getNamespace(name string) *NameSpace {
209220
ns := gen.NameSpaces[name]
210221
if ns == nil {
@@ -614,6 +625,7 @@ func (gen *Generator) book(minlength, maxlength int, path string, limits *LimitN
614625
length := minlength + gen.Rand.IntN(maxlength-minlength+1)
615626
content, ok := gen.FileCache[path]
616627
if !ok {
628+
gen.Verbosef("\nLoading book %q\n", path)
617629
file, err := os.Open(path)
618630
if err != nil {
619631
return "", err

0 commit comments

Comments
 (0)