Skip to content

Commit

Permalink
deprecation of -file-path arg; renaming -local arg into -company-pref…
Browse files Browse the repository at this point in the history
…ixes; custom sorting for imports groups (#79)

* remove -file-path arg & rename -local arg into -company-prefixes
* custom import groups ordering
  • Loading branch information
incu6us authored Aug 3, 2022
1 parent 8596f94 commit 979c6e7
Show file tree
Hide file tree
Showing 16 changed files with 530 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ build-lint-linux-arm64:

.PHONY: update-std-package-list
update-std-package-list:
@go run -tags gen github.com/incu6us/goimports-reviser/v2/pkg/std/gen
@go run -tags gen github.com/incu6us/goimports-reviser/v3/pkg/std/gen
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
!['logo'](./images/reviser-muscot_200.png)


Tool for Golang to sort goimports by 3-4 groups(with own [linter](linter/README.md)): std, general, local(which is optional) and project dependencies.
Tool for Golang to sort goimports by 3-4 groups(with own [linter](linter/README.md)): std, general, company(which is optional) and project dependencies.
Also, formatting for your code will be prepared(so, you don't need to use `gofmt` or `goimports` separately).
Use additional options `-rm-unused` to remove unused imports and `-set-alias` to rewrite import aliases for versioned packages or for packages with additional prefix/suffix(example: `opentracing "github.com/opentracing/opentracing-go"`).
`-local` - will create group for local imports. Values should be comma-separated.
`-company-prefixes` - will create group for company imports(libs inside your organization). Values should be comma-separated.


## Configuration:
### Cmd
```bash
goimports-reviser -file-path ./reviser/reviser.go -rm-unused -set-alias -format
goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go
```

### Example, to configure it with JetBrains IDEs (via file watcher plugin):
Expand All @@ -43,14 +43,19 @@ goimports-reviser -file-path ./reviser/reviser.go -rm-unused -set-alias -format
### Options:
```text
Usage of goimports-reviser:
-file-path string
File path to fix imports(ex.: ./reviser/reviser.go). Required parameter.
-format
Option will perform additional formatting. Optional parameter.
-imports-order string
Your imports groups can be sorted in your way.
std - std import group;
general - libs for general purpose;
company - inter-org or your company libs(if you set '-company-prefixes'-option, then 4th group will be split separately. In other case, it will be the part of general purpose libs);
project - your local project dependencies.
Optional parameter. (default "std,general,company,project")
-list-diff
Option will list-diff files whose formatting differs from goimports-reviser. Optional parameter.
-local string
Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.
-company-prefixes string
Company package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.
-output string
Can be "file", "write" or "stdout". Whether to write the formatted content back to the file or to stdout. When "write" together with "-list-diff" will list the file name and write back to the file. Optional parameter. (default "file")
-project-name string
Expand All @@ -61,7 +66,6 @@ Usage of goimports-reviser:
Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. In this case import will be set as 'pg "github.com/go-pg/pg/v9"'. Optional parameter.
-set-exit-status
set the exit status to 1 if a change is needed/made. Optional parameter.
```

## Install
Expand Down Expand Up @@ -115,7 +119,7 @@ import (
)
```

### Example with `-local`-option
### Example with `-company-prefixes`-option

Before usage:

Expand All @@ -125,7 +129,7 @@ package testdata // goimports-reviser/testdata
import (
"fmt" //fmt package
"github.com/pkg/errors" //custom package
"github.com/incu6us/goimports-reviser/pkg" // this is a local package which is not a part of the project
"github.com/incu6us/goimports-reviser/pkg" // this is a company package which is not a part of the project, but is a part of your organization
"goimports-reviser/pkg"
)
```
Expand All @@ -139,7 +143,7 @@ import (

"github.com/pkg/errors" // custom package

"github.com/incu6us/goimports-reviser/pkg" // this is a local package which is not a part of the project
"github.com/incu6us/goimports-reviser/pkg" // this is a company package which is not a part of the project, but is a part of your organization

"goimports-reviser/pkg"
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/incu6us/goimports-reviser/v2
module github.com/incu6us/goimports-reviser/v3

go 1.18

Expand Down
4 changes: 2 additions & 2 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package helper
import (
"os"

"github.com/incu6us/goimports-reviser/v2/pkg/module"
"github.com/incu6us/goimports-reviser/v2/reviser"
"github.com/incu6us/goimports-reviser/v3/pkg/module"
"github.com/incu6us/goimports-reviser/v3/reviser"
)

func DetermineProjectName(projectName, filePath string) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"testing"

"github.com/incu6us/goimports-reviser/v2/reviser"
"github.com/incu6us/goimports-reviser/v3/reviser"
)

func TestDetermineProjectName(t *testing.T) {
Expand All @@ -31,7 +31,7 @@ func TestDetermineProjectName(t *testing.T) {
return dir
}(),
},
want: "github.com/incu6us/goimports-reviser/v2",
want: "github.com/incu6us/goimports-reviser/v3",
wantErr: false,
},
{
Expand All @@ -42,7 +42,7 @@ func TestDetermineProjectName(t *testing.T) {
return reviser.StandardInput
}(),
},
want: "github.com/incu6us/goimports-reviser/v2",
want: "github.com/incu6us/goimports-reviser/v3",
wantErr: false,
},
}
Expand Down
Binary file modified images/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions linter/analizer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linter
// +build linter

package main
Expand All @@ -8,10 +9,9 @@ import (
"go/parser"
"go/token"

"golang.org/x/tools/go/analysis"

"github.com/incu6us/goimports-reviser/v2/pkg/module"
"github.com/incu6us/goimports-reviser/v2/reviser"
"golang.org/x/tools/go/analysis"
)

const errMessage = "imports must be formatted"
Expand Down
13 changes: 13 additions & 0 deletions linter/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/incu6us/goimports-reviser/linter

go 1.18

require (
github.com/incu6us/goimports-reviser/v2 v2.5.3
golang.org/x/tools v0.1.12
)

require (
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
)
12 changes: 12 additions & 0 deletions linter/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/incu6us/goimports-reviser/v2 v2.5.3 h1:DzvFl1+qOIDukqN8vMM/10MQswFQywUdwXxsjuowxlc=
github.com/incu6us/goimports-reviser/v2 v2.5.3/go.mod h1:P18aXhQaED7izHIP9IPI9PqEs7Y7D9okq71Q8Y8yHN4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1 change: 1 addition & 0 deletions linter/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linter
// +build linter

package main
Expand Down
Loading

0 comments on commit 979c6e7

Please sign in to comment.