-
Notifications
You must be signed in to change notification settings - Fork 245
/
lint.go
109 lines (102 loc) · 3.2 KB
/
lint.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
// Copyright 2020 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 lint
import (
"bytes"
"context"
"encoding/json"
"errors"
"strings"
"time"
"github.com/bufbuild/buf/internal/buf/bufcheck/buflint"
"github.com/bufbuild/buf/internal/buf/bufconfig"
"github.com/bufbuild/buf/internal/buf/bufcore/bufimage"
"github.com/bufbuild/buf/internal/pkg/app"
"github.com/bufbuild/buf/internal/pkg/app/applog"
"github.com/bufbuild/buf/internal/pkg/app/appproto"
"github.com/bufbuild/buf/internal/pkg/encoding"
"github.com/bufbuild/buf/internal/pkg/storage/storageos"
"google.golang.org/protobuf/types/pluginpb"
)
const defaultTimeout = 10 * time.Second
// Main is the main.
func Main() {
app.Main(context.Background(), appproto.NewRunFunc(appproto.HandlerFunc(handle)))
}
func handle(
ctx context.Context,
container app.EnvStderrContainer,
responseWriter appproto.ResponseWriter,
request *pluginpb.CodeGeneratorRequest,
) error {
responseWriter.SetFeatureProto3Optional()
externalConfig := &externalConfig{}
if err := encoding.UnmarshalJSONOrYAMLStrict(
[]byte(request.GetParameter()),
externalConfig,
); err != nil {
return err
}
timeout := externalConfig.Timeout
if timeout == 0 {
timeout = defaultTimeout
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
logger, err := applog.NewLogger(container.Stderr(), externalConfig.LogLevel, externalConfig.LogFormat)
if err != nil {
return err
}
readWriteBucket, err := storageos.NewReadWriteBucket(".")
if err != nil {
return err
}
config, err := bufconfig.ReadConfig(
ctx,
bufconfig.NewProvider(logger),
readWriteBucket,
encoding.GetJSONStringOrStringValue(externalConfig.InputConfig),
)
if err != nil {
return err
}
image, err := bufimage.NewImageForCodeGeneratorRequest(request)
if err != nil {
return err
}
image = bufimage.ImageWithoutImports(image)
fileAnnotations, err := buflint.NewHandler(logger).Check(
ctx,
config.Lint,
image,
)
if err != nil {
return err
}
if len(fileAnnotations) > 0 {
buffer := bytes.NewBuffer(nil)
if err := buflint.PrintFileAnnotations(buffer, fileAnnotations, externalConfig.ErrorFormat); err != nil {
return err
}
return errors.New(strings.TrimSpace(buffer.String()))
}
return nil
}
type externalConfig struct {
InputConfig json.RawMessage `json:"input_config,omitempty" yaml:"input_config,omitempty"`
LogLevel string `json:"log_level,omitempty" yaml:"log_level,omitempty"`
LogFormat string `json:"log_format,omitempty" yaml:"log_format,omitempty"`
ErrorFormat string `json:"error_format,omitempty" yaml:"error_format,omitempty"`
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}