github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/yconfig.go (about) 1 package protoc 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 8 yaml "gopkg.in/yaml.v3" 9 10 "github.com/bazelbuild/bazel-gazelle/config" 11 ) 12 13 // YConfig is used to configure a combined set of plugins, rules, and languages 14 // in a single YAML file. This is the format of the -proto_config flag. 15 type YConfig struct { 16 Plugin []*YPlugin `yaml:"plugins"` 17 Rule []*YRule `yaml:"rules"` 18 Language []*YLanguage `yaml:"languages"` 19 StarlarkPlugin []string `yaml:"starlarkPlugins"` 20 StarlarkRule []string `yaml:"starlarkRules"` 21 } 22 23 // YPlugin represents a LanguagePluginConfig in YAML. 24 type YPlugin struct { 25 Name string `yaml:"name"` 26 Implementation string `yaml:"implementation"` 27 Enabled *bool `yaml:"enabled,omitempty"` 28 Option []string `yaml:"options"` 29 Flag []string `yaml:"flags"` 30 Dep []string `yaml:"deps"` 31 Label string `yaml:"label"` 32 } 33 34 // YRule represents a LanguageRuleConfig in YAML. 35 type YRule struct { 36 Name string `yaml:"name"` 37 Implementation string `yaml:"implementation"` 38 Enabled *bool `yaml:"enabled,omitempty"` 39 Deps []string `yaml:"deps"` 40 Resolves []string `yaml:"resolves"` 41 Option []string `yaml:"options"` 42 Visibility []string `yaml:"visibility"` 43 } 44 45 // YLanguage represents a LanguageConfig in YAML. 46 type YLanguage struct { 47 Name string `yaml:"name"` 48 Implementation string `yaml:"implementation"` 49 Enabled *bool `yaml:"enabled,omitempty"` 50 Plugin []string `yaml:"plugins"` 51 Rule []string `yaml:"rules"` 52 } 53 54 // ParseYConfigFile parses the given filename and returns a YConfig pointer or 55 // error if a file read or parse error occurs. 56 func ParseYConfigFile(filename string) (*YConfig, error) { 57 data, err := ioutil.ReadFile(filename) 58 if err != nil { 59 return nil, fmt.Errorf("yaml read error %s: %w", filename, err) 60 } 61 var config YConfig 62 if err := yaml.Unmarshal(data, &config); err != nil { 63 return nil, fmt.Errorf("yaml parse error %s: %w", filename, err) 64 } 65 return &config, nil 66 } 67 68 func LoadYConfigFile(c *config.Config, cfg *PackageConfig, filename string) error { 69 if !filepath.IsAbs(filename) { 70 filename = filepath.Join(c.WorkDir, filename) 71 } 72 config, err := ParseYConfigFile(filename) 73 if err != nil { 74 return err 75 } 76 return cfg.LoadYConfig(config) 77 }