You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Update standardPackages for Go 1.17.5
Signed-off-by: Norman Gehrsitz <[email protected]>
* Update Go version
Signed-off-by: Norman Gehrsitz <[email protected]>
* Implement configurable Section based formatting logic. The CLI has been built with Cobra in a backwards compatible manner.
Signed-off-by: Norman Gehrsitz <[email protected]>
* Add tests for file reformatting
Signed-off-by: Norman Gehrsitz <[email protected]>
* Add test for skipping over malformed files
Signed-off-by: Norman Gehrsitz <[email protected]>
GCI, a tool that control golang package import order and make it always deterministic.
3
+
GCI, a tool that controls golang package import order and makes it always deterministic.
4
4
5
-
It handles empty lines more smartly than `goimport` does.
5
+
The desired output format is highly configurable and allows for more custom formatting than `goimport` does.
6
6
7
7
## Download
8
8
@@ -11,19 +11,81 @@ $ go get github.com/daixiang0/gci
11
11
```
12
12
13
13
## Usage
14
+
GCI supports three modes of operation
15
+
```shell
16
+
$ gci print -h
17
+
Print outputs the formatted file. If you want to apply the changes to a file use write instead!
18
+
19
+
Usage:
20
+
gci print path... [flags]
21
+
22
+
Aliases:
23
+
print, output
24
+
25
+
Flags:
26
+
--NoInlineComments Drops inline comments while formatting
27
+
--NoPrefixComments Drops comment lines above an import statement while formatting
28
+
-s, --Section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). A section can contain a Prefix and a Suffix section which is delimited by ":". These sections can be used for formatting and will only be rendered if the main section contains an entry.
29
+
Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
30
+
Def | Default - Contains all imports that could not be matched to another section type
31
+
NL | NewLine - Prints an empty line
32
+
Prefix(gitlab.com/myorg) | pkgPrefix(gitlab.com/myorg) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
33
+
Std | Standard - Captures all standard packages if they do not match another section
34
+
(default [Standard,Default])
35
+
-x, --SectionSeparator strings SectionSeparators are inserted between Sections (default [NewLine])
36
+
-h, --help helpfor print
37
+
```
38
+
39
+
```shell
40
+
$ gci write -h
41
+
Write modifies the specified files in-place
42
+
43
+
Usage:
44
+
gci write path... [flags]
45
+
46
+
Aliases:
47
+
write, overwrite
48
+
49
+
Flags:
50
+
--NoInlineComments Drops inline comments while formatting
51
+
--NoPrefixComments Drops comment lines above an import statement while formatting
52
+
-s, --Section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). A section can contain a Prefix and a Suffix section which is delimited by ":". These sections can be used for formatting and will only be rendered if the main section contains an entry.
53
+
Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
54
+
Def | Default - Contains all imports that could not be matched to another section type
55
+
NL | NewLine - Prints an empty line
56
+
Prefix(gitlab.com/myorg) | pkgPrefix(gitlab.com/myorg) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
57
+
Std | Standard - Captures all standard packages if they do not match another section
58
+
(default [Standard,Default])
59
+
-x, --SectionSeparator strings SectionSeparators are inserted between Sections (default [NewLine])
60
+
-h, --help helpfor write
61
+
```
14
62
15
63
```shell
16
-
$ gci -h
17
-
usage: gci [flags] [path ...]
18
-
-d display diffs instead of rewriting files
19
-
-local string
20
-
put imports beginning with this string after 3rd-party packages, only support one string
21
-
-w write result to (source) file instead of stdout
64
+
$ gci diff -h
65
+
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.
66
+
67
+
Usage:
68
+
gci diff path... [flags]
69
+
70
+
Flags:
71
+
--NoInlineComments Drops inline comments while formatting
72
+
--NoPrefixComments Drops comment lines above an import statement while formatting
73
+
-s, --Section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). A section can contain a Prefix and a Suffix section which is delimited by ":". These sections can be used for formatting and will only be rendered if the main section contains an entry.
74
+
Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
75
+
Def | Default - Contains all imports that could not be matched to another section type
76
+
NL | NewLine - Prints an empty line
77
+
Prefix(gitlab.com/myorg) | pkgPrefix(gitlab.com/myorg) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
78
+
Std | Standard - Captures all standard packages if they do not match another section
79
+
(default [Standard,Default])
80
+
-x, --SectionSeparator strings SectionSeparators are inserted between Sections (default [NewLine])
81
+
-d, --debug Enables debug output from the formatter
82
+
-h, --help helpfor diff
22
83
```
84
+
Support for the old CLI style is still present if you do not specify the subcommands. The only difference is that `--local` requires two dashes now.
23
85
24
86
## Examples
25
87
26
-
Run `gci -w -local github.com/daixiang0/gci main.go` and you will handle following cases.
88
+
Run `gci write --Section Standard --Section Default --Section "Prefix(github.com/daixiang0/gci)" main.go` and you will handle following cases:
27
89
28
90
### simple case
29
91
@@ -58,76 +120,23 @@ package main
58
120
import (
59
121
"fmt"
60
122
go "github.com/golang"
61
-
"github.com/daixiang0"
62
-
)
63
-
```
64
-
65
-
to
66
-
67
-
```go
68
-
package main
69
-
import (
70
-
"fmt"
71
-
72
-
go"github.com/golang"
73
-
74
-
"github.com/daixiang0/gci"
75
-
)
76
-
```
77
-
78
-
### with comment and alias
79
-
80
-
```go
81
-
package main
82
-
import (
83
-
"fmt"
84
-
_ "github.com/golang"// golang
85
-
"github.com/daixiang0"
86
-
)
87
-
```
88
-
89
-
to
90
-
91
-
```go
92
-
package main
93
-
import (
94
-
"fmt"
95
-
96
-
// golang
97
-
_ "github.com/golang"
98
-
99
123
"github.com/daixiang0/gci"
100
124
)
101
125
```
102
126
103
-
### with above comment and alias
104
-
105
-
```go
106
-
package main
107
-
import (
108
-
"fmt"
109
-
// golang
110
-
_ "github.com/golang"
111
-
"github.com/daixiang0"
112
-
)
113
-
```
114
-
115
127
to
116
128
117
129
```go
118
130
package main
119
131
import (
120
132
"fmt"
121
133
122
-
// golang
123
-
_ "github.com/golang"
134
+
go "github.com/golang"
124
135
125
136
"github.com/daixiang0/gci"
126
137
)
127
138
```
128
139
129
140
## TODO
130
141
131
-
- Support multi-3rd-party packages
132
-
- Support multiple lines of comment in import block
sectionSeparatorStrings=cmd.Flags().StringSliceP("SectionSeparator", "x", gci.DefaultSectionSeparators().String(), "SectionSeparators are inserted between Sections")
Short: "Gci controls golang package import order and makes it always deterministic",
25
+
Long: "Gci enables automatic formatting of imports in a deterministic manner"+
26
+
"\n"+
27
+
"If you want to integrate this as part of your CI take a look at golangci-lint.",
28
+
ValidArgsFunction: subCommandOrGoFileCompletion,
29
+
Args: cobra.MinimumNArgs(1),
30
+
Version: version,
31
+
RunE: e.runInCompatibilityMode,
32
+
}
33
+
e.rootCmd=&rootCmd
34
+
e.diffMode=rootCmd.Flags().BoolP("diff", "d", false, "display diffs instead of rewriting files")
35
+
e.writeMode=rootCmd.Flags().BoolP("write", "w", false, "write result to (source) file instead of stdout")
36
+
e.localFlags=rootCmd.Flags().StringSliceP("local", "l", []string{}, "put imports beginning with this string after 3rd-party packages, separate imports by comma")
37
+
e.initDiff()
38
+
e.initPrint()
39
+
e.initWrite()
40
+
return&e
41
+
}
42
+
43
+
// Execute adds all child commands to the root command and sets flags appropriately.
44
+
// This is called by main.main(). It only needs to happen once to the rootCmd.
0 commit comments