/
build.go
195 lines (186 loc) · 5.52 KB
/
build.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
// 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 build
import (
"context"
"errors"
"fmt"
"github.com/bufbuild/buf/internal/buf/bufanalysis"
"github.com/bufbuild/buf/internal/buf/bufcli"
"github.com/bufbuild/buf/internal/buf/bufconfig"
"github.com/bufbuild/buf/internal/buf/buffetch"
imageinternal "github.com/bufbuild/buf/internal/buf/cmd/buf/command/image/internal"
"github.com/bufbuild/buf/internal/pkg/app/appcmd"
"github.com/bufbuild/buf/internal/pkg/app/appflag"
"github.com/bufbuild/buf/internal/pkg/stringutil"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const (
asFileDescriptorSetFlagName = "as-file-descriptor-set"
errorFormatFlagName = "error-format"
excludeImportsFlagName = "exclude-imports"
excludeSourceInfoFlagName = "exclude-source-info"
filesFlagName = "file"
outputFlagName = "output"
outputFlagShortName = "o"
sourceFlagName = "source"
sourceConfigFlagName = "source-config"
)
// NewCommand returns a new Command.
func NewCommand(
name string,
builder appflag.Builder,
moduleResolverReaderProvider bufcli.ModuleResolverReaderProvider,
) *appcmd.Command {
flags := newFlags()
return &appcmd.Command{
Use: name,
Short: "Build all files from the input location and output an Image or FileDescriptorSet.",
Args: cobra.NoArgs,
Run: builder.NewRunFunc(
func(ctx context.Context, container appflag.Container) error {
return run(ctx, container, flags, moduleResolverReaderProvider)
},
),
BindFlags: flags.Bind,
}
}
type flags struct {
AsFileDescriptorSet bool
ErrorFormat string
ExcludeImports bool
ExcludeSourceInfo bool
Files []string
Output string
Source string
SourceConfig string
}
func newFlags() *flags {
return &flags{}
}
func (f *flags) Bind(flagSet *pflag.FlagSet) {
imageinternal.BindAsFileDescriptorSet(flagSet, &f.AsFileDescriptorSet, asFileDescriptorSetFlagName)
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),
),
)
imageinternal.BindExcludeImports(flagSet, &f.ExcludeImports, excludeImportsFlagName)
imageinternal.BindExcludeSourceInfo(flagSet, &f.ExcludeSourceInfo, excludeSourceInfoFlagName)
imageinternal.BindFiles(flagSet, &f.Files, filesFlagName)
flagSet.StringVarP(
&f.Output,
outputFlagName,
outputFlagShortName,
"",
fmt.Sprintf(
`Required. The location to write the image to. Must be one of format %s.`,
buffetch.ImageFormatsString,
),
)
flagSet.StringVar(
&f.Source,
sourceFlagName,
".",
fmt.Sprintf(
`The source or module to build. Must be one of format %s.`,
buffetch.SourceOrModuleFormatsString,
),
)
flagSet.StringVar(
&f.SourceConfig,
sourceConfigFlagName,
"",
`The config file or data to use.`,
)
}
func run(
ctx context.Context,
container appflag.Container,
flags *flags,
moduleResolverReaderProvider bufcli.ModuleResolverReaderProvider,
) error {
if flags.Output == "" {
return appcmd.NewInvalidArgumentErrorf("--%s is required", outputFlagName)
}
sourceOrModuleRef, err := buffetch.NewSourceOrModuleRefParser(
container.Logger(),
).GetSourceOrModuleRef(ctx, flags.Source)
if err != nil {
return fmt.Errorf("--%s: %v", sourceFlagName, err)
}
configProvider := bufconfig.NewProvider(container.Logger())
moduleResolver, err := moduleResolverReaderProvider.GetModuleResolver(ctx, container)
if err != nil {
return err
}
moduleReader, err := moduleResolverReaderProvider.GetModuleReader(ctx, container)
if err != nil {
return err
}
env, fileAnnotations, err := bufcli.NewWireEnvReader(
container.Logger(),
configProvider,
moduleResolver,
moduleReader,
// must be source or module only
).GetSourceOrModuleEnv(
ctx,
container,
sourceOrModuleRef,
flags.SourceConfig,
flags.Files,
false,
flags.ExcludeSourceInfo,
)
if err != nil {
return err
}
if len(fileAnnotations) > 0 {
// stderr since we do output to stdout potentially
if err := bufanalysis.PrintFileAnnotations(
container.Stderr(),
fileAnnotations,
flags.ErrorFormat,
); err != nil {
return err
}
// app works on the concept that an error results in a non-zero exit code
// we already printed the messages with PrintFileAnnotations so we do
// not want to print any additional error message
// we could put the FileAnnotations in this error, but in general with
// linting/breaking change detection we actually print them to stdout
// so doing this here is consistent with lint/breaking change detection
return errors.New("")
}
imageRef, err := buffetch.NewImageRefParser(container.Logger()).GetImageRef(ctx, flags.Output)
if err != nil {
return fmt.Errorf("--%s: %v", outputFlagName, err)
}
return bufcli.NewWireImageWriter(
container.Logger(),
).PutImage(
ctx,
container,
imageRef,
env.Image(),
flags.AsFileDescriptorSet,
flags.ExcludeImports,
)
}