From 30e0eba7d009bc34121bfe8e2a828f2dae67fed5 Mon Sep 17 00:00:00 2001 From: David Terei Date: Wed, 8 Aug 2018 15:58:26 -0700 Subject: [PATCH 1/3] Use enumer not stringer in usage output --- stringer.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stringer.go b/stringer.go index 6c267e5..0b116a7 100644 --- a/stringer.go +++ b/stringer.go @@ -45,10 +45,10 @@ var ( // Usage is a replacement usage function for the flags package. func Usage() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) - fmt.Fprintf(os.Stderr, "\tstringer [flags] -type T [directory]\n") - fmt.Fprintf(os.Stderr, "\tstringer [flags] -type T files... # Must be a single package\n") + fmt.Fprintf(os.Stderr, "\tenumer [flags] -type T [directory]\n") + fmt.Fprintf(os.Stderr, "\tenumer [flags] -type T files... # Must be a single package\n") fmt.Fprintf(os.Stderr, "For more information, see:\n") - fmt.Fprintf(os.Stderr, "\thttp://godoc.org/golang.org/x/tools/cmd/stringer\n") + fmt.Fprintf(os.Stderr, "\thttps://github.com/alvaroloes/enumer\n") fmt.Fprintf(os.Stderr, "Flags:\n") flag.PrintDefaults() } From a4db2f8dd0f621075bd8aa1d6652647ceaf5d677 Mon Sep 17 00:00:00 2001 From: David Terei Date: Wed, 8 Aug 2018 16:16:25 -0700 Subject: [PATCH 2/3] Support including chosen comments in generated code --- stringer.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/stringer.go b/stringer.go index 0b116a7..f32e9e9 100644 --- a/stringer.go +++ b/stringer.go @@ -31,6 +31,17 @@ import ( "github.com/pascaldekloe/name" ) +type arrayFlags []string + +func (af arrayFlags) String() string { + return strings.Join(af, "") +} + +func (af *arrayFlags) Set(value string) error { + *af = append(*af, value) + return nil +} + var ( typeNames = flag.String("type", "", "comma-separated list of type names; must be set") sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.") @@ -42,6 +53,12 @@ var ( trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"") ) +var comments arrayFlags + +func init() { + flag.Var(&comments, "comment", "comments to include in generated code, can repeat. Default: \"\"") +} + // Usage is a replacement usage function for the flags package. func Usage() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) @@ -88,6 +105,7 @@ func main() { // Print the header and package clause. g.Printf("// Code generated by \"enumer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) g.Printf("\n") + g.Printf("// %s\n", comments.String()) g.Printf("package %s", g.pkg.name) g.Printf("\n") g.Printf("import (\n") From 2615c451cef481904bad24495084d0bfeeb5a3d1 Mon Sep 17 00:00:00 2001 From: David Terei Date: Wed, 8 Aug 2018 16:27:25 -0700 Subject: [PATCH 3/3] Write to tempfile first then rename --- stringer.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/stringer.go b/stringer.go index f32e9e9..5788650 100644 --- a/stringer.go +++ b/stringer.go @@ -126,16 +126,31 @@ func main() { // Format the output. src := g.format() - // Write to file. + // Figure out filename to write to outputName := *output if outputName == "" { baseName := fmt.Sprintf("%s_enumer.go", types[0]) outputName = filepath.Join(dir, strings.ToLower(baseName)) } - err := ioutil.WriteFile(outputName, src, 0644) + + // Write to tmpfile first + tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s_enumer_", types[0])) if err != nil { + log.Fatalf("creating temporary file for output: %s", err) + } + _, err = tmpFile.Write(src) + if err != nil { + tmpFile.Close() + os.Remove(tmpFile.Name()) log.Fatalf("writing output: %s", err) } + tmpFile.Close() + + // Rename tmpfile to output file + err = os.Rename(tmpFile.Name(), outputName) + if err != nil { + log.Fatalf("moving tempfile to output file: %s", err) + } } // isDirectory reports whether the named file is a directory.