Skip to content

Commit

Permalink
Merge pull request #16 from indeedeng/release
Browse files Browse the repository at this point in the history
Create 1.1.0 release
  • Loading branch information
cnmcavoy committed Jul 15, 2020
2 parents ef86113 + b98b1e4 commit 23b2559
Show file tree
Hide file tree
Showing 27 changed files with 614 additions and 325 deletions.
87 changes: 87 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
run:
timeout: 3m

linters-settings:
goconst:
min-len: 3
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
- wrapperFunc
- importShadow
- unnamedResult
- unnecessaryBlock
settings:
rangeValCopy:
sizeThreshold: 256
hugeParam:
sizeThreshold: 256
gocyclo:
min-complexity: 16
goimports:
local-prefixes: github.com/golangci/golangci-lint
golint:
min-confidence: 0
govet:
check-shadowing: true
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
enable:
- asciicheck
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- gochecknoinits
- goconst
- gocritic
- gofmt
- goimports
- golint
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- maligned
- misspell
- nakedret
- nolintlint
- prealloc
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
17 changes: 1 addition & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
language: go

go:
- 1.12.x

env:
- GO111MODULE=on

before_install:
- go get golang.org/x/lint/golint

install:
# skip

script:
- go get -v ./...
- go vet ./...
- golint -set_exit_status ./...
- go test -v ./...
- 1.14.x
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

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

### Changed
- go-groups now runs gofmt over the input files, unless disabled by the `-f` flag.

### Fixed
- Fixed go-groups stripping the dot from dot imports (thanks [@jdroot](https://github.com/jdroot))
- Fixed go-groups removing comments and other content from import blocks

## [1.0.3] - 2019-06-20
Initial public release
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
BIN_DIR ?= $(shell go env GOPATH)/bin

default: test

deps: ## download go modules
go mod download

fmt: lint/install # ensure consistent code style
gofmt -s -w .
golangci-lint run --fix > /dev/null 2>&1 || true

lint/install:
@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; \
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

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

help: ## displays this help message
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_\/-]+:.*?## / {printf "\033[34m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | \
sort | \
grep -v '#'
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ the imports.

`go-groups` is similar to `goimports`, but in addition to sorting imports
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
Expand Down Expand Up @@ -51,8 +53,7 @@ import (

#### Typical Workflow

1. Run `go-groups -w ./..` to rewrite and sort import groupings
2. Run `gofmt -w ./..` to format your go source file
Run `go-groups -w ./..` to rewrite and sort import groupings for go source files in a project.

#### More info

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module oss.indeed.com/go/go-groups

go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.6.1
)
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
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/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=
92 changes: 53 additions & 39 deletions gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ package main
import (
"bytes"
"fmt"
"go/scanner"
"io"
"io/ioutil"
"os"
Expand All @@ -44,8 +45,14 @@ import (

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

func isGoFile(f os.FileInfo) bool {
// ignore non-Go files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}

// 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) error {
func processFile(filename string, in io.Reader, out io.Writer, fixFmt bool) error {
var perm os.FileMode = 0644
if in == nil {
f, err := os.Open(filename)
Expand All @@ -66,26 +73,35 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
return err
}

result, rewrite := parse(src)
// nothing to do, file has no imports
if !rewrite {
return nil
if fixFmt {
buffer := bytes.Buffer{}
buffer.Write(src)
cmd := exec.Command("gofmt", "--")
cmd.Stdin = &buffer
cmd.Stderr = os.Stderr
src, err = cmd.Output()
if err != nil {
return err
}
}

// something to do
if !bytes.Equal(src, result) {
res, rewritten := parse(src)
if !bytes.Equal(src, res) && rewritten {
// formatting has changed
if *list {
fmt.Fprintln(out, filename)
if _, err := fmt.Fprintln(out, filename); err != nil { //nolint:govet
return err
}
}
if *write {
// make a temporary backup before overwriting original
bakname, err := backupFile(filename+".", src, perm)
bakname, err := backupFile(filename+".", src, perm) //nolint:govet
if err != nil {
return err
}
err = ioutil.WriteFile(filename, result, perm)
err = ioutil.WriteFile(filename, res, perm)
if err != nil {
os.Rename(bakname, filename)
_ = os.Rename(bakname, filename)
return err
}
err = os.Remove(bakname)
Expand All @@ -94,29 +110,33 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
}
}
if *doDiff {
data, err := diff(src, result, filename)
data, err := diff(src, res, filename) //nolint:govet
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
fmt.Printf("diff -u %s %s\n", filepath.ToSlash(filename+".orig"), filepath.ToSlash(filename))
out.Write(data)
_, err = out.Write(data)
if err != nil {
return err
}
}
}

if !*list && !*write && !*doDiff {
_, err = out.Write(result)
_, err = out.Write(res)
}

return err
}

func visitFile(path string, f os.FileInfo, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, nil, os.Stdout)
err = processFile(path, nil, os.Stdout, !*noFormat)
}
// Don't complain if a file was deleted in the meantime (i.e.
// the directory changed concurrently while running).
// the directory changed concurrently while running gofmt).
if err != nil && !os.IsNotExist(err) {
return err
scanner.PrintError(os.Stderr, err)
}
return nil
}
Expand All @@ -125,28 +145,6 @@ func walkDir(path string) error {
return filepath.Walk(path, visitFile)
}

func isGoFile(f os.FileInfo) bool {
// ignore non-Go files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}

func writeTempFile(dir, prefix string, data []byte) (string, error) {
file, err := ioutil.TempFile(dir, prefix)
if err != nil {
return "", err
}
_, err = file.Write(data)
if err1 := file.Close(); err == nil {
err = err1
}
if err != nil {
os.Remove(file.Name())
return "", err
}
return file.Name(), nil
}

func diff(b1, b2 []byte, filename string) (data []byte, err error) {
f1, err := writeTempFile("", "go-groups", b1)
if err != nil {
Expand Down Expand Up @@ -174,6 +172,22 @@ func diff(b1, b2 []byte, filename string) (data []byte, err error) {
return
}

func writeTempFile(dir, prefix string, data []byte) (string, error) {
file, err := ioutil.TempFile(dir, prefix)
if err != nil {
return "", err
}
_, err = file.Write(data)
if err1 := file.Close(); err == nil {
err = err1
}
if err != nil {
os.Remove(file.Name())
return "", err
}
return file.Name(), nil
}

// replaceTempFilename replaces temporary filenames in diff with actual one.
//
// --- /tmp/gofmt316145376 2017-02-03 19:13:00.280468375 -0500
Expand Down
2 changes: 0 additions & 2 deletions gofmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (
)

// 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
Loading

0 comments on commit 23b2559

Please sign in to comment.