• Stars
    star
    459
  • Rank 95,377 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Right imports sorting & code formatting tool (goimports alternative)


goimports-reviser Tweet

#StandWithUkraine 'Status Badge' 'Release Badge' 'Quality Badge' codecov GitHub All Releases GitHub release (latest by date) goimports-reviser license Mentioned in Awesome Go

Buy Me A Coffee

'logo'

Tool for Golang to sort goimports by 3-4 groups(with own linter): 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"). -company-prefixes - will create group for company imports(libs inside your organization). Values should be comma-separated.

Configuration:

Cmd

goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go

You can also apply rules to a dir or recursively apply using ./... as a target:

goimports-reviser -rm-unused -set-alias -format -recursive reviser
goimports-reviser -rm-unused -set-alias -format ./...

Example, to configure it with JetBrains IDEs (via file watcher plugin):

example

Options:

Usage of goimports-reviser:
  -apply-to-generated-files
    	Apply imports sorting and formatting(if the option is set) to generated files. Generated file is a file with first comment which starts with comment '// Code generated'. Optional parameter.
  -company-prefixes string
    	Company package prefixes which will be placed after 3rd-party group by default(if defined). Values should be comma-separated. Optional parameters.
  -excludes string
    	Exclude files or dirs, example: '.git/,proto/*.go'.
  -file-path string
    	Deprecated. Put file name as an argument(last item) of command line.
  -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;
    	blanked - imports with "_" alias;
    	dotted - imports with "." alias.
    	Optional parameter. (default "std,general,company,project")
  -list-diff
    	Option will list files whose formatting differs from goimports-reviser. Optional parameter.
  -local string
    	Deprecated
  -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
    	Your project name(ex.: github.com/incu6us/goimports-reviser). Optional parameter.
  -recursive
    	Apply rules recursively if target is a directory. In case of ./... execution will be recursively applied by default. Optional parameter.
  -rm-unused
    	Remove unused imports. Optional parameter.
  -set-alias
    	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.
  -use-cache
    	Use cache to improve performance. Optional parameter.
  -version
    	Show version.

Install

With Go

go install -v github.com/incu6us/goimports-reviser/v3@latest

With Brew

brew tap incu6us/homebrew-tap
brew install incu6us/homebrew-tap/goimports-reviser

With Snap

snap install goimports-reviser

Examples

Before usage:

package testdata

import (
	"log"

	"github.com/incu6us/goimports-reviser/testdata/innderpkg"

	"bytes"

	"golang.org/x/exp/slices"
)

After usage:

package testdata

import (
	"bytes"
	"log"

	"golang.org/x/exp/slices"

	"github.com/incu6us/goimports-reviser/testdata/innderpkg"
)

Comments(not Docs) for imports is acceptable. Example:

package testdata

import (
    "fmt" // comments to the package here
)

Example with -company-prefixes-option

Before usage:

package testdata // goimports-reviser/testdata

import (
	"fmt" //fmt package
	"golang.org/x/exp/slices" //custom package
	"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"
)

After usage:

package testdata // goimports-reviser/testdata

import (
	"fmt" // fmt package

	"golang.org/x/exp/slices" // custom package

	"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"
)

Example with -imports-order std,general,company,project,blanked,dotted-option

Before usage:

package testdata // goimports-reviser/testdata

import (
	_ "github.com/pkg1"
	. "github.com/pkg2"
	"fmt" //fmt package
	"golang.org/x/exp/slices" //custom package
	"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"
)

After usage:

package testdata // goimports-reviser/testdata

import (
	"fmt" // fmt package

	"golang.org/x/exp/slices" // custom package

	"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"

	_ "github.com/pkg1"

	. "github.com/pkg2"
)

Example with -format-option

Before usage:

package main
func test(){
}
func additionalTest(){
}

After usage:

package main

func test(){
}

func additionalTest(){
}

Give a Star! ⭐

If you like or are using this project, please give it a star.

Stargazers

Stargazers over time

More Repositories

1

proxy-chrome-extension

Chrome plugin to setting up a proxy settings via plugin's UI or by browser's address bar
JavaScript
20
star
2

asterisk-ami-api

Call, SMS, CDR APIs for Asterisk
Go
14
star
3

gostruct2graphql

Go structure to GraphQL object
Go
12
star
4

redis-mock-template

Redis Mock (Stateful) for Java based apps & tests
Java
6
star
5

meteor

Lite CI/CD tool with Slack notifications
Go
5
star
6

asterisk-dialer

JavaScript
5
star
7

liqueur-sensor

Firmware for water leakage sensor with low battery control based on Attiny13
C++
3
star
8

idrac

Shell
2
star
9

kafka-client

2
star
10

docker

Nginx
2
star
11

gdb

Data backup tool for MySQL databases, local files and directories to S3 or DigitalOcean Space
Go
2
star
12

pincode

Java
1
star
13

ansible-paybooks

1
star
14

BigData

Java
1
star
15

elk

ELK + logspout
Go
1
star
16

sensu-server

1
star
17

grpc-java-test

Java
1
star
18

joomla-extensions

PHP
1
star
19

cm_api_client

Java
1
star
20

flux-test

JavaScript
1
star
21

goutils

Go
1
star
22

vote-bot

telegram bot for voting based on AWS DynamoDB
Go
1
star
23

thrift-logger

Thrift log decorator library
Java
1
star
24

AMBARI

JavaScript
1
star
25

vue-dart-template

HTML
1
star
26

xml-creator

Go
1
star
27

logger

Go
1
star
28

fs-automation

Go
1
star
29

http-debug-server

Go
1
star
30

RabbitMQProducer

Go
1
star
31

nginx_modules

C
1
star
32

VIDEO_PROJECTS

Java
1
star
33

jwt-example

Go
1
star
34

homebrew-tap

Homebrew repository for different tools
Ruby
1
star
35

go-with-sonar

Dockerfile
1
star
36

cfengine-client

Java
1
star
37

ckafka

Go
1
star
38

graphQlGoTest

Go
1
star
39

openvox-sms-ui

JavaScript
1
star
40

spring-redis-tpl-example

Example of redis-mock-template usage for Medium article
Java
1
star
41

check-openvox-gsm

Tool to check GSM signal on OpenVox
Go
1
star
42

POLYCOM

Java
1
star
43

json-check

Python
1
star