/
generate.go
222 lines (213 loc) · 5.57 KB
/
generate.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// 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 generate
import (
"context"
"errors"
"fmt"
"strings"
"github.com/bufbuild/buf/internal/buf/bufanalysis"
"github.com/bufbuild/buf/internal/buf/bufcli"
"github.com/bufbuild/buf/internal/buf/bufcore/bufimage"
"github.com/bufbuild/buf/internal/buf/buffetch"
"github.com/bufbuild/buf/internal/buf/cmd/internal"
"github.com/bufbuild/buf/internal/pkg/app/appcmd"
"github.com/bufbuild/buf/internal/pkg/app/appflag"
"github.com/bufbuild/buf/internal/pkg/app/applog"
"github.com/bufbuild/buf/internal/pkg/app/appproto"
"github.com/bufbuild/buf/internal/pkg/app/appproto/appprotoexec"
"github.com/bufbuild/buf/internal/pkg/storage/storageos"
"github.com/bufbuild/buf/internal/pkg/stringutil"
"github.com/bufbuild/buf/internal/pkg/thread"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const (
errorFormatFlagName = "error-format"
filesFlagName = "file"
inputFlagName = "input"
inputConfigFlagName = "input-config"
pluginNameFlagName = "plugin"
pluginOutFlagName = "plugin-out"
pluginOptFlagName = "plugin-opt"
pluginPathFlagName = "plugin-path"
)
// NewCommand returns a new Command.
func NewCommand(
name string,
builder appflag.Builder,
moduleReaderProvider bufcli.ModuleReaderProvider,
) *appcmd.Command {
flags := newFlags()
return &appcmd.Command{
Use: name,
Short: "Generate stubs for a plugin.",
Args: cobra.NoArgs,
Run: builder.NewRunFunc(
func(ctx context.Context, container applog.Container) error {
return run(ctx, container, flags, moduleReaderProvider)
},
),
BindFlags: flags.Bind,
}
}
type flags struct {
ErrorFormat string
Files []string
Input string
InputConfig string
PluginName string
PluginOut string
PluginOpt []string
PluginPath string
}
func newFlags() *flags {
return &flags{}
}
func (f *flags) Bind(flagSet *pflag.FlagSet) {
flagSet.StringVar(
&f.ErrorFormat,
errorFormatFlagName,
"text",
fmt.Sprintf(
"The format for build errors, printed to stderr. Must be one of %s.",
stringutil.SliceToString(bufanalysis.AllFormatStrings),
),
)
flagSet.StringSliceVar(
&f.Files,
filesFlagName,
nil,
`Limit to specific files. This is an advanced feature and is not recommended.`,
)
flagSet.StringVar(
&f.Input,
inputFlagName,
".",
fmt.Sprintf(
`The source or image to generate from. Must be one of format %s.`,
buffetch.AllFormatsString,
),
)
flagSet.StringVar(
&f.InputConfig,
inputConfigFlagName,
"",
`The config file or data to use.`,
)
flagSet.StringVar(
&f.PluginName,
pluginNameFlagName,
"",
`The plugin to use. By default, protoc-gen-PLUGIN must be on your $PATH.`,
)
flagSet.StringVar(
&f.PluginOut,
pluginOutFlagName,
"",
`The output directory.`,
)
flagSet.StringSliceVar(
&f.PluginOpt,
pluginOptFlagName,
nil,
`The options to use.`,
)
flagSet.StringVar(
&f.PluginPath,
pluginPathFlagName,
"",
`The path to the plugin binary. By default, uses protoc-gen-PLUGIN on your $PATH.`,
)
}
func run(
ctx context.Context,
container applog.Container,
flags *flags,
moduleReaderProvider bufcli.ModuleReaderProvider,
) (retErr error) {
if flags.PluginName == "" {
return appcmd.NewInvalidArgumentErrorf("--%s is required", pluginNameFlagName)
}
if flags.PluginOut == "" {
return appcmd.NewInvalidArgumentErrorf("--%s is required", pluginOutFlagName)
}
ref, err := buffetch.NewRefParser(container.Logger()).GetRef(ctx, flags.Input)
if err != nil {
return fmt.Errorf("--%s: %v", inputFlagName, err)
}
moduleReader, err := moduleReaderProvider.GetModuleReader(ctx, container)
if err != nil {
return err
}
env, fileAnnotations, err := internal.NewBufwireEnvReader(
container.Logger(),
inputConfigFlagName,
moduleReader,
).GetEnv(
ctx,
container,
ref,
flags.InputConfig,
flags.Files, // we filter on files
false, // input files must exist
false, // we must include source info for generation
)
if err != nil {
return err
}
if len(fileAnnotations) > 0 {
if err := bufanalysis.PrintFileAnnotations(container.Stderr(), fileAnnotations, flags.ErrorFormat); err != nil {
return err
}
return errors.New("")
}
images, err := bufimage.ImageByDir(env.Image())
if err != nil {
return err
}
readWriteBucket, err := storageos.NewReadWriteBucket(flags.PluginOut)
if err != nil {
return err
}
var handlerOptions []appprotoexec.HandlerOption
if flags.PluginPath != "" {
handlerOptions = append(handlerOptions, appprotoexec.HandlerWithPluginPath(flags.PluginPath))
}
handler, err := appprotoexec.NewHandler(
container.Logger(),
flags.PluginName,
handlerOptions...,
)
if err != nil {
return err
}
executor := appproto.NewExecutor(container.Logger(), handler)
jobs := make([]func() error, len(images))
for i, image := range images {
image := image
jobs[i] = func() error {
return executor.Execute(
ctx,
container,
readWriteBucket,
bufimage.ImageToCodeGeneratorRequest(
image,
strings.Join(flags.PluginOpt, ","),
),
)
}
}
return thread.Parallelize(jobs...)
}