Skip to content

Commit d3bf2ab

Browse files
authored
Merge pull request #473 from yoheimuta/fix-lib-lint/471
Fix library usage
2 parents 9b762e3 + 1972846 commit d3bf2ab

File tree

7 files changed

+112
-38
lines changed

7 files changed

+112
-38
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,24 @@ You can also use protolint from Go code.
273273
See [Go Documentation](https://pkg.go.dev/github.com/yoheimuta/protolint/lib) and [lib/lint_test.go](https://github.com/yoheimuta/protolint/blob/master/lib/lint_test.go) in detail.
274274

275275
```go
276-
args := []string{"-config_path", "path/to/your_protolint.yaml", "."}
277-
var stdout bytes.Buffer
278-
var stderr bytes.Buffer
279-
280-
err := lib.Lint(test.inputArgs, &stdout, &stderr)
276+
import (
277+
"bytes"
278+
279+
"github.com/yoheimuta/protolint/lib"
280+
)
281+
282+
func main() {
283+
args := []string{"-config_path", "path/to/your_protolint.yaml", "."}
284+
var stdout bytes.Buffer
285+
var stderr bytes.Buffer
286+
287+
err := lib.Lint(args, &stdout, &stderr)
288+
if err != nil {
289+
// Handle error
290+
}
291+
292+
// Process output in stdout and stderr
293+
}
281294
```
282295

283296
## Rules

internal/cmd/lint_runner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package cmd
33
import (
44
"io"
55

6+
"github.com/yoheimuta/protolint/internal/libinternal"
67
"github.com/yoheimuta/protolint/internal/osutil"
7-
"github.com/yoheimuta/protolint/lib"
88
)
99

10-
// CmdLintRunner implements the lib.LintRunner interface for cmd package
10+
// CmdLintRunner implements the LintRunner interface for cmd package
1111
type CmdLintRunner struct{}
1212

1313
// NewCmdLintRunner creates a new CmdLintRunner
@@ -20,7 +20,7 @@ func (r *CmdLintRunner) Run(args []string, stdout, stderr io.Writer) osutil.Exit
2020
return Do(args, stdout, stderr)
2121
}
2222

23-
// Initialize registers the cmd lint runner with the lib package
23+
// Initialize registers the cmd lint runner with the internal library
2424
func Initialize() {
25-
lib.SetLintRunner(NewCmdLintRunner())
25+
libinternal.SetLintRunner(NewCmdLintRunner())
2626
}

internal/libinternal/lint.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package libinternal
2+
3+
import (
4+
"errors"
5+
"io"
6+
7+
"github.com/yoheimuta/protolint/internal/osutil"
8+
)
9+
10+
var (
11+
// ErrLintFailure error is returned when there is a linting error
12+
ErrLintFailure = errors.New("lint error")
13+
// ErrInternalFailure error is returned when there is a parsing, internal, or runtime error.
14+
ErrInternalFailure = errors.New("parsing, internal or runtime errors")
15+
)
16+
17+
// LintRunner is an interface for running lint commands
18+
type LintRunner interface {
19+
Run(args []string, stdout, stderr io.Writer) osutil.ExitCode
20+
}
21+
22+
var defaultRunner LintRunner
23+
24+
// SetLintRunner sets the runner used by the Lint function
25+
func SetLintRunner(runner LintRunner) {
26+
defaultRunner = runner
27+
}
28+
29+
// GetLintRunner returns the current lint runner
30+
func GetLintRunner() LintRunner {
31+
return defaultRunner
32+
}
33+
34+
// Lint is used to lint Protocol Buffer files with the protolint tool.
35+
// It takes an array of strings (args) representing command line arguments,
36+
// as well as two io.Writer instances (stdout and stderr) to which the output of the command should be written.
37+
// It returns an error in the case of a linting error (ErrLintFailure)
38+
// or a parsing, internal, or runtime error (ErrInternalFailure).
39+
// Otherwise, it returns nil on success.
40+
func Lint(args []string, stdout, stderr io.Writer) error {
41+
if defaultRunner == nil {
42+
return ErrInternalFailure
43+
}
44+
45+
switch defaultRunner.Run(args, stdout, stderr) {
46+
case osutil.ExitSuccess:
47+
return nil
48+
49+
case osutil.ExitLintFailure:
50+
return ErrLintFailure
51+
52+
default:
53+
return ErrInternalFailure
54+
}
55+
}

lib/lint.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
package lib
22

33
import (
4-
"errors"
54
"io"
65

7-
"github.com/yoheimuta/protolint/internal/osutil"
6+
"github.com/yoheimuta/protolint/internal/cmd"
7+
"github.com/yoheimuta/protolint/internal/libinternal"
88
)
99

1010
var (
1111
// ErrLintFailure error is returned when there is a linting error
12-
ErrLintFailure = errors.New("lint error")
12+
ErrLintFailure = libinternal.ErrLintFailure
1313
// ErrInternalFailure error is returned when there is a parsing, internal, or runtime error.
14-
ErrInternalFailure = errors.New("parsing, internal or runtime errors")
14+
ErrInternalFailure = libinternal.ErrInternalFailure
1515
)
1616

1717
// LintRunner is an interface for running lint commands
18-
type LintRunner interface {
19-
Run(args []string, stdout, stderr io.Writer) osutil.ExitCode
20-
}
21-
22-
var defaultRunner LintRunner
18+
type LintRunner = libinternal.LintRunner
2319

2420
// SetLintRunner sets the runner used by the Lint function
2521
func SetLintRunner(runner LintRunner) {
26-
defaultRunner = runner
22+
libinternal.SetLintRunner(runner)
23+
}
24+
25+
// GetLintRunner returns the current lint runner
26+
func GetLintRunner() LintRunner {
27+
return libinternal.GetLintRunner()
2728
}
2829

2930
// Lint is used to lint Protocol Buffer files with the protolint tool.
@@ -32,19 +33,15 @@ func SetLintRunner(runner LintRunner) {
3233
// It returns an error in the case of a linting error (ErrLintFailure)
3334
// or a parsing, internal, or runtime error (ErrInternalFailure).
3435
// Otherwise, it returns nil on success.
36+
//
37+
// Note: This function automatically initializes the default lint runner if none is set,
38+
// so you don't need to call cmd.Initialize() before using it.
3539
func Lint(args []string, stdout, stderr io.Writer) error {
36-
if defaultRunner == nil {
37-
return ErrInternalFailure
40+
// Auto-initialize if needed
41+
if libinternal.GetLintRunner() == nil {
42+
cmd.Initialize()
3843
}
3944

40-
switch defaultRunner.Run(args, stdout, stderr) {
41-
case osutil.ExitSuccess:
42-
return nil
43-
44-
case osutil.ExitLintFailure:
45-
return ErrLintFailure
46-
47-
default:
48-
return ErrInternalFailure
49-
}
45+
// Use the internal implementation
46+
return libinternal.Lint(args, stdout, stderr)
5047
}

lib/lint_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import (
1111
)
1212

1313
func TestLint(t *testing.T) {
14+
// Set the mock lint runner for testing
15+
originalRunner := lib.GetLintRunner() // Save the original runner to restore later
16+
lib.SetLintRunner(NewMockLintRunner())
17+
defer func() {
18+
// Restore the original runner after the test
19+
lib.SetLintRunner(originalRunner)
20+
}()
21+
1422
tests := []struct {
1523
name string
1624
inputArgs []string

lib/lint_test_runner.go renamed to lib/mock_lint_runner_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package lib
1+
package lib_test
22

33
import (
44
"fmt"
55
"io"
66
"strings"
77

88
"github.com/yoheimuta/protolint/internal/osutil"
9+
"github.com/yoheimuta/protolint/lib"
910
)
1011

1112
// MockLintRunner is a mock implementation of LintRunner for testing
@@ -38,7 +39,7 @@ func (r *MockLintRunner) Run(args []string, stdout, stderr io.Writer) osutil.Exi
3839
return osutil.ExitSuccess
3940
}
4041

41-
func init() {
42-
// Set the default runner to our mock implementation
43-
SetLintRunner(&MockLintRunner{})
42+
// NewMockLintRunner creates a new MockLintRunner
43+
func NewMockLintRunner() lib.LintRunner {
44+
return &MockLintRunner{}
4445
}

mcp/tools.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88

9-
"github.com/yoheimuta/protolint/lib"
9+
"github.com/yoheimuta/protolint/internal/libinternal"
1010
)
1111

1212
// Tool defines the interface for MCP tools
@@ -92,12 +92,12 @@ func (t *LintFilesTool) Execute(args json.RawMessage) (any, error) {
9292
var errorBuffer bytes.Buffer
9393

9494
// Run lint command
95-
err := lib.Lint(cmdArgs, &outputBuffer, &errorBuffer)
95+
err := libinternal.Lint(cmdArgs, &outputBuffer, &errorBuffer)
9696

9797
// Determine exit code based on error
9898
exitCode := 0
9999
if err != nil {
100-
if err == lib.ErrLintFailure {
100+
if err == libinternal.ErrLintFailure {
101101
exitCode = 1
102102
} else {
103103
exitCode = 2

0 commit comments

Comments
 (0)