Skip to content

Commit

Permalink
Add fish completion (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev authored Sep 21, 2020
1 parent 1e844cf commit d9a10b1
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 196 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.14

require (
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d
github.com/envoyproxy/protoc-gen-validate v0.4.1
github.com/gofrs/uuid v3.3.0+incompatible
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.2
Expand All @@ -20,9 +19,11 @@ require (
go.uber.org/multierr v1.6.0
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 // indirect
golang.org/x/tools v0.0.0-20200918232735-d647fc253266 // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/tools v0.0.0-20200921181913-ccabf82fa1ae // indirect
google.golang.org/genproto v0.0.0-20200921165018-b9da36f5f452 // indirect
google.golang.org/protobuf v1.25.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
Expand Down
16 changes: 2 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.4.1 h1:7dLaJvASGRD7X49jSCSXXHwKPm0ZN9r9kJD+p+vS7dM=
github.com/envoyproxy/protoc-gen-validate v0.4.1/go.mod h1:E+IEazqdaWv3FrnGtZIu3b9fPFMK8AzeTTrk9SfVwWs=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -120,8 +118,6 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7 h1:ux/56T2xqZO/3cP1I2F86qpeoYPCOzk+KF/UH/Ar+lk=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jhump/protoreflect v1.7.1-0.20200723220026-11eaaf73e0ec h1:LeWD9kGWul9js18sWzNP2SjMuO6smUEpXt8OmRs7qU0=
Expand All @@ -138,15 +134,12 @@ github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYs
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lyft/protoc-gen-star v0.5.1 h1:sImehRT+p7lW9n6R7MQc5hVgzWGEkDVZU4AsBQ4Isu8=
github.com/lyft/protoc-gen-star v0.5.1/go.mod h1:9toiA3cC7z5uVbODF7kEQ91Xn7XNFkVUl+SrEe+ZORU=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
Expand All @@ -172,7 +165,6 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.5.0 h1:042Buzk+NhDI+DeSAA62RwJL8VAuZUMQZUjCsRz1Mug=
github.com/pkg/profile v1.5.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand All @@ -198,9 +190,6 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.3.4 h1:8q6vk3hthlpb2SouZcnBVKboxWQWMDNF38bwholZrJc=
github.com/spf13/afero v1.3.4/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.1-0.20200713175500-884edc58ad08 h1:hhEmB0BHio4CpWUm9oKszEvw2y/QFjO6Y67vlCOY5QA=
github.com/spf13/cobra v1.0.1-0.20200713175500-884edc58ad08/go.mod h1:yk5b0mALVusDL5fMM6Rd1wgnoO5jUPhwsQ6LQAJTidQ=
Expand Down Expand Up @@ -248,7 +237,6 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down Expand Up @@ -357,8 +345,8 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6 h1:nULzSsKgihxFGLnQFv2T7lE5vIhOtg8ZPpJHapEt7o0=
golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200918232735-d647fc253266 h1:k7tVuG0g1JwmD3Jh8oAl1vQ1C3jb4Hi/dUl1wWDBJpQ=
golang.org/x/tools v0.0.0-20200918232735-d647fc253266/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20200921181913-ccabf82fa1ae h1:iNsgnQMzc+DzfYCTsDkDcDnHNaOO89eTgnTb/ldeQKs=
golang.org/x/tools v0.0.0-20200921181913-ccabf82fa1ae/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
2 changes: 1 addition & 1 deletion internal/buf/bufcore/bufmodule/bufmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func ModuleToProtoModule(ctx context.Context, module Module) (*modulev1.Module,
Files: protoModuleFiles,
Dependencies: protoModuleNames,
}
if err := validateProtoModule(protoModule); err != nil {
if err := ValidateProtoModule(protoModule); err != nil {
return nil, err
}
return protoModule, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/buf/bufcore/bufmodule/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type module struct {
}

func newModuleForProto(ctx context.Context, protoModule *modulev1.Module) (*module, error) {
if err := validateProtoModule(protoModule); err != nil {
if err := ValidateProtoModule(protoModule); err != nil {
return nil, err
}
pathToData := make(map[string][]byte)
Expand Down
4 changes: 2 additions & 2 deletions internal/buf/bufcore/bufmodule/module_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newModuleName(
func newModuleNameForProto(
protoModuleName *modulev1.ModuleName,
) (*moduleName, error) {
if err := validateProtoModuleName(protoModuleName); err != nil {
if err := ValidateProtoModuleName(protoModuleName); err != nil {
return nil, err
}
return &moduleName{
Expand Down Expand Up @@ -105,7 +105,7 @@ func newProtoModuleNameForModuleName(
Version: moduleName.Version(),
Digest: moduleName.Digest(),
}
if err := validateProtoModuleName(protoModuleName); err != nil {
if err := ValidateProtoModuleName(protoModuleName); err != nil {
return nil, err
}
return protoModuleName, nil
Expand Down
155 changes: 120 additions & 35 deletions internal/buf/bufcore/bufmodule/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,56 @@ import (
"github.com/bufbuild/buf/internal/pkg/normalpath"
)

// 32MB
const maxModuleTotalContentLength = 32 << 20
const (
// 32MB
maxModuleTotalContentLength = 32 << 20
ownerNameMinLength = 3
ownerNameMaxLength = 64
protoFileMaxCount = 16384
remoteMinLength = 1
remoteMaxLength = 256
repositoryNameMinLength = 2
repositoryNameMaxLength = 64
repositoryVersionNameMinLength = 2
repositoryVersionNameMaxLength = 32
)

// ValidateDigest verifies the given digest's prefix,
// decodes its base64 representation and checks the
// length of the encoded bytes.
func ValidateDigest(digest string) error {
if digest == "" {
return errors.New("empty digest")
}
split := strings.SplitN(digest, "-", 2)
if len(split) != 2 {
return fmt.Errorf("invalid digest: %s", digest)
}
digestPrefix := split[0]
digestValue := split[1]
if digestPrefix != b1DigestPrefix {
return fmt.Errorf("unknown digest prefix: %s", digestPrefix)
}
decoded, err := base64.URLEncoding.DecodeString(digestValue)
if err != nil {
return fmt.Errorf("failed to decode digest %s: %v", digestValue, err)
}
if len(decoded) != 32 {
return fmt.Errorf("invalid sha256 hash, expected 32 bytes: %s", digestValue)
}
return nil
}

func validateProtoModule(protoModule *modulev1.Module) error {
// ValidateProtoModule verifies the given module is well-formed.
func ValidateProtoModule(protoModule *modulev1.Module) error {
if protoModule == nil {
return errors.New("nil Module")
return errors.New("module is required")
}
if len(protoModule.Files) == 0 {
return errors.New("Module had no files")
return errors.New("module has no files")
}
if err := protoModule.Validate(); err != nil {
return err
if len(protoModule.Files) > protoFileMaxCount {
return fmt.Errorf("module can contain at most %d files", protoFileMaxCount)
}
totalContentLength := 0
filePathMap := make(map[string]struct{}, len(protoModule.Files))
Expand All @@ -55,21 +93,89 @@ func validateProtoModule(protoModule *modulev1.Module) error {
return nil
}

func validateProtoModuleName(protoModuleName *modulev1.ModuleName) error {
// ValidateProtoModuleName verifies the given module name is well-formed.
func ValidateProtoModuleName(protoModuleName *modulev1.ModuleName) error {
if protoModuleName == nil {
return errors.New("nil ModuleName")
return errors.New("module name is required")
}
if err := validateRemote(protoModuleName.Remote); err != nil {
return err
}
if err := ValidateOwnerName(protoModuleName.Owner, "owner"); err != nil {
return err
}
if err := protoModuleName.Validate(); err != nil {
if err := ValidateRepositoryName(protoModuleName.Repository); err != nil {
return err
}
if err := ValidateRepositoryVersionName(protoModuleName.Version); err != nil {
return err
}
if protoModuleName.Digest != "" {
if err := validateDigest(protoModuleName.Digest); err != nil {
if err := ValidateDigest(protoModuleName.Digest); err != nil {
return err
}
}
return nil
}

// ValidateOwnerName is used to validate owner names, i.e. usernames and organization names.
func ValidateOwnerName(ownerName string, ownerType string) error {
if ownerName == "" {
return fmt.Errorf("%s name is required", ownerType)
}
if len(ownerName) < ownerNameMinLength || len(ownerName) > ownerNameMaxLength {
return fmt.Errorf("%s name %q must be between at least %d and at most %d characters", ownerType, ownerName, ownerNameMinLength, ownerNameMaxLength)
}
for _, char := range ownerName {
if !isLowerAlphanumeric(char) && char != '-' {
return fmt.Errorf("%s name %q must only contain lowercase letters, digits, or hyphens (-)", ownerType, ownerName)
}
}
return nil
}

// ValidateRepositoryName verifies the given repository name is well-formed.
func ValidateRepositoryName(repositoryName string) error {
if repositoryName == "" {
return errors.New("repository name is required")
}
if len(repositoryName) < repositoryNameMinLength || len(repositoryName) > repositoryNameMaxLength {
return fmt.Errorf("repository name must be at least %d and at most %d characters", repositoryNameMinLength, repositoryNameMaxLength)
}
for _, char := range repositoryName {
if !isLowerAlphanumeric(char) && char != '-' {
return fmt.Errorf("repository name %q must only contain lowercase letters, digits, or hyphens (-)", repositoryName)
}
}
return nil
}

// ValidateRepositoryVersionName verifies the given repository version name is well-formed.
func ValidateRepositoryVersionName(versionName string) error {
if versionName == "" {
return errors.New("repository version name is required")
}
if len(versionName) < repositoryVersionNameMinLength || len(versionName) > repositoryVersionNameMaxLength {
return fmt.Errorf("repository version name %q must be at least %d and at most %d characters", versionName, repositoryVersionNameMinLength, repositoryVersionNameMaxLength)
}
for _, char := range versionName {
if !isLowerAlphanumeric(char) && char != '-' && char != '.' {
return fmt.Errorf("repository version name %q must only contain lowercase letters, digits, periods (.), or hyphens (-)", versionName)
}
}
return nil
}

func validateRemote(remote string) error {
if remote == "" {
return errors.New("remote is required")
}
if len(remote) < remoteMinLength || len(remote) > remoteMaxLength {
return fmt.Errorf("remote %q must be at least %d and at most %d characters", remote, remoteMinLength, remoteMaxLength)
}
return nil
}

func validateModuleFilePaths(paths []string) error {
if len(paths) == 0 {
return nil
Expand Down Expand Up @@ -108,28 +214,7 @@ func validateModuleFilePathWithoutNormalization(path string) error {
return nil
}

// validateDigest verifies the given digest's prefix,
// decodes its base64 representation and checks the
// length of the encoded bytes.
func validateDigest(digest string) error {
if digest == "" {
return errors.New("empty digest")
}
split := strings.SplitN(digest, "-", 2)
if len(split) != 2 {
return fmt.Errorf("invalid digest: %s", digest)
}
digestPrefix := split[0]
digestValue := split[1]
if digestPrefix != b1DigestPrefix {
return fmt.Errorf("unknown digest prefix: %s", digestPrefix)
}
decoded, err := base64.URLEncoding.DecodeString(digestValue)
if err != nil {
return fmt.Errorf("failed to decode digest %s: %v", digestValue, err)
}
if len(decoded) != 32 {
return fmt.Errorf("invalid sha256 hash, expected 32 bytes: %s", digestValue)
}
return nil
// islowerAlphanumeric returns true for [0-9a-z].
func isLowerAlphanumeric(r rune) bool {
return ('0' <= r && r <= '9') || ('a' <= r && r <= 'z')
}
Loading

0 comments on commit d9a10b1

Please sign in to comment.