Debian Codegraph @

config.go

raw symbols SCIP
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 main
18
19 import (
20 "fmt"
21 "io/ioutil"
22 "log"
23
24 "github.com/pelletier/go-toml"
25 )
26
27 type config struct {
28 Version string
29 Generators []string
30
31 // Generator is a code generator which is used from protoc.
32 // Deprecated: Use Generators instead.
33 Generator string
34
35 // Parameters are custom parameters to be passed to the generators.
36 // The parameter key must be the generator name with a table value
37 // of keys and string values to be passed.
38 // Example:
39 // [parameters.go-ttrpc]
40 // customkey = "somevalue"
41 Parameters map[string]map[string]string
42
43 // Plugins will be deprecated. It has to be per-Generator setting,
44 // but neither protoc-gen-go nor protoc-gen-go-grpc support plugins.
45 // So the refactoring is not worth to do.
46 Plugins []string
47
48 Includes struct {
49 Before []string
50 Vendored []string
51 Packages []string
52 After []string
53 }
54
55 Packages map[string]string
56
57 Overrides []struct {
58 Prefixes []string
59 // Generator is a code generator which is used from protoc.
60 // Deprecated: Use Generators instead.
61 Generator string
62 Generators []string
63 Parameters map[string]map[string]string
64 Plugins *[]string
65
66 // TODO(stevvooe): We could probably support overriding of includes and
67 // package maps, but they don't seem to be as useful. Likely,
68 // overriding the package map is more useful but includes happen
69 // project-wide.
70 }
71
72 Descriptors []struct {
73 Prefix string
74 Target string
75 IgnoreFiles []string `toml:"ignore_files"`
76 }
77 }
78
79 func newDefaultConfig() config {
80 return config{
81 Includes: struct {
82 Before []string
83 Vendored []string
84 Packages []string
85 After []string
86 }{
87 Before: []string{"."},
88 After: []string{"/usr/local/include", "/usr/include"},
89 },
90 }
91 }
92
93 func readConfig(path string) (config, error) {
94 p, err := ioutil.ReadFile(path)
95 if err != nil {
96 log.Fatalln(err)
97 }
98 return readConfigFrom(p)
99 }
100
101 func readConfigFrom(p []byte) (config, error) {
102 c := newDefaultConfig()
103 if err := toml.Unmarshal(p, &c); err != nil {
104 log.Fatalln(err)
105 }
106
107 if c.Generator != "" {
108 if len(c.Generators) > 0 {
109 return config{}, fmt.Errorf(
110 `specify either "generators = %v" or "generator = %v", not both`,
111 c.Generators, c.Generator,
112 )
113 }
114 c.Generators = []string{c.Generator}
115 c.Generator = ""
116 }
117
118 for i, o := range c.Overrides {
119 if o.Generator != "" {
120 if len(o.Generators) > 0 {
121 return config{}, fmt.Errorf(
122 `specify either "overrides[%d].generators" or "overrides[%d].generator", not both`,
123 i, i,
124 )
125 }
126 c.Overrides[i].Generators = []string{o.Generator}
127 c.Overrides[i].Generator = ""
128 }
129 }
130
131 if len(c.Generators) == 0 {
132 c.Generators = []string{"go"}
133 }
134
135 return c, nil
136 }