Skip to content

Commit 2a4450b

Browse files
authored
Support tools like stringer (#277)
2 parents 5dabc3d + bf44130 commit 2a4450b

File tree

12 files changed

+325
-175
lines changed

12 files changed

+325
-175
lines changed

internal/choose/choose.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"context"
55

66
"github.com/anttiharju/vmatch/internal/doctor"
7-
"github.com/anttiharju/vmatch/internal/inject"
87
"github.com/anttiharju/vmatch/internal/language"
98
"github.com/anttiharju/vmatch/internal/linter"
9+
"github.com/anttiharju/vmatch/internal/scripts"
10+
"github.com/anttiharju/vmatch/internal/symlinks"
1011
"github.com/anttiharju/vmatch/pkg/version"
1112
)
1213

@@ -15,19 +16,23 @@ func firstArgIs(arg string, args []string) bool {
1516
}
1617

1718
func Wrapper(ctx context.Context, args []string) int {
18-
exitCode := inject.Scripts()
19+
exitCode := scripts.Inject()
1920
if exitCode != 0 {
2021
return exitCode
2122
}
2223

23-
if firstArgIs("go", args) {
24-
wrappedLanguage := language.Wrap("go")
24+
defer func() {
25+
symlinks.Sync()
26+
}()
27+
28+
if firstArgIs(string(scripts.Golang), args) {
29+
wrappedLanguage := language.Wrap(scripts.Golang)
2530

2631
return wrappedLanguage.Run(ctx, args[1:])
2732
}
2833

29-
if firstArgIs("golangci-lint", args) {
30-
wrappedLinter := linter.Wrap("golangci-lint")
34+
if firstArgIs(string(scripts.GolangCILint), args) {
35+
wrappedLinter := linter.Wrap(scripts.GolangCILint)
3136

3237
return wrappedLinter.Run(ctx, args[1:])
3338
}

internal/find/find.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

internal/inject/inject.go

Lines changed: 0 additions & 93 deletions
This file was deleted.

internal/language/language.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"strings"
1212

1313
"github.com/anttiharju/vmatch/internal/exitcode"
14-
"github.com/anttiharju/vmatch/internal/find"
15-
"github.com/anttiharju/vmatch/internal/install"
16-
"github.com/anttiharju/vmatch/internal/wrapper"
14+
"github.com/anttiharju/vmatch/internal/scripts"
15+
"github.com/anttiharju/vmatch/pkg/install"
16+
"github.com/anttiharju/vmatch/pkg/wrapper"
1717
)
1818

1919
type WrappedLanguage struct {
@@ -45,15 +45,10 @@ func validateVersion(version string) (string, error) {
4545
return version, nil
4646
}
4747

48-
func Wrap(name string) *WrappedLanguage {
49-
baseWrapper := wrapper.BaseWrapper{Name: name}
48+
func Wrap(script scripts.Script) *WrappedLanguage {
49+
baseWrapper := wrapper.BaseWrapper{Name: string(script)}
5050

51-
desiredVersion, err := find.Version("go.mod", languageParser, validateVersion)
52-
if err != nil {
53-
baseWrapper.ExitWithPrintln(exitcode.VersionReadFileIssue, err.Error())
54-
}
55-
56-
err = baseWrapper.GenerateInstallPath(desiredVersion)
51+
err := baseWrapper.GenerateInstallPath("go.mod", languageParser, validateVersion)
5752
if err != nil {
5853
baseWrapper.ExitWithPrintln(exitcode.InstallPathIssue, err.Error())
5954
}

internal/linter/linter.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"strings"
1010

1111
"github.com/anttiharju/vmatch/internal/exitcode"
12-
"github.com/anttiharju/vmatch/internal/find"
13-
"github.com/anttiharju/vmatch/internal/wrapper"
12+
"github.com/anttiharju/vmatch/internal/scripts"
13+
"github.com/anttiharju/vmatch/pkg/wrapper"
1414
)
1515

1616
type WrappedLinter struct {
@@ -33,15 +33,10 @@ func validateVersion(version string) (string, error) {
3333
return version, nil
3434
}
3535

36-
func Wrap(name string) *WrappedLinter {
37-
baseWrapper := wrapper.BaseWrapper{Name: name}
36+
func Wrap(script scripts.Script) *WrappedLinter {
37+
baseWrapper := wrapper.BaseWrapper{Name: string(script)}
3838

39-
desiredVersion, err := find.Version(".golangci-version", linterParser, validateVersion)
40-
if err != nil {
41-
baseWrapper.ExitWithPrintln(exitcode.VersionReadFileIssue, err.Error())
42-
}
43-
44-
err = baseWrapper.GenerateInstallPath(desiredVersion)
39+
err := baseWrapper.GenerateInstallPath(".golangci-version", linterParser, validateVersion)
4540
if err != nil {
4641
baseWrapper.ExitWithPrintln(exitcode.InstallPathIssue, err.Error())
4742
}
File renamed without changes.
File renamed without changes.

internal/scripts/scripts.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package scripts
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
type Script string
12+
13+
const (
14+
Golang Script = "go"
15+
GolangCILint Script = "golangci-lint"
16+
GolangCILintV2 Script = "golangci-lint-v2"
17+
)
18+
19+
func Scripts() []Script {
20+
return []Script{Golang, GolangCILint, GolangCILintV2}
21+
}
22+
23+
func (s Script) File() string {
24+
switch s {
25+
case Golang:
26+
return "go.sh"
27+
case GolangCILint, GolangCILintV2:
28+
return "golangci-lint.sh"
29+
default:
30+
return ""
31+
}
32+
}
33+
34+
func exists(path string) bool {
35+
_, err := os.Stat(path)
36+
37+
return err == nil
38+
}
39+
40+
func Inject() int {
41+
homeDir, err := os.UserHomeDir()
42+
if err != nil {
43+
fmt.Fprintf(os.Stderr, "Error: failed to get home directory: %v\n", err)
44+
45+
return 1
46+
}
47+
48+
binDir := filepath.Join(homeDir, ".vmatch", "bin")
49+
if !exists(binDir) {
50+
if err := os.MkdirAll(binDir, 0o755); err != nil {
51+
fmt.Fprintf(os.Stderr, "Error: failed to create directory %s: %v\n", binDir, err)
52+
53+
return 1
54+
}
55+
}
56+
57+
scripts := Scripts()
58+
for _, script := range scripts {
59+
if err := createScript(binDir, script); err != nil {
60+
fmt.Fprintf(os.Stderr, "Error: failed to create script %s: %v\n", script, err)
61+
62+
return 1
63+
}
64+
}
65+
66+
return 0
67+
}
68+
69+
//go:embed go.sh golangci-lint.sh
70+
var scripts embed.FS
71+
72+
func createScript(binDir string, script Script) error {
73+
name := string(script)
74+
sourcePath := script.File()
75+
destPath := filepath.Join(binDir, string(script))
76+
77+
if exists(destPath) {
78+
return nil
79+
}
80+
81+
content, err := fs.ReadFile(scripts, sourcePath)
82+
if err != nil {
83+
return fmt.Errorf("failed to read embedded script %s: %w", name, err)
84+
}
85+
86+
//nolint:gosec // using 0o755 instead of 0o600 is intentional here
87+
if err := os.WriteFile(destPath, content, 0o755); err != nil {
88+
return fmt.Errorf("failed to write script %s: %w", name, err)
89+
}
90+
91+
return nil
92+
}

0 commit comments

Comments
 (0)