-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (82 loc) · 2.69 KB
/
main.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
// Copyright 2024-2025 Buf Technologies, Inc.
//
// Licensed 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 main implements a simple plugin that checks that syntax is specified in every file.
//
// This is just demonstrating the additional functionality that check.Files have
// over FileDescriptors and FileDescriptorProtos.
//
// To use this plugin:
//
// # buf.yaml
// version: v2
// lint:
// use:
// - STANDARD # omit if you do not want to use the rules builtin to buf
// - PLUGIN_SYNTAX_SPECIFIED
// plugins:
// - plugin: buf-plugin-syntax-specified
//
// Note that the buf CLI implements this check by as a builtin rule, but this is just for example.
package main
import (
"context"
"buf.build/go/bufplugin/check"
"buf.build/go/bufplugin/check/checkutil"
"buf.build/go/bufplugin/descriptor"
"buf.build/go/bufplugin/info"
)
// syntaxSpecifiedRuleID is the Rule ID of the syntax specified Rule.
//
// This has a "PLUGIN_" prefix as the buf CLI has a rule "SYNTAX_SPECIFIED" builtin,
// and plugins/the buf CLI must have unique Rule IDs.
const syntaxSpecifiedRuleID = "PLUGIN_SYNTAX_SPECIFIED"
var (
// syntaxSpecifiedRuleSpec is the RuleSpec for the syntax specified Rule.
syntaxSpecifiedRuleSpec = &check.RuleSpec{
ID: syntaxSpecifiedRuleID,
Default: true,
Purpose: "Checks that syntax is specified.",
Type: check.RuleTypeLint,
Handler: checkutil.NewFileRuleHandler(checkSyntaxSpecified, checkutil.WithoutImports()),
}
// spec is the Spec for the syntax specified plugin.
spec = &check.Spec{
Rules: []*check.RuleSpec{
syntaxSpecifiedRuleSpec,
},
// Optional.
Info: &info.Spec{
SPDXLicenseID: "apache-2.0",
LicenseURL: "https://github.com/bufbuild/bufplugin-go/blob/main/LICENSE",
},
}
)
func main() {
check.Main(spec)
}
func checkSyntaxSpecified(
_ context.Context,
responseWriter check.ResponseWriter,
_ check.Request,
fileDescriptor descriptor.FileDescriptor,
) error {
if fileDescriptor.IsSyntaxUnspecified() {
syntax := fileDescriptor.FileDescriptorProto().GetSyntax()
responseWriter.AddAnnotation(
check.WithMessagef("Syntax should be specified but was %q.", syntax),
check.WithDescriptor(fileDescriptor.ProtoreflectFileDescriptor()),
)
}
return nil
}