-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathlocal_module.go
59 lines (46 loc) · 1.13 KB
/
local_module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package section
import (
"fmt"
"os"
"strings"
"golang.org/x/mod/modfile"
"github.com/daixiang0/gci/pkg/parse"
"github.com/daixiang0/gci/pkg/specificity"
)
const LocalModuleType = "localmodule"
type LocalModule struct {
Path string
}
func (m *LocalModule) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
if spec.Path == m.Path || strings.HasPrefix(spec.Path, m.Path+"/") {
return specificity.LocalModule{}
}
return specificity.MisMatch{}
}
func (m *LocalModule) String() string {
return LocalModuleType
}
func (m *LocalModule) Type() string {
return LocalModuleType
}
// Configure configures the module section by finding the module
// for the current path
func (m *LocalModule) Configure(path string) error {
if path != "" {
m.Path = path
} else {
path, err := findLocalModule()
if err != nil {
return fmt.Errorf("finding local modules for `localModule` configuration: %w", err)
}
m.Path = path
}
return nil
}
func findLocalModule() (string, error) {
b, err := os.ReadFile("go.mod")
if err != nil {
return "", fmt.Errorf("reading go.mod: %w", err)
}
return modfile.ModulePath(b), nil
}