Skip to content

Commit

Permalink
feat: add stock mode (#37)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
haunt98 authored Jul 10, 2023
1 parent bcf0baf commit b56aba1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type action struct {
diff bool
verbose bool
profiler bool
stock bool
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -77,6 +80,10 @@ func NewApp() *App {
Name: flagProfilerName,
Usage: flagProfilerUsage,
},
&cli.BoolFlag{
Name: flagStockName,
Usage: flagStockUsage,
},
},
Action: a.Run,
}
Expand Down
41 changes: 24 additions & 17 deletions internal/imports/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Formatter struct {
isWrite bool
isDiff bool
isVerbose bool
isStock bool
}

func NewFormmater(opts ...FormatterOptionFn) (*Formatter, error) {
Expand Down Expand Up @@ -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 len(ft.companyPrefixes) != 0 {
result[companyImport] = 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)
}
result[localImport] = make([]*dst.ImportSpec, 0, 8)

for _, importSpec := range importSpecs {
// "github.com/abc/xyz" -> github.com/abc/xyz
Expand All @@ -338,23 +343,25 @@ func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleNa
continue
}

if strings.HasPrefix(importPath, moduleName) {
result[localImport] = append(result[localImport], importSpec)
continue
}
if !ft.isStock {
if strings.HasPrefix(importPath, moduleName) {
result[localImport] = append(result[localImport], importSpec)
continue
}

if len(ft.companyPrefixes) != 0 {
existImport := false
for companyPrefix := range ft.companyPrefixes {
if strings.HasPrefix(importPath, companyPrefix) {
result[companyImport] = append(result[companyImport], importSpec)
existImport = true
break
if len(ft.companyPrefixes) != 0 {
existImport := false
for companyPrefix := range ft.companyPrefixes {
if strings.HasPrefix(importPath, companyPrefix) {
result[companyImport] = append(result[companyImport], importSpec)
existImport = true
break
}
}
}

if existImport {
continue
if existImport {
continue
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions internal/imports/formatter_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ func FormatterWithCompanyPrefix(companyPrefix string) FormatterOptionFn {
}
}
}

func FormatterWithStock(isStock bool) FormatterOptionFn {
return func(ft *Formatter) {
ft.isStock = isStock
}
}

0 comments on commit b56aba1

Please sign in to comment.