From 31c6e181db1bb1d6d50124dcb8abfae6fb7a8d6e Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 16 Oct 2019 14:54:40 +0000 Subject: Add go brace whitespace formatter --- _support/Makefile.template | 18 +++++++++++++----- _support/makegen.go | 7 ++++++- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to '_support') diff --git a/_support/Makefile.template b/_support/Makefile.template index 592c1e7f9..e2982a8f6 100644 --- a/_support/Makefile.template +++ b/_support/Makefile.template @@ -159,17 +159,25 @@ lint: {{ .GoLint }} go get golang.org/x/lint/golint@959b441ac422379a43da2230f62be024250818b0 .PHONY: check-formatting -check-formatting: {{ .GoImports }} +check-formatting: {{ .GoImports }} {{ .BraceFmt }} # goimports - @cd {{ .SourceDir }} && goimports -e -l {{ join .GoFiles " " }} | awk '{ print } END { if(NR>0) { print "Formatting error, run make format"; exit(1) } }' + @cd {{ .SourceDir }} && goimports -e -l {{ join .GoFiles " " }} | {{ .MakeFormatCheck }} + # bracefmt + @cd {{ .SourceDir }} && {{ .BraceFmt }} {{ join .GoFiles " " }} | {{ .MakeFormatCheck }} {{ .GoImports }}: go get golang.org/x/tools/cmd/goimports@2538eef75904eff384a2551359968e40c207d9d2 +{{ .BraceFmt }}: + @cd {{ .SourceDir }} && go build -o $@ ./internal/cmd/bracefmt + .PHONY: format -format: {{ .GoImports }} - # In addition to fixing imports, goimports also formats your code in the same style as gofmt - # so it can be used as a replacement. +format: {{ .GoImports }} {{ .BraceFmt }} + # goimports pass 1 + @cd {{ .SourceDir }} && goimports -w -l {{ join .GoFiles " " }} + # bracefmt + @cd {{ .SourceDir }} && {{ .BraceFmt }} -w {{ join .GoFiles " " }} + # goimports pass 2 @cd {{ .SourceDir }} && goimports -w -l {{ join .GoFiles " " }} .PHONY: staticcheck diff --git a/_support/makegen.go b/_support/makegen.go index 3a076317a..6849464f4 100644 --- a/_support/makegen.go +++ b/_support/makegen.go @@ -72,6 +72,7 @@ func (gm *gitalyMake) BuildDir() string { func (gm *gitalyMake) Pkg() string { return "gitlab.com/gitlab-org/gitaly" } func (gm *gitalyMake) GoImports() string { return "bin/goimports" } +func (gm *gitalyMake) BraceFmt() string { return filepath.Join(gm.BuildDir(), "bin/bracefmt") } func (gm *gitalyMake) GoCovMerge() string { return "bin/gocovmerge" } func (gm *gitalyMake) GoLint() string { return "bin/golint" } func (gm *gitalyMake) GoVendor() string { return "bin/govendor" } @@ -249,7 +250,7 @@ func (gm *gitalyMake) GoFiles() []string { } } - if !info.IsDir() && strings.HasSuffix(path, ".go") { + if !info.IsDir() && strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".pb.go") { rel, err := filepath.Rel(root, path) if err != nil { return err @@ -309,6 +310,10 @@ func (gm *gitalyMake) ProtoCSHA256() string { return protoCDownload[runtime.GOOS+"/"+runtime.GOARCH].sha256 } +func (gm *gitalyMake) MakeFormatCheck() string { + return `awk '{ print } END { if(NR>0) { print "Formatting error, run make format"; exit(1) } }'` +} + var templateText = func() string { contents, err := ioutil.ReadFile("../_support/Makefile.template") if err != nil { -- cgit v1.2.3