feat: add stock mode (#37)

* feat: add stock flag

* feat: add formatter option stock

* feat: actually split stock import

* fix: missing flag when init cmd

* chore: add how to use stock mode

* chore: add stock mode in flag usage

* chore: remove dup

* chore: consistent docs
main
sudo pacman -Syu 2023-07-10 20:41:08 +07:00 committed by GitHub
parent bcf0bafe7e
commit b56aba18fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 18 deletions

View File

@ -29,6 +29,11 @@ go install github.com/haunt98/gofimports/cmd/gofimports@latest
# - print diff (-d)
# - company prefix, split using comma (,)
gofimports -l -w -d --company github.com/make-go-great,github.com/haunt98 ./internal
# Format ./internal with:
# - write
# - stock mode, only split standard and non standard
gofimports -w --stock ./internal
```
Example result:

View File

@ -19,6 +19,7 @@ type action struct {
diff bool
verbose bool
profiler bool
stock bool
}
}
@ -33,6 +34,7 @@ func (a *action) getFlags(c *cli.Context) {
a.flags.diff = c.Bool(flagDiffName)
a.flags.verbose = c.Bool(flagVerboseName)
a.flags.profiler = c.Bool(flagProfilerName)
a.flags.stock = c.Bool(flagStockName)
if a.flags.verbose {
fmt.Printf("flags: %+v\n", a.flags)
@ -73,6 +75,7 @@ func (a *action) Run(c *cli.Context) error {
imports.FormatterWithDiff(a.flags.diff),
imports.FormatterWithVerbose(a.flags.verbose),
imports.FormatterWithCompanyPrefix(a.flags.companyPrefix),
imports.FormatterWithStock(a.flags.stock),
)
if err != nil {
return fmt.Errorf("imports: failed to new formatter: %w", err)

View File

@ -30,6 +30,9 @@ const (
flagProfilerName = "profiler"
flagProfilerUsage = "go profiler, for debug only"
flagStockName = "stock"
flagStockUsage = "stock mode, only split standard pkg and non standard, ignore company flag"
)
var (
@ -77,6 +80,10 @@ func NewApp() *App {
Name: flagProfilerName,
Usage: flagProfilerUsage,
},
&cli.BoolFlag{
Name: flagStockName,
Usage: flagStockUsage,
},
},
Action: a.Run,
}

View File

@ -62,6 +62,7 @@ type Formatter struct {
isWrite bool
isDiff bool
isVerbose bool
isStock bool
}
func NewFormmater(opts ...FormatterOptionFn) (*Formatter, error) {
@ -324,10 +325,14 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
result := make(map[string][]*dst.ImportSpec)
result[stdImport] = make([]*dst.ImportSpec, 0, 8)
result[thirdPartyImport] = make([]*dst.ImportSpec, 0, 8)
if !ft.isStock {
// Only split company, local imports if not stock
// Otherwise everything is third party imports
if len(ft.companyPrefixes) != 0 {
result[companyImport] = make([]*dst.ImportSpec, 0, 8)
}
result[localImport] = make([]*dst.ImportSpec, 0, 8)
}
for _, importSpec := range importSpecs {
// "github.com/abc/xyz" -> github.com/abc/xyz
@ -338,6 +343,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
continue
}
if !ft.isStock {
if strings.HasPrefix(importPath, moduleName) {
result[localImport] = append(result[localImport], importSpec)
continue
@ -357,6 +363,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
continue
}
}
}
result[thirdPartyImport] = append(result[thirdPartyImport], importSpec)
}

View File

@ -40,3 +40,9 @@ func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn {
}
}
}
func FormatterWithStock(isStock bool) FormatterOptionFn {
return func(ft *Formatter) {
ft.isStock = isStock
}
}