| 1 | /* |
| 2 | Copyright The containerd Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package mainscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/ |
| 18 | |
| 19 | import ( |
| 20 | "bytesscip-go gomod github.com/golang/go/src go1.13 bytes/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 22 | "ioscip-go gomod github.com/golang/go/src go1.13 io/" |
| 23 | "io/ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/" |
| 24 | "logscip-go gomod github.com/golang/go/src go1.13 log/" |
| 25 | "osscip-go gomod github.com/golang/go/src go1.13 os/" |
| 26 | "os/execscip-go gomod github.com/golang/go/src go1.13 `os/exec`/" |
| 27 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 28 | |
| 29 | "github.com/golang/protobuf/protoscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/proto`/" |
| 30 | "github.com/golang/protobuf/protoc-gen-go/descriptorscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/" |
| 31 | ) |
| 32 | |
| 33 | type descriptorSetscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet# struct { |
| 34 | mergedscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#merged. descriptorscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/.FileDescriptorSetscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/FileDescriptorSet# |
| 35 | seenscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#seen. map[string]struct{} |
| 36 | ignoreFilesscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#ignoreFiles. map[string]struct{} |
| 37 | descProtoscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#descProto. string |
| 38 | includeDirscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#includeDir. string |
| 39 | } |
| 40 | |
| 41 | func newDescriptorSetscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/newDescriptorSet().(ignoreFiles []string, d string, i string) *descriptorSetscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet# { |
| 42 | ifm := make(map[string]struct{}, len(ignoreFiles)) |
| 43 | for _, ignore := range ignoreFiles { |
| 44 | ifm[ignore] = struct{}{} |
| 45 | } |
| 46 | return &descriptorSetscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#{ |
| 47 | seenscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#seen.: make(map[string]struct{}), |
| 48 | ignoreFilesscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#ignoreFiles.: ifm, |
| 49 | descProtoscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#descProto.: d, |
| 50 | includeDirscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#includeDir.: i, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (d *descriptorSetscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#) addscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#add().(descs ...*descriptorscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/.FileDescriptorProtoscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/FileDescriptorProto#) { |
| 55 | for _, file := range descs { |
| 56 | name := file.GetNamescip-go gomod google.golang.org/protobuf v1.27.1 `google.golang.org/protobuf/types/descriptorpb`/FileDescriptorProto#GetName().() |
| 57 | if _, ok := d.seenscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#seen.[name]; ok { |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | if _, ok := d.ignoreFilesscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#ignoreFiles.[name]; ok { |
| 62 | continue |
| 63 | } |
| 64 | |
| 65 | // TODO(stevvooe): If we want to filter certain fields in the descriptor, |
| 66 | // this is the place to do it. May be necessary if certain fields are |
| 67 | // noisy, such as option fields. |
| 68 | d.mergedscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#merged..Filescip-go gomod google.golang.org/protobuf v1.27.1 `google.golang.org/protobuf/types/descriptorpb`/FileDescriptorSet#File. = append(d.mergedscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#merged..Filescip-go gomod google.golang.org/protobuf v1.27.1 `google.golang.org/protobuf/types/descriptorpb`/FileDescriptorSet#File., file) |
| 69 | d.seenscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#seen.[name] = struct{}{} |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // stabilize outputs the merged protobuf descriptor set into the provided writer. |
| 74 | // |
| 75 | // This is equivalent to the following command: |
| 76 | // |
| 77 | // cat merged.pb | protoc -I /path/to --decode google.protobuf.FileDescriptorSet /path/to/google/protobuf/descriptor.proto |
| 78 | func (d *descriptorSetscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#) marshalToscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#marshalTo().(w ioscip-go gomod github.com/golang/go/src go1.13 io/.Writerscip-go gomod github.com/golang/go/src go1.13 io/Writer#) error { |
| 79 | p, err := protoscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/proto`/.Marshalscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/proto`/Marshal().(&d.mergedscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#merged.) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | args := []string{ |
| 85 | "protoc", |
| 86 | "-I", |
| 87 | d.includeDirscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#includeDir., |
| 88 | "--decode", |
| 89 | "google.protobuf.FileDescriptorSet", |
| 90 | d.descProtoscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/descriptorSet#descProto., |
| 91 | } |
| 92 | |
| 93 | cmd := execscip-go gomod github.com/golang/go/src go1.13 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Command().(args[0], args[1:]...) |
| 94 | cmd.Stdinscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Cmd#Stdin. = bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.13 bytes/NewReader().(p) |
| 95 | cmd.Stdoutscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Cmd#Stdout. = w |
| 96 | cmd.Stderrscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Cmd#Stderr. = osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr. |
| 97 | |
| 98 | if !quietscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/quiet. { |
| 99 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.13 fmt/Println().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Joinscip-go gomod github.com/golang/go/src go1.13 strings/Join().(args, " ")) |
| 100 | } |
| 101 | return cmd.Runscip-go gomod github.com/golang/go/src go1.13 `os/exec`/Cmd#Run().() |
| 102 | } |
| 103 | |
| 104 | func readDescscip-go gomod github.com/containerd/protobuild . `github.com/containerd/protobuild`/readDesc().(path string) (*descriptorscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/.FileDescriptorSetscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/FileDescriptorSet#, error) { |
| 105 | var desc descriptorscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/.FileDescriptorSetscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/protoc-gen-go/descriptor`/FileDescriptorSet# |
| 106 | |
| 107 | p, err := ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/ReadFile().(path) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | if err := protoscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/proto`/.Unmarshalscip-go gomod github.com/golang/protobuf v1.5.0 `github.com/golang/protobuf/proto`/Unmarshal().(p, &desc); err != nil { |
| 113 | logscip-go gomod github.com/golang/go/src go1.13 log/.Fatallnscip-go gomod github.com/golang/go/src go1.13 log/Fatalln().(err) |
| 114 | } |
| 115 | |
| 116 | return &desc, nil |
| 117 | } |