Skip to content

Commit

Permalink
Merge pull request #82 from moul/dev/moul/lint
Browse files Browse the repository at this point in the history
Setup gometalinter + fix lint
  • Loading branch information
moul committed Dec 28, 2017
2 parents f84ba57 + c64e1d8 commit 7e17e43
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ install:
- go get github.com/Masterminds/glide
- wget https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/.travis/install-protoc.sh && chmod +x install-protoc.sh && ./install-protoc.sh 3.2.0
- go get -u github.com/golang/protobuf/protoc-gen-go
- go get -u github.com/alecthomas/gometalinter
- gometalinter --install
script:
- make install
- make test
- make lint
cache:
directories:
- $HOME/local
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ docker.build:
.PHONY: docker.push
docker.push: docker.build
docker push moul/protoc-gen-gotemplate

.PHONY: lint
lint:
gometalinter --disable-all --enable=errcheck --enable=vet --enable=vetshadow --enable=golint --enable=gas --enable=ineffassign --enable=goconst --enable=goimports --enable=gofmt --exclude="Binds to all network interfaces" --exclude="should have comment" --enable=staticcheck --enable=gosimple --enable=misspell --deadline=120s . ./cmd/... ./helpers/...
38 changes: 29 additions & 9 deletions cmd/web-editor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -32,18 +33,23 @@ func generate(w http.ResponseWriter, r *http.Request) {
if err != nil {
returnError(w, err)
}
defer os.RemoveAll(dir) // clean up
if err := ioutil.WriteFile(filepath.Join(dir, "example.proto"), []byte(input.Protobuf), 0644); err != nil {
// clean up
defer func() {
if err = os.RemoveAll(dir); err != nil {
log.Printf("error: failed to remove temporary directory: %v", err)
}
}()
if err = ioutil.WriteFile(filepath.Join(dir, "example.proto"), []byte(input.Protobuf), 0644); err != nil {
returnError(w, err)
return
}
if err := ioutil.WriteFile(filepath.Join(dir, "example.output.tmpl"), []byte(input.Template), 0644); err != nil {
if err = ioutil.WriteFile(filepath.Join(dir, "example.output.tmpl"), []byte(input.Template), 0644); err != nil {
returnError(w, err)
return
}

// generate
cmd := exec.Command("protoc", "-I"+dir, "--gotemplate_out=template_dir="+dir+",debug=true:"+dir, filepath.Join(dir, "example.proto"))
cmd := exec.Command("protoc", "-I"+dir, "--gotemplate_out=template_dir="+dir+",debug=true:"+dir, filepath.Join(dir, "example.proto")) // #nosec
out, err := cmd.CombinedOutput()
if err != nil {
returnError(w, errors.New(string(out)))
Expand All @@ -64,20 +70,32 @@ func returnContent(w http.ResponseWriter, output interface{}) {
payload := map[string]interface{}{
"output": fmt.Sprintf("%s", output),
}
response, _ := json.Marshal(payload)
response, err := json.Marshal(payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(response)
if _, err := w.Write(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func returnError(w http.ResponseWriter, err error) {
payload := map[string]interface{}{
"error": fmt.Sprintf("%v", err),
}
response, _ := json.Marshal(payload)
response, err := json.Marshal(payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write(response)
if _, err := w.Write(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func main() {
Expand All @@ -94,5 +112,7 @@ func main() {
h := handlers.LoggingHandler(os.Stderr, r)
h = handlers.CompressHandler(h)
h = handlers.RecoveryHandler()(h)
http.ListenAndServe(addr, r)
if err := http.ListenAndServe(addr, h); err != nil {
panic(err)
}
}
18 changes: 14 additions & 4 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,20 @@ func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {

func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
// prepare the ast passed to the template engine
hostname, _ := os.Hostname()
pwd, _ := os.Getwd()
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
goPwd := ""
if os.Getenv("GOPATH") != "" {
goPwd, _ = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
goPwd, err = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
if err != nil {
return nil, err
}
if strings.Contains(goPwd, "../") {
goPwd = ""
}
Expand Down Expand Up @@ -170,7 +179,8 @@ func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse
resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length)
for _, templateFilename := range templates {
go func(tmpl string) {
content, translatedFilename, err := e.buildContent(tmpl)
var translatedFilename, content string
content, translatedFilename, err = e.buildContent(tmpl)
if err != nil {
errChan <- err
return
Expand Down
14 changes: 10 additions & 4 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
options "google.golang.org/genproto/googleapis/api/annotations"
)

var jsReservedRe *regexp.Regexp = regexp.MustCompile(`(^|[^A-Za-z])(do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)($|[^A-Za-z])`)
var jsReservedRe = regexp.MustCompile(`(^|[^A-Za-z])(do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)($|[^A-Za-z])`)

var (
registry *ggdescriptor.Registry // some helpers need access to registry
Expand All @@ -32,11 +32,17 @@ var ProtoHelpersFuncMap = template.FuncMap{
return i.String()
},
"json": func(v interface{}) string {
a, _ := json.Marshal(v)
a, err := json.Marshal(v)
if err != nil {
return err.Error()
}
return string(a)
},
"prettyjson": func(v interface{}) string {
a, _ := json.MarshalIndent(v, "", " ")
a, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err.Error()
}
return string(a)
},
"splitArray": func(sep string, s string) []interface{} {
Expand Down Expand Up @@ -315,7 +321,7 @@ func goType(pkg string, f *descriptor.FieldDescriptorProto) string {

func jsType(f *descriptor.FieldDescriptorProto) string {
template := "%s"
if isFieldRepeated(f) == true {
if isFieldRepeated(f) {
template = "Array<%s>"
}

Expand Down
28 changes: 14 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ var (
registry *ggdescriptor.Registry // some helpers need access to registry
)

const (
boolTrue = "true"
boolFalse = "false"
)

func main() {
g := generator.New()

Expand All @@ -26,7 +31,7 @@ func main() {
g.Error(err, "reading input")
}

if err := proto.Unmarshal(data, g.Request); err != nil {
if err = proto.Unmarshal(data, g.Request); err != nil {
g.Error(err, "parsing input proto")
}

Expand Down Expand Up @@ -54,37 +59,32 @@ func main() {
switch parts[0] {
case "template_dir":
templateDir = parts[1]
break
case "destination_dir":
destinationDir = parts[1]
break
case "single-package-mode":
switch strings.ToLower(parts[1]) {
case "true", "t":
case boolTrue, "t":
singlePackageMode = true
case "false", "f":
case boolFalse, "f":
default:
log.Printf("Err: invalid value for single-package-mode: %q", parts[1])
}
break
case "debug":
switch strings.ToLower(parts[1]) {
case "true", "t":
case boolTrue, "t":
debug = true
case "false", "f":
case boolFalse, "f":
default:
log.Printf("Err: invalid value for debug: %q", parts[1])
}
break
case "all":
switch strings.ToLower(parts[1]) {
case "true", "t":
case boolTrue, "t":
all = true
case "false", "f":
case boolFalse, "f":
default:
log.Printf("Err: invalid value for debug: %q", parts[1])
}
break
default:
log.Printf("Err: unknown parameter: %q", param)
}
Expand All @@ -104,7 +104,7 @@ func main() {
if singlePackageMode {
registry = ggdescriptor.NewRegistry()
pgghelpers.SetRegistry(registry)
if err := registry.Load(g.Request); err != nil {
if err = registry.Load(g.Request); err != nil {
g.Error(err, "registry: failed to load the request")
}
}
Expand All @@ -113,7 +113,7 @@ func main() {
for _, file := range g.Request.GetProtoFile() {
if all {
if singlePackageMode {
if _, err := registry.LookupFile(file.GetName()); err != nil {
if _, err = registry.LookupFile(file.GetName()); err != nil {
g.Error(err, "registry: failed to lookup file %q", file.GetName())
}
}
Expand Down

0 comments on commit 7e17e43

Please sign in to comment.