-
Notifications
You must be signed in to change notification settings - Fork 75
/
buflint.go
155 lines (141 loc) · 5.08 KB
/
buflint.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package buflint contains the linting functionality.
//
// The primary entry point to this package is the Handler.
package buflint
import (
"context"
)
import (
"go.uber.org/zap"
)
import (
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufanalysis"
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufcheck"
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufcheck/buflint/buflintconfig"
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufcheck/buflint/internal/buflintv1"
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufcheck/buflint/internal/buflintv1beta1"
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufcheck/internal"
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufconfig"
"github.com/apache/dubbo-kubernetes/pkg/bufman/bufpkg/bufimage"
)
// AllFormatStrings are all format strings.
var AllFormatStrings = append(
bufanalysis.AllFormatStrings,
"config-ignore-yaml",
)
// Handler handles the main lint functionality.
type Handler interface {
// Check runs the lint checks.
//
// The image should have source code info for this to work properly.
//
// Images should be filtered with regards to imports before passing to this function.
Check(
ctx context.Context,
config *buflintconfig.Config,
image bufimage.Image,
) ([]bufanalysis.FileAnnotation, error)
}
// NewHandler returns a new Handler.
func NewHandler(logger *zap.Logger) Handler {
return newHandler(logger)
}
// RulesForConfig returns the rules for a given config.
//
// Should only be used for printing.
func RulesForConfig(config *buflintconfig.Config) ([]bufcheck.Rule, error) {
internalConfig, err := internalConfigForConfig(config)
if err != nil {
return nil, err
}
return rulesForInternalRules(internalConfig.Rules), nil
}
// GetAllRulesV1Beta1 gets all known rules.
//
// Should only be used for printing.
func GetAllRulesV1Beta1() ([]bufcheck.Rule, error) {
internalConfig, err := internalConfigForConfig(
&buflintconfig.Config{
Use: internal.AllIDsForVersionSpec(buflintv1beta1.VersionSpec),
Version: bufconfig.V1Beta1Version,
},
)
if err != nil {
return nil, err
}
return rulesForInternalRules(internalConfig.Rules), nil
}
// GetAllRulesV1 gets all known rules.
//
// Should only be used for printing.
func GetAllRulesV1() ([]bufcheck.Rule, error) {
internalConfig, err := internalConfigForConfig(
&buflintconfig.Config{
Use: internal.AllIDsForVersionSpec(buflintv1.VersionSpec),
Version: bufconfig.V1Version,
},
)
if err != nil {
return nil, err
}
return rulesForInternalRules(internalConfig.Rules), nil
}
// GetAllRulesAndCategoriesV1Beta1 returns all rules and categories for v1beta1 as a string slice.
//
// This is used for validation purposes only.
func GetAllRulesAndCategoriesV1Beta1() []string {
return internal.AllCategoriesAndIDsForVersionSpec(buflintv1beta1.VersionSpec)
}
// GetAllRulesAndCategoriesV1 returns all rules and categories for v1 as a string slice.
//
// This is used for validation purposes only.
func GetAllRulesAndCategoriesV1() []string {
return internal.AllCategoriesAndIDsForVersionSpec(buflintv1.VersionSpec)
}
func internalConfigForConfig(config *buflintconfig.Config) (*internal.Config, error) {
var versionSpec *internal.VersionSpec
switch config.Version {
case bufconfig.V1Beta1Version:
versionSpec = buflintv1beta1.VersionSpec
case bufconfig.V1Version:
versionSpec = buflintv1.VersionSpec
}
return internal.ConfigBuilder{
Use: config.Use,
Except: config.Except,
IgnoreRootPaths: config.IgnoreRootPaths,
IgnoreIDOrCategoryToRootPaths: config.IgnoreIDOrCategoryToRootPaths,
AllowCommentIgnores: config.AllowCommentIgnores,
EnumZeroValueSuffix: config.EnumZeroValueSuffix,
RPCAllowSameRequestResponse: config.RPCAllowSameRequestResponse,
RPCAllowGoogleProtobufEmptyRequests: config.RPCAllowGoogleProtobufEmptyRequests,
RPCAllowGoogleProtobufEmptyResponses: config.RPCAllowGoogleProtobufEmptyResponses,
ServiceSuffix: config.ServiceSuffix,
}.NewConfig(
versionSpec,
)
}
func rulesForInternalRules(rules []*internal.Rule) []bufcheck.Rule {
if rules == nil {
return nil
}
s := make([]bufcheck.Rule, len(rules))
for i, e := range rules {
s[i] = e
}
return s
}