Skip to content

Commit 7160b4f

Browse files
authored
Add sorting (#13)
1 parent deeda6f commit 7160b4f

File tree

5 files changed

+75
-18
lines changed

5 files changed

+75
-18
lines changed

flake.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
packages = with pkgs; [
3535
go_1_21
3636
go-tools
37+
gopls
38+
delve
39+
gomodifytags
3740
];
3841
};
3942
});

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21
44

55
require (
66
github.com/koki-develop/go-fzf v0.15.0
7-
golang.org/x/mod v0.12.0
7+
golang.org/x/mod v0.14.0
88
)
99

1010
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
3838
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
3939
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
4040
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
41+
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
42+
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
4143
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
4244
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
4345
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

main.go

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@ import (
1515
)
1616

1717
var (
18-
selectAll bool
19-
selectAllNoConfirm bool
20-
dryRun bool
21-
flagVersion bool
18+
fSelectAll bool
19+
fSelectAllNoConfirm bool
20+
fDryRun bool
21+
fVersion bool
22+
fSort bool
2223
)
2324

2425
func init() {
25-
flag.BoolVar(&selectAll, "a", false, "select everything by default")
26-
flag.BoolVar(&selectAllNoConfirm, "A", false, "select and update everything without confirmation")
27-
flag.BoolVar(&dryRun, "d", false, "dry run, just print what will be executed")
28-
flag.BoolVar(&flagVersion, "v", false, "show version information")
26+
flag.BoolVar(&fSelectAll, "a", false, "select everything by default")
27+
flag.BoolVar(&fSelectAllNoConfirm, "A", false, "select and update everything without confirmation")
28+
flag.BoolVar(&fDryRun, "d", false, "dry run, just print what will be executed")
29+
flag.BoolVar(&fVersion, "v", false, "show version information")
30+
flag.BoolVar(&fSort, "s", false, "sort require lines")
2931
}
3032

3133
func main() {
3234
flag.Parse()
3335

34-
if flagVersion {
36+
if fVersion {
3537
if version != "" {
3638
fmt.Printf("version:\t%s\n", version)
3739
}
@@ -73,19 +75,25 @@ func updateGoMod(gomodPath string) error {
7375
gomodPath = filepath.Join(gomodPath, "go.mod")
7476
}
7577

76-
modules, err := parseDirectDeps(gomodPath)
78+
gomod, err := parseGoMod(gomodPath)
7779
if err != nil {
7880
return fmt.Errorf("parse direct deps: %w", err)
7981
}
8082

83+
modules := extractDirectDeps(gomod)
84+
8185
if len(modules) == 0 {
8286
return errors.New("no direct dependencies found")
8387
}
8488

85-
if selectAllNoConfirm {
89+
if fSelectAllNoConfirm {
8690
return runGoGet(gomodPath, modulesToPaths(modules))
8791
}
8892

93+
if fSort {
94+
return sortImports(gomodPath, gomod)
95+
}
96+
8997
selected, err := runUI(modules)
9098
if err != nil {
9199
return fmt.Errorf("ui failed: %w", err)
@@ -98,7 +106,7 @@ func updateGoMod(gomodPath string) error {
98106
return runGoGet(gomodPath, modulesToPaths(selected))
99107
}
100108

101-
func parseDirectDeps(path string) ([]module.Version, error) {
109+
func parseGoMod(path string) (*modfile.File, error) {
102110
content, err := os.ReadFile(path)
103111
if err != nil {
104112
return nil, fmt.Errorf("read file: %w", err)
@@ -109,16 +117,20 @@ func parseDirectDeps(path string) ([]module.Version, error) {
109117
return nil, fmt.Errorf("parse go.mod: %w", err)
110118
}
111119

120+
return ast, nil
121+
}
122+
123+
func extractDirectDeps(gomod *modfile.File) []module.Version {
112124
var modules []module.Version
113-
for _, req := range ast.Require {
125+
for _, req := range gomod.Require {
114126
if req.Indirect {
115127
continue
116128
}
117129

118130
modules = append(modules, req.Mod)
119131
}
120132

121-
return modules, nil
133+
return modules
122134
}
123135

124136
func runUI(modules []module.Version) ([]module.Version, error) {
@@ -128,7 +140,7 @@ func runUI(modules []module.Version) ([]module.Version, error) {
128140
}
129141

130142
var findOpts []fzf.FindOption
131-
if selectAll {
143+
if fSelectAll {
132144
findOpts = append(findOpts, fzf.WithPreselectAll(true))
133145
}
134146

@@ -151,7 +163,7 @@ func runUI(modules []module.Version) ([]module.Version, error) {
151163
func runGoGet(path string, selected []string) error {
152164
fmt.Println("go get", strings.Join(selected, " "))
153165

154-
if dryRun {
166+
if fDryRun {
155167
return nil
156168
}
157169

@@ -176,3 +188,43 @@ func modulesToPaths(modules []module.Version) []string {
176188
}
177189
return paths
178190
}
191+
192+
func sortImports(gomodPath string, gomod *modfile.File) error {
193+
var direct, indirect []modfile.Require
194+
for _, req := range gomod.Require {
195+
if req.Indirect {
196+
indirect = append(indirect, *req)
197+
} else {
198+
direct = append(direct, *req)
199+
}
200+
gomod.DropRequire(req.Mod.Path)
201+
}
202+
203+
for _, dep := range direct {
204+
gomod.AddNewRequire(dep.Mod.Path, dep.Mod.Version, false)
205+
}
206+
for _, dep := range indirect {
207+
gomod.AddNewRequire(dep.Mod.Path, dep.Mod.Version, true)
208+
}
209+
210+
gomod.Cleanup()
211+
212+
gomod.SetRequireSeparateIndirect(gomod.Require)
213+
214+
bytes, err := gomod.Format()
215+
if err != nil {
216+
return fmt.Errorf("format gomod: %w", err)
217+
}
218+
219+
info, err := os.Stat(gomodPath)
220+
if err != nil {
221+
return fmt.Errorf("stat gomod: %w", err)
222+
}
223+
224+
err = os.WriteFile(gomodPath, bytes, info.Mode())
225+
if err != nil {
226+
return fmt.Errorf("write gomod: %w", err)
227+
}
228+
229+
return nil
230+
}

modupdate.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ buildGoModule {
1515
./version.go
1616
];
1717
};
18-
vendorSha256 = "sha256-XU4kLbEAPCL8mNk4omk2OIijKdiiAsJKBfoKkJJfHkI=";
18+
vendorSha256 = "sha256-QWeXMgZUEg0fAyc4DS3QaWqv2VxDznhUP9Fl8GOuIYk=";
1919

2020
ldflags = ["-s" "-w"];
2121

0 commit comments

Comments
 (0)