-
Notifications
You must be signed in to change notification settings - Fork 248
/
bufcli.go
855 lines (798 loc) · 28.1 KB
/
bufcli.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
// Copyright 2020-2021 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 bufcli
import (
"bufio"
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/bufbuild/buf/private/buf/bufapp"
"github.com/bufbuild/buf/private/buf/bufcheck/buflint"
"github.com/bufbuild/buf/private/buf/bufconfig"
"github.com/bufbuild/buf/private/buf/buffetch"
"github.com/bufbuild/buf/private/buf/bufwire"
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
"github.com/bufbuild/buf/private/bufpkg/bufapiclient"
"github.com/bufbuild/buf/private/bufpkg/bufapimodule"
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimagebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulecache"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/bufpkg/bufrpc"
"github.com/bufbuild/buf/private/bufpkg/buftransport"
"github.com/bufbuild/buf/private/gen/proto/apiclient/buf/alpha/registry/v1alpha1/registryv1alpha1apiclient"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appflag"
"github.com/bufbuild/buf/private/pkg/app/appname"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/filelock"
"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/httpauth"
"github.com/bufbuild/buf/private/pkg/netrc"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/rpc/rpcauth"
"github.com/bufbuild/buf/private/pkg/storage/storageos"
"github.com/spf13/pflag"
"go.uber.org/zap"
"golang.org/x/term"
)
const (
// Version is the CLI version of buf.
Version = "1.0.0-rc5"
// DeprecationMessageSuffix is the suffix that should be added to any command or flag
// that was deprecated and removed for v1.0.
DeprecationMessageSuffix = `
At Buf, we take compatibility very seriously. When we say v1.0, we mean it - we hope "buf" will be
stable on v1 for the next decade, and if there is something we want to change, it is our responsibility
to make sure that we don't break you, not your responsibility to change because of us. We have learned
a lot about "buf" usage in the last two years of our beta, and have deprecated flags and commands
(including this one) as we go, but for v1.0, we have removed the deprecated items to make sure we
have a clean setup going forward.
Update your invocation for v1.0 and you'll be good to go. We apologize for any inconvenience.`
inputHTTPSUsernameEnvKey = "BUF_INPUT_HTTPS_USERNAME"
inputHTTPSPasswordEnvKey = "BUF_INPUT_HTTPS_PASSWORD"
inputSSHKeyFileEnvKey = "BUF_INPUT_SSH_KEY_FILE"
inputSSHKnownHostsFilesEnvKey = "BUF_INPUT_SSH_KNOWN_HOSTS_FILES"
tokenEnvKey = "BUF_TOKEN"
alphaSuppressWarningsEnvKey = "BUF_ALPHA_SUPPRESS_WARNINGS"
betaSuppressWarningsEnvKey = "BUF_BETA_SUPPRESS_WARNINGS"
inputHashtagFlagName = "__hashtag__"
inputHashtagFlagShortName = "#"
userPromptAttempts = 3
)
var (
// defaultHTTPClient is the client we use for HTTP requests.
// Timeout should be set through context for calls to ImageConfigReader, not through http.Client
defaultHTTPClient = &http.Client{}
// defaultHTTPAuthenticator is the default authenticator
// used for HTTP requests.
defaultHTTPAuthenticator = httpauth.NewMultiAuthenticator(
httpauth.NewNetrcAuthenticator(),
// must keep this for legacy purposes
httpauth.NewEnvAuthenticator(
inputHTTPSPasswordEnvKey,
inputHTTPSPasswordEnvKey,
),
)
// defaultGitClonerOptions defines the default git clone options.
defaultGitClonerOptions = git.ClonerOptions{
HTTPSUsernameEnvKey: inputHTTPSUsernameEnvKey,
HTTPSPasswordEnvKey: inputHTTPSPasswordEnvKey,
SSHKeyFileEnvKey: inputSSHKeyFileEnvKey,
SSHKnownHostsFilesEnvKey: inputSSHKnownHostsFilesEnvKey,
}
// AllCacheModuleRelDirPaths are all directory paths for all time concerning the module cache.
//
// These are normalized.
// These are relative to container.CacheDirPath().
//
// This variable is used for clearing the cache.
AllCacheModuleRelDirPaths = []string{
v1beta1CacheModuleDataRelDirPath,
v1beta1CacheModuleLockRelDirPath,
v1CacheModuleDataRelDirPath,
v1CacheModuleLockRelDirPath,
v1CacheModuleSumRelDirPath,
}
// ErrNotATTY is returned when an input io.Reader is not a TTY where it is expected.
ErrNotATTY = errors.New("reader was not a TTY as expected")
// v1CacheModuleDataRelDirPath is the relative path to the cache directory where module data
// was stored in v1beta1.
//
// Normalized.
v1beta1CacheModuleDataRelDirPath = "mod"
// v1CacheModuleLockRelDirPath is the relative path to the cache directory where module lock files
// were stored in v1beta1.
//
// Normalized.
v1beta1CacheModuleLockRelDirPath = normalpath.Join("lock", "mod")
// v1CacheModuleDataRelDirPath is the relative path to the cache directory where module data is stored.
//
// Normalized.
// This is where the actual "clones" of the modules are located.
v1CacheModuleDataRelDirPath = normalpath.Join("v1", "module", "data")
// v1CacheModuleLockRelDirPath is the relative path to the cache directory where module lock files are stored.
//
// Normalized.
// These lock files are used to make sure that multiple buf processes do not corrupt the cache.
v1CacheModuleLockRelDirPath = normalpath.Join("v1", "module", "lock")
// v1CacheModuleSumRelDirPath is the relative path to the cache directory where module digests are stored.
//
// Normalized.
// These digests are used to make sure that the data written is actually what we expect, and if it is not,
// we clear an entry from the cache, i.e. delete the relevant data directory.
v1CacheModuleSumRelDirPath = normalpath.Join("v1", "module", "sum")
)
// GlobalFlags contains global flags for buf commands.
type GlobalFlags struct{}
// NewGlobalFlags creates a new GlobalFlags with default values..
func NewGlobalFlags() *GlobalFlags {
return &GlobalFlags{}
}
// BindRoot binds the global flags to the root command flag set.
func (*GlobalFlags) BindRoot(*pflag.FlagSet) {}
// BindAsFileDescriptorSet binds the exclude-imports flag.
func BindAsFileDescriptorSet(flagSet *pflag.FlagSet, addr *bool, flagName string) {
flagSet.BoolVar(
addr,
flagName,
false,
`Output as a google.protobuf.FileDescriptorSet instead of an image.
Note that images are wire-compatible with FileDescriptorSets, however this flag will strip
the additional metadata added for Buf usage.`,
)
}
// BindExcludeImports binds the exclude-imports flag.
func BindExcludeImports(flagSet *pflag.FlagSet, addr *bool, flagName string) {
flagSet.BoolVar(
addr,
flagName,
false,
"Exclude imports.",
)
}
// BindExcludeSourceInfo binds the exclude-source-info flag.
func BindExcludeSourceInfo(flagSet *pflag.FlagSet, addr *bool, flagName string) {
flagSet.BoolVar(
addr,
flagName,
false,
"Exclude source info.",
)
}
// BindPaths binds the paths flag.
func BindPaths(
flagSet *pflag.FlagSet,
pathsAddr *[]string,
pathsFlagName string,
) {
flagSet.StringSliceVar(
pathsAddr,
pathsFlagName,
nil,
`Limit to specific files or directories, for example "proto/a/a.proto" or "proto/a".
If specified multiple times, the union will be taken.`,
)
}
// BindPathsAndDeprecatedFiles binds the paths flag and the deprecated files flag.
//
// We do not mark the files flag as deprecated as we now error when we hit it, and
// print out an error message via the returned error.
func BindPathsAndDeprecatedFiles(
flagSet *pflag.FlagSet,
pathsAddr *[]string,
pathsFlagName string,
filesAddr *[]string,
filesFlagName string,
) {
BindPaths(flagSet, pathsAddr, pathsFlagName)
flagSet.StringSliceVar(
filesAddr,
filesFlagName,
nil,
`Limit to specific files.
If specified multiple times, the union will be taken.`,
)
_ = flagSet.MarkHidden(filesFlagName)
}
// BindInputHashtag binds the input hashtag flag.
//
// This needs to be added to any command that has the input as the first argument.
// This deals with the situation "buf build -#format=json" which results in
// a parse error from pflag.
func BindInputHashtag(flagSet *pflag.FlagSet, addr *string) {
flagSet.StringVarP(
addr,
inputHashtagFlagName,
inputHashtagFlagShortName,
"",
"",
)
_ = flagSet.MarkHidden(inputHashtagFlagName)
}
// GetInputLong gets the long command description for an input-based command.
func GetInputLong(inputArgDescription string) string {
return fmt.Sprintf(
`The first argument is %s.
The first argument must be one of format %s.
If no argument is specified, defaults to ".".`,
inputArgDescription,
buffetch.AllFormatsString,
)
}
// GetSourceOrModuleLong gets the long command description for an input-based command.
func GetSourceOrModuleLong(inputArgDescription string) string {
return fmt.Sprintf(
`The first argument is %s.
The first argument must be one of format %s.
If no argument is specified, defaults to ".".`,
inputArgDescription,
buffetch.SourceOrModuleFormatsString,
)
}
// GetInputValue gets the first arg and errors if the deprecated flag is used.
//
// Also parses the special input hashtag flag that deals with the situation "buf build -#format=json".
// The existence of 0 or 1 args should be handled by the Args field on Command.
func GetInputValue(
container appflag.Container,
inputHashtag string,
deprecatedFlag string,
deprecatedFlagName string,
defaultValue string,
) (string, error) {
var arg string
switch numArgs := container.NumArgs(); numArgs {
case 0:
if inputHashtag != "" {
arg = "-#" + inputHashtag
}
case 1:
arg = container.Arg(0)
if arg == "" {
return "", errors.New("first argument is present but empty")
}
// if arg is non-empty and inputHashtag is non-empty, this means two arguments were specified
if inputHashtag != "" {
return "", errors.New("only 1 argument allowed but 2 arguments specified")
}
default:
return "", fmt.Errorf("only 1 argument allowed but %d arguments specified", numArgs)
}
if arg != "" && deprecatedFlag != "" {
return "", fmt.Errorf("cannot specify both first argument and deprecated flag --%s, use the first argument instead%s", deprecatedFlagName, DeprecationMessageSuffix)
}
if arg != "" {
return arg, nil
}
if deprecatedFlag != "" {
return "", fmt.Errorf("flag --%s is no longer supported, use the first argument instead%s", deprecatedFlagName, DeprecationMessageSuffix)
}
return defaultValue, nil
}
// GetStringFlagOrDeprecatedFlag gets the flag, or the deprecated flag.
//
// An error is returned if the deprecated flag is used.
func GetStringFlagOrDeprecatedFlag(
flag string,
flagName string,
deprecatedFlag string,
deprecatedFlagName string,
) (string, error) {
if flag != "" && deprecatedFlag != "" {
return "", fmt.Errorf("cannot specify both --%s and --%s, use --%s instead%s", flagName, deprecatedFlagName, flagName, DeprecationMessageSuffix)
}
if flag != "" {
return flag, nil
}
if deprecatedFlag != "" {
return "", fmt.Errorf("flag --%s is no longer supported, use --%s instead%s", deprecatedFlagName, flagName, DeprecationMessageSuffix)
}
return "", nil
}
// GetStringSliceFlagOrDeprecatedFlag gets the flag, or the deprecated flag.
//
// An error is returned if the deprecated flag is used.
func GetStringSliceFlagOrDeprecatedFlag(
flag []string,
flagName string,
deprecatedFlag []string,
deprecatedFlagName string,
) ([]string, error) {
if len(flag) > 0 && len(deprecatedFlag) > 0 {
return nil, fmt.Errorf("cannot specify both --%s and --%s, use --%s instead%s", flagName, deprecatedFlagName, flagName, DeprecationMessageSuffix)
}
if len(flag) > 0 {
return flag, nil
}
if len(deprecatedFlag) > 0 {
return nil, fmt.Errorf("flag --%s is no longer supported, use --%s instead%s", deprecatedFlagName, flagName, DeprecationMessageSuffix)
}
return nil, nil
}
// WarnAlphaCommand prints a warning for a alpha command unless the alphaSuppressWarningsEnvKey
// environment variable is set.
func WarnAlphaCommand(ctx context.Context, container appflag.Container) {
if container.Env(alphaSuppressWarningsEnvKey) == "" {
container.Logger().Warn("This command is in alpha. It is hidden for a reason. This command is purely for development purposes, and may never even be promoted to beta, do not rely on this command's functionality. To suppress this warning, set " + alphaSuppressWarningsEnvKey + "=1")
}
}
// WarnBetaCommand prints a warning for a beta command unless the betaSuppressWarningsEnvKey
// environment variable is set.
func WarnBetaCommand(ctx context.Context, container appflag.Container) {
if container.Env(betaSuppressWarningsEnvKey) == "" {
container.Logger().Warn("This command is in beta. It is unstable and likely to change. To suppress this warning, set " + betaSuppressWarningsEnvKey + "=1")
}
}
// NewWireImageConfigReader returns a new ImageConfigReader.
func NewWireImageConfigReader(
container appflag.Container,
storageosProvider storageos.Provider,
runner command.Runner,
registryProvider registryv1alpha1apiclient.Provider,
) (bufwire.ImageConfigReader, error) {
logger := container.Logger()
moduleResolver := bufapimodule.NewModuleResolver(logger, registryProvider)
moduleReader, err := NewModuleReaderAndCreateCacheDirs(container, registryProvider)
if err != nil {
return nil, err
}
return bufwire.NewImageConfigReader(
logger,
storageosProvider,
newFetchReader(logger, storageosProvider, runner, moduleResolver, moduleReader),
bufmodulebuild.NewModuleBucketBuilder(logger),
bufmodulebuild.NewModuleFileSetBuilder(logger, moduleReader),
bufimagebuild.NewBuilder(logger),
), nil
}
// NewWireModuleConfigReader returns a new ModuleConfigReader.
func NewWireModuleConfigReader(
container appflag.Container,
storageosProvider storageos.Provider,
runner command.Runner,
registryProvider registryv1alpha1apiclient.Provider,
) (bufwire.ModuleConfigReader, error) {
logger := container.Logger()
moduleResolver := bufapimodule.NewModuleResolver(logger, registryProvider)
moduleReader, err := NewModuleReaderAndCreateCacheDirs(container, registryProvider)
if err != nil {
return nil, err
}
return bufwire.NewModuleConfigReader(
logger,
storageosProvider,
newFetchReader(logger, storageosProvider, runner, moduleResolver, moduleReader),
bufmodulebuild.NewModuleBucketBuilder(logger),
), nil
}
// NewWireModuleConfigReaderForModuleReader returns a new ModuleConfigReader using
// the given ModuleReader.
func NewWireModuleConfigReaderForModuleReader(
container appflag.Container,
storageosProvider storageos.Provider,
runner command.Runner,
registryProvider registryv1alpha1apiclient.Provider,
moduleReader bufmodule.ModuleReader,
) (bufwire.ModuleConfigReader, error) {
logger := container.Logger()
moduleResolver := bufapimodule.NewModuleResolver(logger, registryProvider)
return bufwire.NewModuleConfigReader(
logger,
storageosProvider,
newFetchReader(logger, storageosProvider, runner, moduleResolver, moduleReader),
bufmodulebuild.NewModuleBucketBuilder(logger),
), nil
}
// NewWireFileLister returns a new FileLister.
func NewWireFileLister(
container appflag.Container,
storageosProvider storageos.Provider,
runner command.Runner,
registryProvider registryv1alpha1apiclient.Provider,
) (bufwire.FileLister, error) {
logger := container.Logger()
moduleResolver := bufapimodule.NewModuleResolver(logger, registryProvider)
moduleReader, err := NewModuleReaderAndCreateCacheDirs(container, registryProvider)
if err != nil {
return nil, err
}
return bufwire.NewFileLister(
logger,
storageosProvider,
newFetchReader(logger, storageosProvider, runner, moduleResolver, moduleReader),
bufmodulebuild.NewModuleBucketBuilder(logger),
bufmodulebuild.NewModuleFileSetBuilder(logger, moduleReader),
bufimagebuild.NewBuilder(logger),
), nil
}
// NewWireImageReader returns a new ImageReader.
func NewWireImageReader(
logger *zap.Logger,
storageosProvider storageos.Provider,
runner command.Runner,
) bufwire.ImageReader {
return bufwire.NewImageReader(
logger,
newFetchImageReader(logger, storageosProvider, runner),
)
}
// NewWireImageWriter returns a new ImageWriter.
func NewWireImageWriter(
logger *zap.Logger,
) bufwire.ImageWriter {
return bufwire.NewImageWriter(
logger,
buffetch.NewWriter(
logger,
),
)
}
// NewModuleReaderAndCreateCacheDirs returns a new ModuleReader while creating the
// required cache directories.
func NewModuleReaderAndCreateCacheDirs(
container appflag.Container,
registryProvider registryv1alpha1apiclient.Provider,
) (bufmodule.ModuleReader, error) {
cacheModuleDataDirPath := normalpath.Join(container.CacheDirPath(), v1CacheModuleDataRelDirPath)
cacheModuleLockDirPath := normalpath.Join(container.CacheDirPath(), v1CacheModuleLockRelDirPath)
cacheModuleSumDirPath := normalpath.Join(container.CacheDirPath(), v1CacheModuleSumRelDirPath)
if err := checkExistingCacheDirs(
container.CacheDirPath(),
container.CacheDirPath(),
cacheModuleDataDirPath,
cacheModuleLockDirPath,
cacheModuleSumDirPath,
); err != nil {
return nil, err
}
if err := createCacheDirs(
cacheModuleDataDirPath,
cacheModuleLockDirPath,
cacheModuleSumDirPath,
); err != nil {
return nil, err
}
storageosProvider := storageos.NewProvider(storageos.ProviderWithSymlinks())
// do NOT want to enable symlinks for our cache
dataReadWriteBucket, err := storageosProvider.NewReadWriteBucket(cacheModuleDataDirPath)
if err != nil {
return nil, err
}
// do NOT want to enable symlinks for our cache
sumReadWriteBucket, err := storageosProvider.NewReadWriteBucket(cacheModuleSumDirPath)
if err != nil {
return nil, err
}
fileLocker, err := filelock.NewLocker(cacheModuleLockDirPath)
if err != nil {
return nil, err
}
moduleReader := bufmodulecache.NewModuleReader(
container.Logger(),
container.VerbosePrinter(),
fileLocker,
dataReadWriteBucket,
sumReadWriteBucket,
bufapimodule.NewModuleReader(registryProvider),
registryProvider,
)
return moduleReader, nil
}
// NewConfig creates a new Config.
func NewConfig(container appflag.Container) (*bufapp.Config, error) {
externalConfig := bufapp.ExternalConfig{}
if err := appname.ReadConfig(container, &externalConfig); err != nil {
return nil, err
}
return bufapp.NewConfig(container, externalConfig)
}
// NewRegistryProvider creates a new registryv1alpha1apiclient.Provider.
func NewRegistryProvider(ctx context.Context, container appflag.Container) (registryv1alpha1apiclient.Provider, error) {
config, err := NewConfig(container)
if err != nil {
return nil, err
}
options := []bufapiclient.RegistryProviderOption{
bufapiclient.RegistryProviderWithContextModifierProvider(NewContextModifierProvider(container)),
}
if buftransport.IsAPISubdomainEnabled(container) {
options = append(options, bufapiclient.RegistryProviderWithAddressMapper(buftransport.PrependAPISubdomain))
}
return bufapiclient.NewRegistryProvider(
ctx,
container.Logger(),
config.TLS,
options...,
)
}
// NewContextModifierProvider returns a new context modifier provider for API providers.
//
// Public for use in other packages that provide API provider constructors.
func NewContextModifierProvider(
container appflag.Container,
) func(string) (func(context.Context) context.Context, error) {
return func(address string) (func(context.Context) context.Context, error) {
token := container.Env(tokenEnvKey)
if token == "" {
machine, err := netrc.GetMachineForName(container, address)
if err != nil {
return nil, fmt.Errorf("failed to read server password from netrc: %w", err)
}
if machine != nil {
token = machine.Password()
}
}
return func(ctx context.Context) context.Context {
ctx = bufrpc.WithOutgoingCLIVersionHeader(ctx, Version)
if token != "" {
return rpcauth.WithToken(ctx, token)
}
return ctx
}, nil
}
}
// PromptUserForDelete is used to receieve user confirmation that a specific
// entity should be deleted. If the user's answer does not match the expected
// answer, an error is returned.
// ErrNotATTY is returned if the input containers Stdin is not a terminal.
func PromptUserForDelete(container app.Container, entityType string, expectedAnswer string) error {
confirmation, err := PromptUser(
container,
fmt.Sprintf(
"Please confirm that you want to DELETE this %s by entering its name (%s) again."+
"\nWARNING: This action is NOT reversible!\n",
entityType,
expectedAnswer,
),
)
if err != nil {
if errors.Is(err, ErrNotATTY) {
return errors.New("cannot perform an interactive delete from a non-TTY device")
}
return err
}
if confirmation != expectedAnswer {
return fmt.Errorf(
"expected %q, but received %q",
expectedAnswer,
confirmation,
)
}
return nil
}
// PromptUser reads a line from Stdin, prompting the user with the prompt first.
// The prompt is repeatedly shown until the user provides a non-empty response.
// ErrNotATTY is returned if the input containers Stdin is not a terminal.
func PromptUser(container app.Container, prompt string) (string, error) {
return promptUser(container, prompt, false)
}
// PromptUserForPassword reads a line from Stdin, prompting the user with the prompt first.
// The prompt is repeatedly shown until the user provides a non-empty response.
// ErrNotATTY is returned if the input containers Stdin is not a terminal.
func PromptUserForPassword(container app.Container, prompt string) (string, error) {
return promptUser(container, prompt, true)
}
// promptUser reads a line from Stdin, prompting the user with the prompt first.
// The prompt is repeatedly shown until the user provides a non-empty response.
// ErrNotATTY is returned if the input containers Stdin is not a terminal.
func promptUser(container app.Container, prompt string, isPassword bool) (string, error) {
file, ok := container.Stdin().(*os.File)
if !ok || !term.IsTerminal(int(file.Fd())) {
return "", ErrNotATTY
}
var attempts int
for attempts < userPromptAttempts {
attempts++
if _, err := fmt.Fprint(
container.Stdout(),
prompt,
); err != nil {
return "", NewInternalError(err)
}
var value string
if isPassword {
data, err := term.ReadPassword(int(file.Fd()))
if err != nil {
return "", NewInternalError(err)
}
value = string(data)
} else {
scanner := bufio.NewScanner(container.Stdin())
if !scanner.Scan() {
return "", NewInternalError(scanner.Err())
}
value = scanner.Text()
if err := scanner.Err(); err != nil {
return "", NewInternalError(err)
}
}
if len(strings.TrimSpace(value)) != 0 {
// We want to preserve spaces in user input, so we only apply
// strings.TrimSpace to verify an answer was provided.
return value, nil
}
if attempts < userPromptAttempts {
// We only want to ask the user to try again if they actually
// have another attempt.
if _, err := fmt.Fprintln(
container.Stdout(),
"An answer was not provided; please try again.",
); err != nil {
return "", NewInternalError(err)
}
}
}
return "", NewTooManyEmptyAnswersError(userPromptAttempts)
}
// ReadModuleWithWorkspacesDisabled gets a module from a source ref.
//
// Workspaces are disabled for this function.
func ReadModuleWithWorkspacesDisabled(
ctx context.Context,
container appflag.Container,
storageosProvider storageos.Provider,
runner command.Runner,
source string,
) (bufmodule.Module, bufmoduleref.ModuleIdentity, error) {
sourceRef, err := buffetch.NewSourceRefParser(
container.Logger(),
).GetSourceRef(
ctx,
source,
)
if err != nil {
return nil, nil, err
}
sourceBucket, err := newFetchSourceReader(
container.Logger(),
storageosProvider,
runner,
).GetSourceBucket(
ctx,
container,
sourceRef,
buffetch.GetSourceBucketWithWorkspacesDisabled(),
)
if err != nil {
return nil, nil, err
}
existingConfigFilePath, err := bufconfig.ExistingConfigFilePath(ctx, sourceBucket)
if err != nil {
return nil, nil, NewInternalError(err)
}
if existingConfigFilePath == "" {
return nil, nil, ErrNoConfigFile
}
// TODO: This should just read a lock file
sourceConfig, err := bufconfig.GetConfigForBucket(
ctx,
sourceBucket,
)
if err != nil {
return nil, nil, err
}
moduleIdentity := sourceConfig.ModuleIdentity
if moduleIdentity == nil {
return nil, nil, ErrNoModuleName
}
module, err := bufmodulebuild.NewModuleBucketBuilder(container.Logger()).BuildForBucket(
ctx,
sourceBucket,
sourceConfig.Build,
)
if err != nil {
return nil, nil, err
}
return module, moduleIdentity, err
}
// ValidateErrorFormatFlag validates the error format flag for all commands but lint.
func ValidateErrorFormatFlag(errorFormatString string, errorFormatFlagName string) error {
return validateErrorFormatFlag(bufanalysis.AllFormatStrings, errorFormatString, errorFormatFlagName)
}
// ValidateErrorFormatFlagLint validates the error format flag for lint.
func ValidateErrorFormatFlagLint(errorFormatString string, errorFormatFlagName string) error {
return validateErrorFormatFlag(buflint.AllFormatStrings, errorFormatString, errorFormatFlagName)
}
func validateErrorFormatFlag(validFormatStrings []string, errorFormatString string, errorFormatFlagName string) error {
for _, formatString := range validFormatStrings {
if errorFormatString == formatString {
return nil
}
}
return appcmd.NewInvalidArgumentErrorf("--%s: invalid format: %q", errorFormatFlagName, errorFormatString)
}
// newFetchReader creates a new buffetch.Reader with the default HTTP client
// and git cloner.
func newFetchReader(
logger *zap.Logger,
storageosProvider storageos.Provider,
runner command.Runner,
moduleResolver bufmodule.ModuleResolver,
moduleReader bufmodule.ModuleReader,
) buffetch.Reader {
return buffetch.NewReader(
logger,
storageosProvider,
defaultHTTPClient,
defaultHTTPAuthenticator,
git.NewCloner(logger, storageosProvider, runner, defaultGitClonerOptions),
moduleResolver,
moduleReader,
)
}
// newFetchSourceReader creates a new buffetch.SourceReader with the default HTTP client
// and git cloner.
func newFetchSourceReader(
logger *zap.Logger,
storageosProvider storageos.Provider,
runner command.Runner,
) buffetch.SourceReader {
return buffetch.NewSourceReader(
logger,
storageosProvider,
defaultHTTPClient,
defaultHTTPAuthenticator,
git.NewCloner(logger, storageosProvider, runner, defaultGitClonerOptions),
)
}
// newFetchImageReader creates a new buffetch.ImageReader with the default HTTP client
// and git cloner.
func newFetchImageReader(
logger *zap.Logger,
storageosProvider storageos.Provider,
runner command.Runner,
) buffetch.ImageReader {
return buffetch.NewImageReader(
logger,
storageosProvider,
defaultHTTPClient,
defaultHTTPAuthenticator,
git.NewCloner(logger, storageosProvider, runner, defaultGitClonerOptions),
)
}
func checkExistingCacheDirs(baseCacheDirPath string, dirPaths ...string) error {
for _, dirPath := range dirPaths {
dirPath = normalpath.Unnormalize(dirPath)
// OK to use os.Stat instead of os.LStat here as this is CLI-only
fileInfo, err := os.Stat(dirPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if !fileInfo.IsDir() {
return fmt.Errorf("Expected %q to be a directory. This is used for buf's cache. The base cache directory %q can be overridden by setting the $BUF_CACHE_DIR environment variable.", dirPath, baseCacheDirPath)
}
if fileInfo.Mode().Perm()&0700 != 0700 {
return fmt.Errorf("Expected %q to be a writeable directory. This is used for buf's cache. The base cache directory %q can be overridden by setting the $BUF_CACHE_DIR environment variable.", dirPath, baseCacheDirPath)
}
}
return nil
}
func createCacheDirs(dirPaths ...string) error {
for _, dirPath := range dirPaths {
// os.MkdirAll does nothing if the directory already exists
if err := os.MkdirAll(normalpath.Unnormalize(dirPath), 0755); err != nil {
return err
}
}
return nil
}