Skip to content

Commit

Permalink
Merge pull request #18 from indeedeng/cnmcavoy/gh-17
Browse files Browse the repository at this point in the history
Ignore generated code by default, added -g flag to analyze generated code
  • Loading branch information
cnmcavoy committed Aug 6, 2020
2 parents 23b2559 + 04aec40 commit 9217bfa
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 51 deletions.
20 changes: 8 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,21 @@ linters-settings:
- unnecessaryBlock
settings:
rangeValCopy:
sizeThreshold: 256
sizeThreshold: 512
hugeParam:
sizeThreshold: 256
sizeThreshold: 512
gocyclo:
min-complexity: 16
goimports:
local-prefixes: github.com/golangci/golangci-lint
golint:
min-confidence: 0
govet:
check-shadowing: true
check-shadowing: false
lll:
line-length: 300
maligned:
suggest-new: true
misspell:
locale: US
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped

linters:
disable-all: true
Expand All @@ -56,11 +49,13 @@ linters:
- dogsled
- dupl
- errcheck
- exhaustive
- exportloopref
- gochecknoinits
- goconst
- gocritic
- godot
- gofmt
- goimports
- golint
- goprintffuncname
- gosec
Expand All @@ -76,6 +71,7 @@ linters:
- prealloc
- rowserrcheck
- scopelint
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
Expand All @@ -84,4 +80,4 @@ linters:
- unparam
- unused
- varcheck
- whitespace
- whitespace
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: go

go:
- 1.14.x
- 1.14.x

before_script:
- make lint-install
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 07-15-20
## [1.1.1] - 2020-08-06
### Added
- Added -g flag to instruct go-groups to analyze and include generated code

### Changed
- go-groups now detects generated code and ignores generated code by default

## [1.1.0] - 2020-07-15
### Added
- Added golangci-lint linter, Makefile, and fixed linter warnings

Expand Down
50 changes: 36 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
BIN_DIR ?= $(shell go env GOPATH)/bin
MAKEFLAGS += --warn-undefined-variables
SHELL := bash
.SHELLFLAGS := -euo pipefail -c
.DEFAULT_GOAL := all

default: test
BIN_DIR ?= $(shell go env GOPATH)/bin
export PATH := $(PATH):$(BIN_DIR)

.PHONY: deps
deps: ## download go modules
go mod download

fmt: lint/install # ensure consistent code style
.PHONY: fmt
fmt: lint/check ## ensure consistent code style
go run oss.indeed.com/go/go-groups -w .
gofmt -s -w .
golangci-lint run --fix > /dev/null 2>&1 || true
go mod tidy

lint/install:
.PHONY: lint/check
lint/check:
@if ! golangci-lint --version > /dev/null 2>&1; then \
echo "Installing golangci-lint"; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(BIN_DIR) v1.28.3; \
echo -e "\033[0;33mgolangci-lint is not installed: run \`\033[0;32mmake lint-install\033[0m\033[0;33m\` or install it from https://golangci-lint.run\033[0m"; \
exit 1; \
fi

lint: lint/install ## run golangci-lint
@if ! golangci-lint run; then \
echo "\033[0;33mdetected fmt problems: run \`\033[0;32mmake fmt\033[0m\`"; \
exit 1; \
fi
.PHONY: lint-install
lint-install: ## installs golangci-lint to the go bin dir
@if ! golangci-lint --version > /dev/null 2>&1; then \
echo "Installing golangci-lint"; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(BIN_DIR) v1.30.0; \
fi

.PHONY: lint
lint: lint/check ## run golangci-lint
golangci-lint run
@if [ -n "$$(gofmt -s -l .)" ] || [ -n "$$(go run oss.indeed.com/go/go-groups -d .)" ]; then \
echo -e "\033[0;33mdetected fmt problems: run \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
exit 1; \
fi

.PHONY: test
test: lint ## run go tests
go vet ./...
go test ./... -race

.PHONY: all
all: test

.PHONY: help
help: ## displays this help message
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_\/-]+:.*?## / {printf "\033[34m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | \
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_\/-]+:.*?## / {printf "\033[34m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | \
sort | \
grep -v '#'
grep -v '#'
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ go-groups
[![NetflixOSS Lifecycle](https://img.shields.io/osslifecycle/indeedeng/go-groups.svg)](OSSMETADATA)
[![GitHub](https://img.shields.io/github/license/indeedeng/go-groups.svg)](LICENSE)


# Project Overview

Command `go-groups` is a CLI tool to parse go import blocks, sort, and re-group
Expand All @@ -18,7 +17,6 @@ lexigraphically, it also separates import blocks at a per-project level.
Like `goimports`, `go-groups` also runs gofmt and fixes any style/formatting
issues.


# Getting Started

The `go-groups` command can be installed by running:
Expand All @@ -27,6 +25,20 @@ The `go-groups` command can be installed by running:
$ go get oss.indeed.com/go/go-groups
```

# Usage
```
$ go-groups -h
usage: go-groups [flags] [path ...]
-d display diffs instead of rewriting files
-f disables the automatic gofmt style fixes
-g include generated code in analysis
-l list files whose formatting differs
-v display the version of go-groups
-w write result to (source) file instead of stdout
```

# Formatting Behavior

#### With this example source file input
```
import (
Expand Down Expand Up @@ -55,10 +67,6 @@ import (

Run `go-groups -w ./..` to rewrite and sort import groupings for go source files in a project.

#### More info

`go-groups -h` to print usage help text and available flags

# Asking Questions

For technical questions about `go-groups`, just file an issue in the GitHub tracker.
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ module oss.indeed.com/go/go-groups

go 1.14

require (
github.com/stretchr/testify v1.6.1
)
require github.com/stretchr/testify v1.6.1
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 26 additions & 5 deletions gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package main
*/

import (
"bufio"
"bytes"
"fmt"
"go/scanner"
Expand All @@ -43,6 +44,16 @@ import (
"strings"
)

func isGeneratedCode(src []byte) bool {
scanner := bufio.NewScanner(bytes.NewReader(src))
for scanner.Scan() {
if generatedRegex.MatchString(scanner.Text()) {
return true
}
}
return false
}

// based on https://golang.org/src/cmd/gofmt/gofmt.go with a few modifications

func isGoFile(f os.FileInfo) bool {
Expand All @@ -52,7 +63,7 @@ func isGoFile(f os.FileInfo) bool {
}

// If in == nil, the source is the contents of the file with the given filename.
func processFile(filename string, in io.Reader, out io.Writer, fixFmt bool) error {
func processFile(filename string, in io.Reader, out io.Writer, fixFmt, generatedCode bool) error {
var perm os.FileMode = 0644
if in == nil {
f, err := os.Open(filename)
Expand All @@ -73,6 +84,16 @@ func processFile(filename string, in io.Reader, out io.Writer, fixFmt bool) erro
return err
}

if !generatedCode && isGeneratedCode(src) {
if !*list {
_, err = out.Write(src)
if err != nil {
return err
}
}
return nil
}

if fixFmt {
buffer := bytes.Buffer{}
buffer.Write(src)
Expand All @@ -89,13 +110,13 @@ func processFile(filename string, in io.Reader, out io.Writer, fixFmt bool) erro
if !bytes.Equal(src, res) && rewritten {
// formatting has changed
if *list {
if _, err := fmt.Fprintln(out, filename); err != nil { //nolint:govet
if _, err := fmt.Fprintln(out, filename); err != nil {
return err
}
}
if *write {
// make a temporary backup before overwriting original
bakname, err := backupFile(filename+".", src, perm) //nolint:govet
bakname, err := backupFile(filename+".", src, perm)
if err != nil {
return err
}
Expand All @@ -110,7 +131,7 @@ func processFile(filename string, in io.Reader, out io.Writer, fixFmt bool) erro
}
}
if *doDiff {
data, err := diff(src, res, filename) //nolint:govet
data, err := diff(src, res, filename)
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
Expand All @@ -131,7 +152,7 @@ func processFile(filename string, in io.Reader, out io.Writer, fixFmt bool) erro

func visitFile(path string, f os.FileInfo, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, nil, os.Stdout, !*noFormat)
err = processFile(path, nil, os.Stdout, !*noFormat, *genCode)
}
// Don't complain if a file was deleted in the meantime (i.e.
// the directory changed concurrently while running gofmt).
Expand Down
2 changes: 1 addition & 1 deletion gofmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
"testing"
)

// based on https://golang.org/src/cmd/gofmt/gofmt_test.go with a few modifications
// based on https://golang.org/src/cmd/gofmt/gofmt_test.go with a few modifications.
func TestBackupFile(t *testing.T) {
dir, err := ioutil.TempDir("", "gofmt_test")
if err != nil {
Expand Down
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@ const (
exitBadStdin = 2
exitInternalError = 3

versionStr = "go-groups version 1.1.0 (2020-07-15)"
versionStr = "go-groups version 1.1.1 (2020-08-06)"
)

var (
importStartRegex = regexp.MustCompile(`^\s*import\s*\(\s*$`)
importEndRegex = regexp.MustCompile(`^\s*\)\s*$`)

// any whitespace + any unicode_letter_or_underscore + any unicode_letter_or_underscore_or_unicode number + any whitespace + quote + any + quote + any
// any whitespace + any unicode_letter_or_underscore + any unicode_letter_or_underscore_or_unicode number + any whitespace + quote + any + quote + any.
groupedImportRegex = regexp.MustCompile(`^\s*[\p{L}_\\.]*[\s*[\p{L}_\p{N}]*\s*".*".*$`)
externalImport = regexp.MustCompile(`"([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?\/([\p{L}_\-\p{N}]*)\/?.*"`)
// see https://golang.org/pkg/cmd/go/internal/generate/ for details.
generatedRegex = regexp.MustCompile(`^// Code generated .* DO NOT EDIT.$`)

list = flag.Bool("l", false, "list files whose formatting differs")
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
version = flag.Bool("v", false, "display the version of go-groups")
noFormat = flag.Bool("f", false, "disables the automatic gofmt style fixes")
genCode = flag.Bool("g", false, "include generated code in analysis")
)

func main() {
Expand All @@ -49,7 +52,7 @@ func main() {
_, _ = fmt.Fprintln(os.Stderr, "error: cannot use -w with standard input")
os.Exit(exitBadStdin)
}
if err := processFile("<standard input>", os.Stdin, os.Stdout, !*noFormat); err != nil {
if err := processFile("<standard input>", os.Stdin, os.Stdout, !*noFormat, *genCode); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "failed to parse stdin: "+err.Error())
os.Exit(exitBadFlags)
}
Expand All @@ -68,7 +71,7 @@ func main() {
os.Exit(exitInternalError)
}
default:
_ = processFile(path, nil, os.Stdout, !*noFormat)
_ = processFile(path, nil, os.Stdout, !*noFormat, *genCode)
}
}
}
Expand Down Expand Up @@ -113,7 +116,7 @@ func parse(src []byte) (result []byte, rewritten bool) {
group.lineEnd = n
groups = append(groups, group)
} else if groupedImportRegex.MatchString(line) {
importLine := importLine{ //nolint:govet
importLine := importLine{
line: line,
}
var above, below int
Expand Down Expand Up @@ -220,7 +223,7 @@ func fixupFile(contents map[int]string, numLines int, groups []importGroup) []by
// standard library imports are grouped together and sorted alphabetically
// each second-level external import is grouped together (e.g github.com/pkg.* is one group)
// each of these second-level groups is discovered and sorted alphabetically
// then each import is matched with their group and the list of lines to be written is built up
// then each import is matched with their group and the list of lines to be written is built up.
func regroupImportGroups(group importGroup) importGroup {
standardImports := make(Imports, 0, len(group.lines))

Expand Down
Loading

0 comments on commit 9217bfa

Please sign in to comment.