diff --git a/cmd/errorer.go b/cmd/errorer.go deleted file mode 100644 index 79db040..0000000 --- a/cmd/errorer.go +++ /dev/null @@ -1,85 +0,0 @@ -package errorer - -import ( - "errorer" - "flag" - "fmt" - "io/ioutil" - "log" - "os" - "path/filepath" - "strings" -) - -var ( - typeNames = flag.String("type", "", "comma-separated list of type names; must be set") - output = flag.String("output", "", "output file name; default srcdir/_errors.go") -) - -func main() { - flag.Parse() - - if len(*typeNames) == 0 { - os.Exit(2) - } - - types := strings.Split(*typeNames, ",") - // We accept either one directory or a list of files. Which do we have? - args := flag.Args() - if len(args) == 0 { - // Default: process whole package in current directory. - args = []string{"."} - } - - // Parse the package once. - var ( - dir string - g errorer.Generator - ) - if len(args) == 1 && isDirectory(args[0]) { - dir = args[0] - g.ParsePackageDir(args[0]) - } else { - dir = filepath.Dir(args[0]) - g.ParsePackageFiles(args) - } - - // Print the header and package clause. - g.Printf("// Code generated by \"errorer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) - g.Printf("\n") - g.Printf("package %s", g.Pkg.GetName()) - g.Printf("\n") - g.Printf("import (\n") - g.Printf("\t\"fmt\"\n") // Used by all methods. - g.Printf("\t\"encoding/json\"\n") // Used by all methods. - g.Printf("\t\"bytes\"\n") - g.Printf(")\n") - - // Run generate for each type. - for _, typeName := range types { - g.Generate(typeName) - } - - // Format the output. - src := g.Format() - - // Write to file. - outputName := *output - if outputName == "" { - baseName := fmt.Sprintf("%s_string.go", types[0]) - outputName = filepath.Join(dir, strings.ToLower(baseName)) - } - err := ioutil.WriteFile(outputName, src, 0644) - if err != nil { - log.Fatalf("writing output: %s", err) - } -} - -// isDirectory reports whether the named file is a directory. -func isDirectory(name string) bool { - info, err := os.Stat(name) - if err != nil { - log.Fatal(err) - } - return info.IsDir() -} diff --git a/e2e_test.go b/e2e_test.go index 50442f6..b1be1cd 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -6,7 +6,7 @@ // +build !android -package errorer +package main import ( "fmt" @@ -33,7 +33,7 @@ func TestEndToEnd(t *testing.T) { defer os.RemoveAll(dir) // Create stringer in temporary directory. stringer := filepath.Join(dir, "stringer.exe") - err = run("go", "build", "-o", stringer, "cmd/errorer.go") + err = run("go", "build", "-o", stringer) if err != nil { t.Fatalf("building stringer: %s", err) } diff --git a/importer18.go b/importer18.go index cffdf8a..d98925f 100644 --- a/importer18.go +++ b/importer18.go @@ -1,6 +1,6 @@ // +build !go1.9 -package errorer +package main import ( "go/importer" diff --git a/importer19.go b/importer19.go index 6a00530..b55419b 100644 --- a/importer19.go +++ b/importer19.go @@ -1,5 +1,5 @@ // +build go1.9 -package errorer +package main import ( "go/importer" diff --git a/jsoner.go b/jsoner.go index 781f8c5..aef7ec7 100644 --- a/jsoner.go +++ b/jsoner.go @@ -1,4 +1,4 @@ -package errorer +package main import "fmt" diff --git a/stringer.go b/stringer.go index 76fa4c5..9c8d06d 100644 --- a/stringer.go +++ b/stringer.go @@ -1,8 +1,9 @@ // Adapted from github.com/golang/tools/cmd/stringer -package errorer +package main import ( "bytes" + "flag" "fmt" "go/ast" "go/build" @@ -11,13 +12,88 @@ import ( "go/parser" "go/token" "go/types" + "io/ioutil" "log" + "os" "path/filepath" "reflect" "sort" "strings" ) +var ( + typeNames = flag.String("type", "", "comma-separated list of type names; must be set") + output = flag.String("output", "", "output file name; default srcdir/_errors.go") +) + +func main() { + flag.Parse() + + if len(*typeNames) == 0 { + os.Exit(2) + } + + types := strings.Split(*typeNames, ",") + // We accept either one directory or a list of files. Which do we have? + args := flag.Args() + if len(args) == 0 { + // Default: process whole package in current directory. + args = []string{"."} + } + + // Parse the package once. + var ( + dir string + g Generator + ) + if len(args) == 1 && isDirectory(args[0]) { + dir = args[0] + g.ParsePackageDir(args[0]) + } else { + dir = filepath.Dir(args[0]) + g.ParsePackageFiles(args) + } + + // Print the header and package clause. + g.Printf("// Code generated by \"errorer %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) + g.Printf("\n") + g.Printf("package %s", g.Pkg.GetName()) + g.Printf("\n") + g.Printf("import (\n") + g.Printf("\t\"fmt\"\n") // Used by all methods. + g.Printf("\t\"encoding/json\"\n") // Used by all methods. + g.Printf("\t\"bytes\"\n") + g.Printf(")\n") + + // Run generate for each type. + for _, typeName := range types { + g.Generate(typeName) + } + + // Format the output. + src := g.Format() + + // Write to file. + outputName := *output + if outputName == "" { + baseName := fmt.Sprintf("%s_string.go", types[0]) + outputName = filepath.Join(dir, strings.ToLower(baseName)) + } + err := ioutil.WriteFile(outputName, src, 0644) + if err != nil { + log.Fatalf("writing output: %s", err) + } +} + +// isDirectory reports whether the named file is a directory. +func isDirectory(name string) bool { + info, err := os.Stat(name) + if err != nil { + log.Fatal(err) + } + return info.IsDir() +} + // Generator holds the state of the analysis. Primarily used to buffer // the output for format.Source. type Generator struct { diff --git a/stringer_test.go b/stringer_test.go index 88e027b..ae9f6b2 100644 --- a/stringer_test.go +++ b/stringer_test.go @@ -1,4 +1,4 @@ -package errorer +package main import ( "strings"