Skip to content

Commit

Permalink
Add basic attempt at local module handling
Browse files Browse the repository at this point in the history
The idea here is to support a new formatting section, `module`, which is
the module we're currently running in as a replacement for
`prefix(module/we/are/running/in)`.

This attempt is very basic: if the `module` section is requested it:

* Looks for a `go.mod` in the current directory (and errors if we can't
  find or read it)
* Stores the module path from `go.mod`
* Looks for imports that have that path as a prefix when specifying
  ordering

This attempt intentionally does not attempt to:

* Do any more clever searching for `go.mod` (e.g. traverse up/down
  directory tree until we find one)
* Do any handling of multiple module setups, e.g. any module under the
  current directory is just ignored
  • Loading branch information
matthewhughes934 committed Oct 28, 2023
1 parent 19d84f7 commit 53e1b60
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/gci/gcicommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
if err != nil {
return err
}
// TOOD: "." is a hack (just to see how things work)
if err = gciCfg.LoadModulePaths("."); err != nil {
return err
}
if *debug {
log.SetLevel(zapcore.DebugLevel)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,22 @@ func ParseConfig(in string) (*Config, error) {

return gciCfg, nil
}

// LoadModulePaths checks if a 'Module' section was requested
// then looks for a 'go.mod' in dir and loads the module path defined there
// into the module section
func (g *Config) LoadModulePaths(dir string) error {
var moduleSection *section.Module
for i := range g.Sections {
s := g.Sections[i]
if m, ok := s.(*section.Module); ok {
moduleSection = m
break
}
}
if moduleSection == nil {
// skip collecting Go modules when not needed
return nil
}
return moduleSection.Populate(dir)
}
7 changes: 7 additions & 0 deletions pkg/gci/gci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func TestRun(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// TOOD: another path hack (we need to load gci's 'go.mod')
// which is also hacky since it implicity ties some of our
// testcases to that file. It would probably be nicer to explicitly
// include all required bits in the testcase itself
if err := config.LoadModulePaths("../.."); err != nil {
t.Fatal(err)
}

old, new, err := LoadFormat([]byte(testCases[i].in), "", *config)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/gci/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,28 @@ import (
testing "github.com/daixiang0/test"
g "github.com/golang"
)
`,
},
{
"basic module",
`sections:
- Standard
- Module
`,
`package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
`,
`package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
`,
},
}
2 changes: 2 additions & 0 deletions pkg/section/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func Parse(data []string) (SectionList, error) {
list = append(list, Blank{})
} else if s == "alias" {
list = append(list, Alias{})
} else if s == "module" {
list = append(list, &Module{})
} else {
errString += fmt.Sprintf(" %s", s)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func (list SectionList) String() []string {
}

func DefaultSections() SectionList {
return SectionList{Standard{}, Default{}}
// pointer for Module since we need to mutate it
return SectionList{Standard{}, Default{}, &Module{}}
}

func DefaultSectionSeparators() SectionList {
Expand Down
1 change: 1 addition & 0 deletions pkg/specificity/specificity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
StandardClass = 20
MatchClass = 30
NameClass = 40
ModuleClass = 50
)

// MatchSpecificity is used to determine which section matches an import best
Expand Down

0 comments on commit 53e1b60

Please sign in to comment.