-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement customizable formatting #43
Conversation
30fa9a9
to
cab7277
Compare
@@ -11,19 +11,81 @@ $ go get github.com/daixiang0/gci | |||
``` | |||
|
|||
## Usage | |||
GCI supports three modes of operation | |||
```shell | |||
$ gci print -h |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we merge print
and write
together as write
seems more like an option for print
, how about gci format
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for choosing write
and print
is that I think they make it immediately clear to the user what they do. With format
I would not be sure if it reformats the file or just prints a formatted version.
However I could easily add an alias like there is with overwrite
for write
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact print
may mislead as well, let's make it clear and easy for users:
gci <files>
to print formatted versiongci -w <files>
to write formatted version
Keep same logic with goimports
makes users easily migrate, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "old" style were you can simply do gci <files>
and gci -w <files>
is still fully present and functional to provide backwards compatibility. Thus if some users prefer that format they can use it.
However I did not want to modify this style with breaking changes by introducing the options for sections there.
I also did not add the help output for this to the Readme in order not to confuse new users who can start with the new subcommands. But if you just run gci -h
you still see all the old options:
Usage:
gci [-diff | -write] [-local localPackageURLs] path... [flags]
gci [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
diff Prints a git style diff to STDOUT
help Help about any command
print Outputs the formatted file to STDOUT
write Formats the specified files in-place
Flags:
-d, --diff display diffs instead of rewriting files
-h, --help help for gci
-l, --local strings put imports beginning with this string after 3rd-party p
ackages, separate imports by comma
-v, --version version for gci
-w, --write write result to (source) file instead of stdout
I think this is the best compromise for adding this new functionality. This way the cli that previous users are accustomed to is still present. If you want to use the new functionality you can just use the new commands.
Another benefit of the new subcommands is that they have autocomplete support. Just writing gci w<TAB>
which gets autocompleted is even faster than writing gci -w
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, let's make it land.
cab7277
to
5a32415
Compare
Also please sign all your commits, you can refer to this doc. |
Signed-off-by: Norman Gehrsitz <norman.gehrsitz@gmx.de>
Signed-off-by: Norman Gehrsitz <norman.gehrsitz@gmx.de>
…en built with Cobra in a backwards compatible manner. Signed-off-by: Norman Gehrsitz <norman.gehrsitz@gmx.de>
Signed-off-by: Norman Gehrsitz <norman.gehrsitz@gmx.de>
Signed-off-by: Norman Gehrsitz <norman.gehrsitz@gmx.de>
5a32415
to
c3b5b05
Compare
@ngehrsitz thanks for your great contribution! |
This PR adds the functionality to customize how imports are formatted by introducing the concept of sections.
Sections are specified at run time and describe the desired output format of the imports. Every import is parsed and assigned to the section that it matches best with. Some sections are used purely for formatting purposes like
NewLine
and never match any imports. Some sections may also have a prefix or suffix that is only rendered if there were imports matched to the section.The CLI is now built upon cobra and introduces the
diff
,print
andwrite
subcommands with support for the new functionality.Backwards compatibility to the old CLI is kept with the only exception being that
-local
needs to be specified as--local
now.The corresponding changes to golangci-lint are also ready and I will open a PR there once this has been merged.
Test cases based on #39 were also added.