Skip to content

Commit 834f489

Browse files
committed
fix: barry quick fix, 2025-06-25 14:09:27
1 parent bd61d7c commit 834f489

5 files changed

Lines changed: 116 additions & 70 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package linters
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/samber/lo"
7+
"path/filepath"
68
"strings"
79

810
"github.com/googleapis/api-linter/lint"
@@ -17,6 +19,7 @@ func formatGitHubActionOutput(responses []lint.Response) []byte {
1719
// ::error file={name},line={line},endLine={endLine},title={title}::{message}
1820
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
1921

22+
fmt.Println(lo.Must(filepath.Abs(response.FilePath)))
2023
fmt.Fprintf(&buf, "::error file=%s", response.FilePath)
2124
if problem.Location != nil {
2225
// Some findings are *line level* and only have start positions but no
Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"github.com/samber/lo"
78
"os"
89
"strings"
910
"sync"
@@ -16,25 +17,25 @@ import (
1617
)
1718

1819
type CliArgs struct {
19-
FormatType string
20-
ProtoImportPaths []string
21-
EnabledRules []string
22-
DisabledRules []string
23-
ListRulesFlag bool
24-
DebugFlag bool
25-
IgnoreCommentDisablesFlag bool
20+
//FormatType string
21+
//ProtoImportPaths []string
22+
EnabledRules []string
23+
DisabledRules []string
24+
ListRulesFlag bool
25+
DebugFlag bool
26+
//IgnoreCommentDisablesFlag bool
2627
}
2728

2829
func NewCli() (*CliArgs, typex.Flags) {
2930
var cliArgs CliArgs
3031

3132
return &cliArgs, typex.Flags{
32-
&cli.BoolFlag{
33-
Name: "ignore-comment-disables",
34-
Usage: "If set to true, disable comments will be ignored.\nThis is helpful when strict enforcement of AIPs are necessary and\nproto definitions should not be able to disable checks.",
35-
Value: false,
36-
Destination: &cliArgs.IgnoreCommentDisablesFlag,
37-
},
33+
//&cli.BoolFlag{
34+
// Name: "ignore-comment-disables",
35+
// Usage: "If set to true, disable comments will be ignored.\nThis is helpful when strict enforcement of AIPs are necessary and\nproto definitions should not be able to disable checks.",
36+
// Value: false,
37+
// Destination: &cliArgs.IgnoreCommentDisablesFlag,
38+
//},
3839

3940
&cli.BoolFlag{
4041
Name: "debug",
@@ -50,58 +51,66 @@ func NewCli() (*CliArgs, typex.Flags) {
5051
Destination: &cliArgs.ListRulesFlag,
5152
},
5253

53-
&cli.StringFlag{
54-
Name: "output-format",
55-
Usage: "The format of the linting results.\nSupported formats include \"yaml\", \"json\",\"github\" and \"summary\" table.\nYAML is the default.",
56-
Aliases: []string{"f"},
57-
Value: "",
58-
Destination: &cliArgs.FormatType,
59-
},
60-
61-
&cli.StringSliceFlag{
62-
Name: "proto-path",
63-
Usage: "The folder for searching proto imports.\\nMay be specified multiple times; directories will be searched in order.\\nThe current working directory is always used.",
64-
Aliases: []string{"I"},
65-
Value: nil,
66-
Destination: &cliArgs.ProtoImportPaths,
67-
},
68-
69-
&cli.StringSliceFlag{
70-
Name: "enable-rule",
71-
Usage: "Enable a rule with the given name.\nMay be specified multiple times.",
72-
Value: nil,
73-
Destination: &cliArgs.EnabledRules,
74-
},
75-
76-
&cli.StringSliceFlag{
77-
Name: "disable-rule",
78-
Usage: "Disable a rule with the given name.\nMay be specified multiple times.",
79-
Value: nil,
80-
Destination: &cliArgs.DisabledRules,
81-
},
54+
//&cli.StringFlag{
55+
// Name: "output-format",
56+
// Usage: "The format of the linting results.\nSupported formats include \"yaml\", \"json\",\"github\" and \"summary\" table.\nYAML is the default.",
57+
// Aliases: []string{"f"},
58+
// Value: "",
59+
// Destination: &cliArgs.FormatType,
60+
//},
61+
62+
//&cli.StringSliceFlag{
63+
// Name: "proto-path",
64+
// Usage: "The folder for searching proto imports.\\nMay be specified multiple times; directories will be searched in order.\\nThe current working directory is always used.",
65+
// Aliases: []string{"I"},
66+
// Value: nil,
67+
// Destination: &cliArgs.ProtoImportPaths,
68+
//},
69+
70+
//&cli.StringSliceFlag{
71+
// Name: "enable-rule",
72+
// Usage: "Enable a rule with the given name.\nMay be specified multiple times.",
73+
// Value: nil,
74+
// Destination: &cliArgs.EnabledRules,
75+
//},
76+
//
77+
//&cli.StringSliceFlag{
78+
// Name: "disable-rule",
79+
// Usage: "Disable a rule with the given name.\nMay be specified multiple times.",
80+
// Value: nil,
81+
// Destination: &cliArgs.DisabledRules,
82+
//},
8283
}
8384

8485
}
8586

86-
func Linter(c *CliArgs, configs lint.Configs, protoFiles []string) error {
87+
type LinterConfig struct {
88+
Rules lint.Config `yaml:"rules,omitempty" hash:"-"`
89+
FormatType string `yaml:"format_type"`
90+
IgnoreCommentDisablesFlag bool `yaml:"ignore_comment_disables_flag"`
91+
}
92+
93+
func Linter(c *CliArgs, config LinterConfig, protoImportPaths []string, protoFiles []string) error {
8794
if c.ListRulesFlag {
88-
return outputRules(c.FormatType)
95+
return outputRules(config.FormatType)
8996
}
9097

9198
// Pre-check if there are files to lint.
9299
if len(protoFiles) == 0 {
93100
return fmt.Errorf("no file to lint")
94101
}
95102

103+
rules := lint.Configs{config.Rules}
104+
96105
// Add configs for the enabled rules.
97-
configs = append(configs, lint.Config{EnabledRules: c.EnabledRules})
98-
configs = append(configs, lint.Config{DisabledRules: c.DisabledRules})
106+
rules = append(rules, lint.Config{EnabledRules: c.EnabledRules})
107+
rules = append(rules, lint.Config{DisabledRules: c.DisabledRules})
99108

100109
var errorsWithPos []protoparse.ErrorWithPos
101110
var lock sync.Mutex
102111
// Parse proto files into `protoreflect` file descriptors.
103112
p := protoparse.Parser{
104-
ImportPaths: append(c.ProtoImportPaths, "."),
113+
ImportPaths: append(protoImportPaths, "."),
105114
IncludeSourceCodeInfo: true,
106115
ErrorReporter: func(errorWithPos protoparse.ErrorWithPos) error {
107116
// Protoparse isn't concurrent right now but just to be safe for the future.
@@ -116,8 +125,8 @@ func Linter(c *CliArgs, configs lint.Configs, protoFiles []string) error {
116125
var err error
117126
// Resolve file absolute paths to relative ones.
118127
// Using supplied import paths first.
119-
if len(c.ProtoImportPaths) > 0 {
120-
protoFiles, err = protoparse.ResolveFilenames(c.ProtoImportPaths, protoFiles...)
128+
if len(protoImportPaths) > 0 {
129+
protoFiles, err = protoparse.ResolveFilenames(protoImportPaths, protoFiles...)
121130
if err != nil {
122131
return err
123132
}
@@ -132,6 +141,7 @@ func Linter(c *CliArgs, configs lint.Configs, protoFiles []string) error {
132141
if err != nil {
133142
return err
134143
}
144+
135145
fd, err := p.ParseFiles(protoFiles...)
136146
if err != nil {
137147
if err == protoparse.ErrInvalidSource {
@@ -149,27 +159,27 @@ func Linter(c *CliArgs, configs lint.Configs, protoFiles []string) error {
149159
}
150160

151161
// Create a Linter to lint the file descriptors.
152-
l := lint.New(globalRules, configs, lint.Debug(c.DebugFlag), lint.IgnoreCommentDisables(c.IgnoreCommentDisablesFlag))
162+
l := lint.New(globalRules, rules, lint.Debug(c.DebugFlag), lint.IgnoreCommentDisables(config.IgnoreCommentDisablesFlag))
153163
results, err := l.LintProtos(fd...)
154164
if err != nil {
155165
return err
156166
}
157167

158-
// Determine the output for writing the results.
159-
// Stdout is the default output.
160-
w := os.Stdout
161-
162168
// Determine the format for printing the results.
163169
// YAML format is the default.
164-
marshal := getOutputFormatFunc(c.FormatType)
170+
marshal := getOutputFormatFunc(config.FormatType)
165171

166172
// Print the results.
167173
b, err := marshal(results)
168174
if err != nil {
169175
return err
170176
}
171-
if _, err = w.Write(b); err != nil {
172-
return err
177+
178+
fmt.Println(string(b))
179+
180+
filterResults := lo.Filter(results, func(item lint.Response, index int) bool { return len(item.Problems) > 0 })
181+
if len(filterResults) > 0 {
182+
os.Exit(1)
173183
}
174184

175185
return nil

cmd/protobuild/cmd.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
linters "github.com/pubgo/protobuild/cmd/linter"
87
"io"
98
"io/fs"
109
"log/slog"
@@ -20,6 +19,7 @@ import (
2019
"github.com/pubgo/funk/pathutil"
2120
"github.com/pubgo/funk/recovery"
2221
"github.com/pubgo/funk/strutil"
22+
linters "github.com/pubgo/protobuild/cmd/linters"
2323
"github.com/pubgo/protobuild/internal/modutil"
2424
"github.com/pubgo/protobuild/internal/shutil"
2525
"github.com/pubgo/protobuild/internal/typex"
@@ -524,13 +524,51 @@ func Main() *cli.Command {
524524
},
525525
&cli.Command{
526526
Name: "lint",
527-
Usage: "lint protobuf",
527+
Usage: "lint protobuf https://linter.aip.dev/rules/",
528528
Flags: flags,
529529
Before: func(ctx context.Context, command *cli.Command) (context.Context, error) {
530530
return ctx, parseConfig()
531531
},
532532
Action: func(ctx context.Context, c *cli.Command) error {
533-
return linters.Linter(cliArgs, globalCfg.Linters, nil)
533+
var protoPaths []string
534+
for i := range globalCfg.Root {
535+
if pathutil.IsNotExist(globalCfg.Root[i]) {
536+
log.Printf("file %s not found", globalCfg.Root[i])
537+
continue
538+
}
539+
540+
assert.Must(filepath.WalkDir(globalCfg.Root[i], func(path string, d fs.DirEntry, err error) error {
541+
if err != nil {
542+
return err
543+
}
544+
545+
if d.IsDir() {
546+
protoPaths = append(protoPaths, path)
547+
}
548+
549+
return nil
550+
}))
551+
}
552+
553+
protoPaths = lo.Uniq(protoPaths)
554+
for _, path := range protoPaths {
555+
// check contains proto file in dir
556+
protoFiles := lo.Map(assert.Must1(os.ReadDir(path)), func(item os.DirEntry, index int) string {
557+
return filepath.Join(path, item.Name())
558+
})
559+
protoFiles = lo.Filter(protoFiles, func(item string, index int) bool { return strings.HasSuffix(item, ".proto") })
560+
if len(protoFiles) == 0 {
561+
continue
562+
}
563+
564+
includes := lo.Uniq(append(globalCfg.Includes, globalCfg.Vendor))
565+
err := linters.Linter(cliArgs, globalCfg.Linter, includes, protoFiles)
566+
if err != nil {
567+
return err
568+
}
569+
}
570+
571+
return nil
534572
},
535573
},
536574
},

cmd/protobuild/config.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package protobuild
22

3-
import "github.com/googleapis/api-linter/lint"
3+
import (
4+
"github.com/pubgo/protobuild/cmd/linters"
5+
)
46

57
type Config struct {
68
Checksum string `yaml:"checksum,omitempty" hash:"-"`
@@ -17,15 +19,8 @@ type Config struct {
1719
Depends []*depend `yaml:"deps,omitempty"`
1820
Plugins []*plugin `yaml:"plugins,omitempty" hash:"-"`
1921
changed bool
20-
Installers []string `yaml:"installers,omitempty" hash:"-"`
21-
Linters lint.Configs `yaml:"linters,omitempty" hash:"-"`
22-
}
23-
24-
type LinterConfig struct {
25-
IncludedPaths []string `json:"included_paths" yaml:"included_paths"`
26-
ExcludedPaths []string `json:"excluded_paths" yaml:"excluded_paths"`
27-
EnabledRules []string `json:"enabled_rules" yaml:"enabled_rules"`
28-
DisabledRules []string `json:"disabled_rules" yaml:"disabled_rules"`
22+
Installers []string `yaml:"installers,omitempty" hash:"-"`
23+
Linter linters.LinterConfig `yaml:"linter,omitempty" hash:"-"`
2924
}
3025

3126
type basePluginCfg struct {

0 commit comments

Comments
 (0)