Skip to content

Commit

Permalink
Change package name
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Tan committed Nov 8, 2017
1 parent ac7c2bb commit 30c787d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 92 deletions.
85 changes: 0 additions & 85 deletions cmd/errorer.go

This file was deleted.

4 changes: 2 additions & 2 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// +build !android

package errorer
package main

import (
"fmt"
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion importer18.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !go1.9

package errorer
package main

import (
"go/importer"
Expand Down
2 changes: 1 addition & 1 deletion importer19.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build go1.9
package errorer
package main

import (
"go/importer"
Expand Down
2 changes: 1 addition & 1 deletion jsoner.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errorer
package main

import "fmt"

Expand Down
78 changes: 77 additions & 1 deletion stringer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Adapted from github.com/golang/tools/cmd/stringer
package errorer
package main

import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/build"
Expand All @@ -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/<type>_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 {
Expand Down
2 changes: 1 addition & 1 deletion stringer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errorer
package main

import (
"strings"
Expand Down

0 comments on commit 30c787d

Please sign in to comment.