Setup gometalinter + fix lint
This commit is contained in:
		| @@ -4,9 +4,12 @@ install: | |||||||
| - go get github.com/Masterminds/glide | - 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 | - 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/golang/protobuf/protoc-gen-go | ||||||
|  | - go get -u github.com/alecthomas/gometalinter | ||||||
|  | - gometalinter --install | ||||||
| script: | script: | ||||||
| - make install | - make install | ||||||
| - make test | - make test | ||||||
|  | - make lint | ||||||
| cache: | cache: | ||||||
|   directories: |   directories: | ||||||
|   - $HOME/local |   - $HOME/local | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							| @@ -29,3 +29,7 @@ docker.build: | |||||||
| .PHONY: docker.push | .PHONY: docker.push | ||||||
| docker.push: docker.build | docker.push: docker.build | ||||||
| 	docker push moul/protoc-gen-gotemplate | 	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/... | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ import ( | |||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
|  | 	"log" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"os" | 	"os" | ||||||
| 	"os/exec" | 	"os/exec" | ||||||
| @@ -32,18 +33,23 @@ func generate(w http.ResponseWriter, r *http.Request) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		returnError(w, err) | 		returnError(w, err) | ||||||
| 	} | 	} | ||||||
| 	defer os.RemoveAll(dir) // clean up | 	// clean up | ||||||
| 	if err := ioutil.WriteFile(filepath.Join(dir, "example.proto"), []byte(input.Protobuf), 0644); err != nil { | 	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) | 		returnError(w, err) | ||||||
| 		return | 		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) | 		returnError(w, err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// generate | 	// 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() | 	out, err := cmd.CombinedOutput() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		returnError(w, errors.New(string(out))) | 		returnError(w, errors.New(string(out))) | ||||||
| @@ -64,20 +70,32 @@ func returnContent(w http.ResponseWriter, output interface{}) { | |||||||
| 	payload := map[string]interface{}{ | 	payload := map[string]interface{}{ | ||||||
| 		"output": fmt.Sprintf("%s", output), | 		"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.Header().Set("Content-Type", "application/json") | ||||||
| 	w.WriteHeader(http.StatusOK) | 	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) { | func returnError(w http.ResponseWriter, err error) { | ||||||
| 	payload := map[string]interface{}{ | 	payload := map[string]interface{}{ | ||||||
| 		"error": fmt.Sprintf("%v", err), | 		"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.Header().Set("Content-Type", "application/json") | ||||||
| 	w.WriteHeader(http.StatusInternalServerError) | 	w.WriteHeader(http.StatusInternalServerError) | ||||||
| 	w.Write(response) | 	if _, err := w.Write(response); err != nil { | ||||||
|  | 		http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func main() { | func main() { | ||||||
| @@ -94,5 +112,7 @@ func main() { | |||||||
| 	h := handlers.LoggingHandler(os.Stderr, r) | 	h := handlers.LoggingHandler(os.Stderr, r) | ||||||
| 	h = handlers.CompressHandler(h) | 	h = handlers.CompressHandler(h) | ||||||
| 	h = handlers.RecoveryHandler()(h) | 	h = handlers.RecoveryHandler()(h) | ||||||
| 	http.ListenAndServe(addr, r) | 	if err := http.ListenAndServe(addr, h); err != nil { | ||||||
|  | 		panic(err) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										18
									
								
								encoder.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								encoder.go
									
									
									
									
									
								
							| @@ -100,11 +100,20 @@ func (e *GenericTemplateBasedEncoder) templates() ([]string, error) { | |||||||
|  |  | ||||||
| func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) { | func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) { | ||||||
| 	// prepare the ast passed to the template engine | 	// prepare the ast passed to the template engine | ||||||
| 	hostname, _ := os.Hostname() | 	hostname, err := os.Hostname() | ||||||
| 	pwd, _ := os.Getwd() | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	pwd, err := os.Getwd() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
| 	goPwd := "" | 	goPwd := "" | ||||||
| 	if os.Getenv("GOPATH") != "" { | 	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, "../") { | 		if strings.Contains(goPwd, "../") { | ||||||
| 			goPwd = "" | 			goPwd = "" | ||||||
| 		} | 		} | ||||||
| @@ -170,7 +179,8 @@ func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse | |||||||
| 	resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length) | 	resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length) | ||||||
| 	for _, templateFilename := range templates { | 	for _, templateFilename := range templates { | ||||||
| 		go func(tmpl string) { | 		go func(tmpl string) { | ||||||
| 			content, translatedFilename, err := e.buildContent(tmpl) | 			var translatedFilename, content string | ||||||
|  | 			content, translatedFilename, err = e.buildContent(tmpl) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				errChan <- err | 				errChan <- err | ||||||
| 				return | 				return | ||||||
|   | |||||||
| @@ -15,7 +15,7 @@ import ( | |||||||
| 	options "google.golang.org/genproto/googleapis/api/annotations" | 	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 ( | var ( | ||||||
| 	registry *ggdescriptor.Registry // some helpers need access to registry | 	registry *ggdescriptor.Registry // some helpers need access to registry | ||||||
| @@ -32,11 +32,17 @@ var ProtoHelpersFuncMap = template.FuncMap{ | |||||||
| 		return i.String() | 		return i.String() | ||||||
| 	}, | 	}, | ||||||
| 	"json": func(v interface{}) string { | 	"json": func(v interface{}) string { | ||||||
| 		a, _ := json.Marshal(v) | 		a, err := json.Marshal(v) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err.Error() | ||||||
|  | 		} | ||||||
| 		return string(a) | 		return string(a) | ||||||
| 	}, | 	}, | ||||||
| 	"prettyjson": func(v interface{}) string { | 	"prettyjson": func(v interface{}) string { | ||||||
| 		a, _ := json.MarshalIndent(v, "", "  ") | 		a, err := json.MarshalIndent(v, "", "  ") | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err.Error() | ||||||
|  | 		} | ||||||
| 		return string(a) | 		return string(a) | ||||||
| 	}, | 	}, | ||||||
| 	"splitArray": func(sep string, s string) []interface{} { | 	"splitArray": func(sep string, s string) []interface{} { | ||||||
| @@ -315,7 +321,7 @@ func goType(pkg string, f *descriptor.FieldDescriptorProto) string { | |||||||
|  |  | ||||||
| func jsType(f *descriptor.FieldDescriptorProto) string { | func jsType(f *descriptor.FieldDescriptorProto) string { | ||||||
| 	template := "%s" | 	template := "%s" | ||||||
| 	if isFieldRepeated(f) == true { | 	if isFieldRepeated(f) { | ||||||
| 		template = "Array<%s>" | 		template = "Array<%s>" | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										28
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								main.go
									
									
									
									
									
								
							| @@ -18,6 +18,11 @@ var ( | |||||||
| 	registry *ggdescriptor.Registry // some helpers need access to registry | 	registry *ggdescriptor.Registry // some helpers need access to registry | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | const ( | ||||||
|  | 	boolTrue  = "true" | ||||||
|  | 	boolFalse = "false" | ||||||
|  | ) | ||||||
|  |  | ||||||
| func main() { | func main() { | ||||||
| 	g := generator.New() | 	g := generator.New() | ||||||
|  |  | ||||||
| @@ -26,7 +31,7 @@ func main() { | |||||||
| 		g.Error(err, "reading input") | 		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") | 		g.Error(err, "parsing input proto") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -54,37 +59,32 @@ func main() { | |||||||
| 			switch parts[0] { | 			switch parts[0] { | ||||||
| 			case "template_dir": | 			case "template_dir": | ||||||
| 				templateDir = parts[1] | 				templateDir = parts[1] | ||||||
| 				break |  | ||||||
| 			case "destination_dir": | 			case "destination_dir": | ||||||
| 				destinationDir = parts[1] | 				destinationDir = parts[1] | ||||||
| 				break |  | ||||||
| 			case "single-package-mode": | 			case "single-package-mode": | ||||||
| 				switch strings.ToLower(parts[1]) { | 				switch strings.ToLower(parts[1]) { | ||||||
| 				case "true", "t": | 				case boolTrue, "t": | ||||||
| 					singlePackageMode = true | 					singlePackageMode = true | ||||||
| 				case "false", "f": | 				case boolFalse, "f": | ||||||
| 				default: | 				default: | ||||||
| 					log.Printf("Err: invalid value for single-package-mode: %q", parts[1]) | 					log.Printf("Err: invalid value for single-package-mode: %q", parts[1]) | ||||||
| 				} | 				} | ||||||
| 				break |  | ||||||
| 			case "debug": | 			case "debug": | ||||||
| 				switch strings.ToLower(parts[1]) { | 				switch strings.ToLower(parts[1]) { | ||||||
| 				case "true", "t": | 				case boolTrue, "t": | ||||||
| 					debug = true | 					debug = true | ||||||
| 				case "false", "f": | 				case boolFalse, "f": | ||||||
| 				default: | 				default: | ||||||
| 					log.Printf("Err: invalid value for debug: %q", parts[1]) | 					log.Printf("Err: invalid value for debug: %q", parts[1]) | ||||||
| 				} | 				} | ||||||
| 				break |  | ||||||
| 			case "all": | 			case "all": | ||||||
| 				switch strings.ToLower(parts[1]) { | 				switch strings.ToLower(parts[1]) { | ||||||
| 				case "true", "t": | 				case boolTrue, "t": | ||||||
| 					all = true | 					all = true | ||||||
| 				case "false", "f": | 				case boolFalse, "f": | ||||||
| 				default: | 				default: | ||||||
| 					log.Printf("Err: invalid value for debug: %q", parts[1]) | 					log.Printf("Err: invalid value for debug: %q", parts[1]) | ||||||
| 				} | 				} | ||||||
| 				break |  | ||||||
| 			default: | 			default: | ||||||
| 				log.Printf("Err: unknown parameter: %q", param) | 				log.Printf("Err: unknown parameter: %q", param) | ||||||
| 			} | 			} | ||||||
| @@ -104,7 +104,7 @@ func main() { | |||||||
| 	if singlePackageMode { | 	if singlePackageMode { | ||||||
| 		registry = ggdescriptor.NewRegistry() | 		registry = ggdescriptor.NewRegistry() | ||||||
| 		pgghelpers.SetRegistry(registry) | 		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") | 			g.Error(err, "registry: failed to load the request") | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -113,7 +113,7 @@ func main() { | |||||||
| 	for _, file := range g.Request.GetProtoFile() { | 	for _, file := range g.Request.GetProtoFile() { | ||||||
| 		if all { | 		if all { | ||||||
| 			if singlePackageMode { | 			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()) | 					g.Error(err, "registry: failed to lookup file %q", file.GetName()) | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user