github.com/daixiang0/gci@v0.13.0/README.md (about)

     1  # GCI
     2  
     3  GCI, a tool that controls Go package import order and makes it always deterministic.
     4  
     5  The desired output format is highly configurable and allows for more custom formatting than `goimport` does.
     6  
     7  GCI considers a import block based on AST as below:
     8  
     9  ```
    10  Doc
    11  Name Path Comment
    12  ```
    13  
    14  All comments will keep as they were, except the isolated comment blocks.
    15  
    16  The isolated comment blocks like below:
    17  
    18  ```
    19  import (
    20    "fmt"
    21    // this line is isolated comment
    22  
    23    // those lines belong to one
    24    // isolated comment blocks
    25  
    26    "github.com/daixiang0/gci"
    27  )
    28  ```
    29  
    30  GCI splits all import blocks into different sections, now support six section type:
    31  
    32  - standard: Go official imports, like "fmt"
    33  - custom: Custom section, use full and the longest match (match full string first, if multiple matches, use the longest one)
    34  - default: All rest import blocks
    35  - blank: Put blank imports together in a separate group
    36  - dot: Put dot imports together in a separate group
    37  - alias: Put alias imports together in a separate group
    38  - localmodule: Put imports from local packages in a separate group
    39  
    40  The priority is standard > default > custom > blank > dot > alias > localmodule, all sections sort alphabetically inside.
    41  By default, blank , dot and alias sections are not used and the corresponding lines end up in the other groups.
    42  
    43  All import blocks use one TAB(`\t`) as Indent.
    44  
    45  Since v0.9.0, GCI always puts C import block as the first.
    46  
    47  **Note**:
    48  
    49  `nolint` is hard to handle at section level, GCI will consider it as a single comment.
    50  
    51  ### LocalModule
    52  
    53  Local module detection is done via listing packages from *the directory where
    54  `gci` is invoked* and reading the modules off these. This means:
    55  
    56    - This mode works when `gci` is invoked from a module root (i.e. directory
    57      containing `go.mod`)
    58    - This mode doesn't work with a multi-module setup, i.e. when `gci` is invoked
    59      from a directory containing `go.work` (though it would work if invoked from
    60      within one of the modules in the workspace)
    61  
    62  ## Installation
    63  
    64  To download and install the highest available release version -
    65  
    66  ```shell
    67  go install github.com/daixiang0/gci@latest
    68  ```
    69  
    70  You may also specify a specific version, for example:
    71  
    72  ```shell
    73  go install github.com/daixiang0/gci@v0.11.2
    74  ```
    75  
    76  ## Usage
    77  
    78  Now GCI provides two command line methods, mainly for backward compatibility.
    79  
    80  ### New style
    81  
    82  GCI supports three modes of operation
    83  
    84  > **Note**
    85  >
    86  > Since v0.10.0, the `-s` and `--section` flag can only be used multiple times to specify multiple sections.
    87  > For example, you could use `-s standard,default` before, but now you must use `-s standard -s default`.
    88  > This breaking change makes it possible for the project to support specifying multiple custom prefixes. (Please see below.)
    89  
    90  ```shell
    91  $ gci print -h
    92  Print outputs the formatted file. If you want to apply the changes to a file use write instead!
    93  
    94  Usage:
    95    gci print path... [flags]
    96  
    97  Aliases:
    98    print, output
    99  
   100  Flags:
   101        --custom-order          Enable custom order of sections
   102    -d, --debug                 Enables debug output from the formatter
   103    -h, --help                  help for print
   104    -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
   105                                standard - standard section that Go provides officially, like "fmt"
   106                                Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
   107                                default - default section, contains all rest imports
   108                                blank - blank section, contains all blank imports.
   109                                dot - dot section, contains all dot imports. (default [standard,default])
   110                                alias - alias section, contains all alias imports.
   111        --skip-generated        Skip generated files
   112        --skip-vendor           Skip files inside vendor directory
   113  ```
   114  
   115  ```shell
   116  $ gci write -h
   117  Write modifies the specified files in-place
   118  
   119  Usage:
   120    gci write path... [flags]
   121  
   122  Aliases:
   123    write, overwrite
   124  
   125  Flags:
   126        --custom-order          Enable custom order of sections
   127    -d, --debug                 Enables debug output from the formatter
   128    -h, --help                  help for write
   129    -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
   130                                standard - standard section that Go provides officially, like "fmt"
   131                                Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
   132                                default - default section, contains all rest imports
   133                                blank - blank section, contains all blank imports.
   134                                dot - dot section, contains all dot imports. (default [standard,default])
   135                                alias - alias section, contains all alias imports.
   136        --skip-generated        Skip generated files
   137        --skip-vendor           Skip files inside vendor directory
   138  ```
   139  
   140  ```shell
   141  $ gci list -h
   142  Prints the filenames that need to be formatted. If you want to show the diff use diff instead, and if you want to apply the changes use write instead
   143  
   144  Usage:
   145    gci list path... [flags]
   146  
   147  Flags:
   148        --custom-order          Enable custom order of sections
   149    -d, --debug                 Enables debug output from the formatter
   150    -h, --help                  help for list
   151    -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
   152                                standard - standard section that Go provides officially, like "fmt"
   153                                Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
   154                                default - default section, contains all rest imports
   155                                blank - blank section, contains all blank imports.
   156                                alias - alias section, contains all alias imports.
   157                                dot - dot section, contains all dot imports. (default [standard,default])
   158        --skip-generated        Skip generated files
   159        --skip-vendor           Skip files inside vendor directory
   160  ```
   161  
   162  ```shell
   163  $ gci diff -h
   164  Diff prints a patch in the style of the diff tool that contains the required changes to the file to make it adhere to the specified formatting.
   165  
   166  Usage:
   167    gci diff path... [flags]
   168  
   169  Flags:
   170        --custom-order          Enable custom order of sections
   171    -d, --debug                 Enables debug output from the formatter
   172    -h, --help                  help for diff
   173    -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
   174                                standard - standard section that Go provides officially, like "fmt"
   175                                Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
   176                                default - default section, contains all rest imports
   177                                blank - blank section, contains all blank imports.
   178                                alias - alias section, contains all alias imports.
   179                                dot - dot section, contains all dot imports. (default [standard,default])
   180        --skip-generated        Skip generated files
   181        --skip-vendor           Skip files inside vendor directory
   182  ```
   183  
   184  ### Old style
   185  
   186  ```shell
   187  Gci enables automatic formatting of imports in a deterministic manner
   188  If you want to integrate this as part of your CI take a look at golangci-lint.
   189  
   190  Usage:
   191    gci [-diff | -write] [--local localPackageURLs] path... [flags]
   192    gci [command]
   193  
   194  Available Commands:
   195    completion  Generate the autocompletion script for the specified shell
   196    diff        Prints a git style diff to STDOUT
   197    help        Help about any command
   198    list        Prints filenames that need to be formatted to STDOUT
   199    print       Outputs the formatted file to STDOUT
   200    write       Formats the specified files in-place
   201  
   202  Flags:
   203    -d, --diff            display diffs instead of rewriting files
   204    -h, --help            help for gci
   205    -l, --local strings   put imports beginning with this string after 3rd-party packages, separate imports by comma
   206    -v, --version         version for gci
   207    -w, --write           write result to (source) file instead of stdout
   208  
   209  Use "gci [command] --help" for more information about a command.
   210  ```
   211  
   212  **Note**::
   213  
   214  The old style is only for local tests, will be deprecated, please uses new style, `golangci-lint` uses new style as well.
   215  
   216  ## Examples
   217  
   218  Run `gci write -s standard -s default -s "prefix(github.com/daixiang0/gci)" main.go` and you will handle following cases:
   219  
   220  ### simple case
   221  
   222  ```go
   223  package main
   224  import (
   225    "golang.org/x/tools"
   226  
   227    "fmt"
   228  
   229    "github.com/daixiang0/gci"
   230  )
   231  ```
   232  
   233  to
   234  
   235  ```go
   236  package main
   237  import (
   238      "fmt"
   239  
   240      "golang.org/x/tools"
   241  
   242      "github.com/daixiang0/gci"
   243  )
   244  ```
   245  
   246  ### with alias
   247  
   248  ```go
   249  package main
   250  import (
   251    "fmt"
   252    go "github.com/golang"
   253    "github.com/daixiang0/gci"
   254  )
   255  ```
   256  
   257  to
   258  
   259  ```go
   260  package main
   261  import (
   262    "fmt"
   263  
   264    go "github.com/golang"
   265  
   266    "github.com/daixiang0/gci"
   267  )
   268  ```
   269  
   270  ### with blank and dot grouping enabled
   271  
   272  ```go
   273  package main
   274  import (
   275    "fmt"
   276    go "github.com/golang"
   277    _ "github.com/golang/blank"
   278    . "github.com/golang/dot"
   279    "github.com/daixiang0/gci"
   280    _ "github.com/daixiang0/gci/blank"
   281    . "github.com/daixiang0/gci/dot"
   282  )
   283  ```
   284  
   285  to
   286  
   287  ```go
   288  package main
   289  import (
   290    "fmt"
   291  
   292    go "github.com/golang"
   293  
   294    "github.com/daixiang0/gci"
   295  
   296    _ "github.com/daixiang0/gci/blank"
   297    _ "github.com/golang/blank"
   298  
   299    . "github.com/daixiang0/gci/dot"
   300    . "github.com/golang/dot"
   301  )
   302  ```
   303  
   304  ### with alias grouping enabled
   305  
   306  ```go
   307  package main
   308  
   309  import (
   310  	testing "github.com/daixiang0/test"
   311  	"fmt"
   312  
   313  	g "github.com/golang"
   314  
   315  	"github.com/daixiang0/gci"
   316  	"github.com/daixiang0/gci/subtest"
   317  )
   318  ```
   319  
   320  to
   321  
   322  ```go
   323  package main
   324  
   325  import (
   326  	"fmt"
   327  
   328  	"github.com/daixiang0/gci"
   329  	"github.com/daixiang0/gci/subtest"
   330  
   331  	testing "github.com/daixiang0/test"
   332  	g "github.com/golang"
   333  )
   334  ```
   335  
   336  ### with localmodule grouping enabled
   337  
   338  Assuming this is run on the root of this repo (i.e. where
   339  `github.com/daixiang0/gci` is a local module)
   340  
   341  ```go
   342  package main
   343  
   344  import (
   345  	"os"
   346  	"github.com/daixiang0/gci/cmd/gci"
   347  )
   348  ```
   349  
   350  to
   351  
   352  ```go
   353  package main
   354  
   355  import (
   356  	"os"
   357  
   358  	"github.com/daixiang0/gci/cmd/gci"
   359  )
   360  ```
   361  
   362  ## TODO
   363  
   364  - Ensure only one blank between `Name` and `Path` in an import block
   365  - Ensure only one blank between `Path` and `Comment` in an import block
   366  - Format comments
   367  - Add more testcases
   368  - Support imports completion (please use `goimports` first then use GCI)
   369  - Optimize comments