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 docsmain
parent
bcf0bafe7e
commit
b56aba18fe
|
@ -29,6 +29,11 @@ go install github.com/haunt98/gofimports/cmd/gofimports@latest
|
||||||
# - print diff (-d)
|
# - print diff (-d)
|
||||||
# - company prefix, split using comma (,)
|
# - company prefix, split using comma (,)
|
||||||
gofimports -l -w -d --company github.com/make-go-great,github.com/haunt98 ./internal
|
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:
|
Example result:
|
||||||
|
|
|
@ -19,6 +19,7 @@ type action struct {
|
||||||
diff bool
|
diff bool
|
||||||
verbose bool
|
verbose bool
|
||||||
profiler bool
|
profiler bool
|
||||||
|
stock bool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ func (a *action) getFlags(c *cli.Context) {
|
||||||
a.flags.diff = c.Bool(flagDiffName)
|
a.flags.diff = c.Bool(flagDiffName)
|
||||||
a.flags.verbose = c.Bool(flagVerboseName)
|
a.flags.verbose = c.Bool(flagVerboseName)
|
||||||
a.flags.profiler = c.Bool(flagProfilerName)
|
a.flags.profiler = c.Bool(flagProfilerName)
|
||||||
|
a.flags.stock = c.Bool(flagStockName)
|
||||||
|
|
||||||
if a.flags.verbose {
|
if a.flags.verbose {
|
||||||
fmt.Printf("flags: %+v\n", a.flags)
|
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.FormatterWithDiff(a.flags.diff),
|
||||||
imports.FormatterWithVerbose(a.flags.verbose),
|
imports.FormatterWithVerbose(a.flags.verbose),
|
||||||
imports.FormatterWithCompanyPrefix(a.flags.companyPrefix),
|
imports.FormatterWithCompanyPrefix(a.flags.companyPrefix),
|
||||||
|
imports.FormatterWithStock(a.flags.stock),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("imports: failed to new formatter: %w", err)
|
return fmt.Errorf("imports: failed to new formatter: %w", err)
|
||||||
|
|
|
@ -30,6 +30,9 @@ const (
|
||||||
|
|
||||||
flagProfilerName = "profiler"
|
flagProfilerName = "profiler"
|
||||||
flagProfilerUsage = "go profiler, for debug only"
|
flagProfilerUsage = "go profiler, for debug only"
|
||||||
|
|
||||||
|
flagStockName = "stock"
|
||||||
|
flagStockUsage = "stock mode, only split standard pkg and non standard, ignore company flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -77,6 +80,10 @@ func NewApp() *App {
|
||||||
Name: flagProfilerName,
|
Name: flagProfilerName,
|
||||||
Usage: flagProfilerUsage,
|
Usage: flagProfilerUsage,
|
||||||
},
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: flagStockName,
|
||||||
|
Usage: flagStockUsage,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: a.Run,
|
Action: a.Run,
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,7 @@ type Formatter struct {
|
||||||
isWrite bool
|
isWrite bool
|
||||||
isDiff bool
|
isDiff bool
|
||||||
isVerbose bool
|
isVerbose bool
|
||||||
|
isStock bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFormmater(opts ...FormatterOptionFn) (*Formatter, error) {
|
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 := make(map[string][]*dst.ImportSpec)
|
||||||
result[stdImport] = make([]*dst.ImportSpec, 0, 8)
|
result[stdImport] = make([]*dst.ImportSpec, 0, 8)
|
||||||
result[thirdPartyImport] = 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 {
|
if len(ft.companyPrefixes) != 0 {
|
||||||
result[companyImport] = make([]*dst.ImportSpec, 0, 8)
|
result[companyImport] = make([]*dst.ImportSpec, 0, 8)
|
||||||
}
|
}
|
||||||
result[localImport] = make([]*dst.ImportSpec, 0, 8)
|
result[localImport] = make([]*dst.ImportSpec, 0, 8)
|
||||||
|
}
|
||||||
|
|
||||||
for _, importSpec := range importSpecs {
|
for _, importSpec := range importSpecs {
|
||||||
// "github.com/abc/xyz" -> github.com/abc/xyz
|
// "github.com/abc/xyz" -> github.com/abc/xyz
|
||||||
|
@ -338,6 +343,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !ft.isStock {
|
||||||
if strings.HasPrefix(importPath, moduleName) {
|
if strings.HasPrefix(importPath, moduleName) {
|
||||||
result[localImport] = append(result[localImport], importSpec)
|
result[localImport] = append(result[localImport], importSpec)
|
||||||
continue
|
continue
|
||||||
|
@ -357,6 +363,7 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result[thirdPartyImport] = append(result[thirdPartyImport], importSpec)
|
result[thirdPartyImport] = append(result[thirdPartyImport], importSpec)
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,3 +40,9 @@ func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FormatterWithStock(isStock bool) FormatterOptionFn {
|
||||||
|
return func(ft *Formatter) {
|
||||||
|
ft.isStock = isStock
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue