config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "encoding/json"
  16. "io/ioutil"
  17. )
  18. // Cfg defines the configuration options for kazel.
  19. type Cfg struct {
  20. GoPrefix string
  21. // evaluated recursively, defaults to ["."]
  22. SrcDirs []string
  23. // regexps that match packages to skip
  24. SkippedPaths []string
  25. // regexps that match packages to skip for k8s codegen.
  26. // note that this skips anything matched by SkippedPaths as well.
  27. SkippedK8sCodegenPaths []string
  28. // whether to add "pkg-srcs" and "all-srcs" filegroups
  29. // note that this operates on the entire tree (not just SrcsDirs) but skips anything matching SkippedPaths
  30. AddSourcesRules bool
  31. // whether to have multiple build files in vendor/ or just one.
  32. VendorMultipleBuildFiles bool
  33. // Whether to manage the upstream Go rules provided by bazelbuild/rules_go.
  34. // If using gazelle, set this to false (or omit).
  35. ManageGoRules bool
  36. // If defined, metadata parsed from "+k8s:" codegen build tags will be saved into this file.
  37. K8sCodegenBzlFile string
  38. // If defined, contains the boilerplate text to be included in the header of the generated bzl file.
  39. K8sCodegenBoilerplateFile string
  40. // Which tags to include in the codegen bzl file.
  41. // Include only the name of the tag.
  42. // For example, to include +k8s:foo=bar, list "foo" here.
  43. K8sCodegenTags []string
  44. }
  45. // ReadCfg reads and unmarshals the specified json file into a Cfg struct.
  46. func ReadCfg(cfgPath string) (*Cfg, error) {
  47. b, err := ioutil.ReadFile(cfgPath)
  48. if err != nil {
  49. return nil, err
  50. }
  51. var cfg Cfg
  52. if err := json.Unmarshal(b, &cfg); err != nil {
  53. return nil, err
  54. }
  55. defaultCfg(&cfg)
  56. return &cfg, nil
  57. }
  58. func defaultCfg(c *Cfg) {
  59. if len(c.SrcDirs) == 0 {
  60. c.SrcDirs = []string{"."}
  61. }
  62. }