goimports-reviser icon indicating copy to clipboard operation
goimports-reviser copied to clipboard

Option to recursively format files in a directory

Open ryanmcnamara opened this issue 3 years ago • 5 comments

I would like to use this tool to format the imports for every file in my project. To do this, I have to write a script something similar to

for file in $(find . -name '*.go'); do
    goimports-reviser  -file-path $file -project-name $PROJECT_NAME
done

goimports supports this, for example simply running goimports -w . will recursively format imports for every go file in .

I would suggest a similar approach, allow the file-path argument to take a directory and format all files when given as an input, but will defer to code owners for what's best.

Thanks!

ryanmcnamara avatar Apr 22 '21 18:04 ryanmcnamara

This helps a lot if you are switching from go run golang.org/x/tools/cmd/[email protected] ... to go run github.com/incu6us/goimports-reviser/[email protected] as iteratively executing go run is expensive in a loop, and it is clutter to have to special-case this vs other tools that can work on all files.

codefromthecrypt avatar Nov 01 '21 02:11 codefromthecrypt

also not sure if this requires a separate issue, but the main concern is working with multiple files. Ex I evaluate them in make, so don't need goimports-reviser to recurse. However, it should be possible to give a list of paths instead of a single one.

codefromthecrypt avatar Nov 01 '21 02:11 codefromthecrypt

Need format all of the project

chenyanchen avatar Mar 29 '22 03:03 chenyanchen

I am using Makefile to manage project, a demo like below:

.PHONY: format
format: 
	@$(foreach file,$(shell find . -type f -name '*.go'),goimports-reviser  -file-path $(file) -project-name $(PROJECT_NAME);)

ppd0705 avatar Jun 08 '22 04:06 ppd0705

You can use version v3.1.0 to apply sorting to each go file in your project, like:

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

incu6us avatar Aug 03 '22 19:08 incu6us