Skip to content
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

Merged
merged 5 commits into from Jan 29, 2022

Conversation

ngehrsitz
Copy link
Contributor

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 and write 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.

@@ -11,19 +11,81 @@ $ go get github.com/daixiang0/gci
```

## Usage
GCI supports three modes of operation
```shell
$ gci print -h
Copy link
Owner

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?

Copy link
Contributor Author

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.

Copy link
Owner

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 version
  • gci -w <files> to write formatted version

Keep same logic with goimports makes users easily migrate, what do you think?

Copy link
Contributor Author

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.

Copy link
Owner

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.

README.md Outdated Show resolved Hide resolved
@daixiang0
Copy link
Owner

daixiang0 commented Jan 28, 2022

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>
@daixiang0
Copy link
Owner

@ngehrsitz thanks for your great contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants