Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed dumpConfig function to optionally save to file #18327

Merged
merged 2 commits into from
Jan 7, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,19 @@ func dumpConfig(ctx *cli.Context) error {
if err != nil {
return err
}
io.WriteString(os.Stdout, comment)
os.Stdout.Write(out)

if ctx.NArg() > 0 {
f, err := os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE, 0644)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're going to need O_TRUNC too

if err != nil {
return err
}
defer f.Close()
f.WriteString(comment)
f.Write(out)
} else {
io.WriteString(os.Stdout, comment)
os.Stdout.Write(out)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minot nitpick, might be a bit cleaner to put the stream into a variable and call the writes only once:

dump := os.Stdout
if ctx.NArg() > 0 {
	dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|O_TRUNC, 0644)
	if err != nil {
		return err
	}
	defer dump.Close()
}
dump.WriteString(comment)
dump.Write(out)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that code looks much cleaner. I've made updates below

return nil
}