cbtrc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2015 Google Inc. All Rights Reserved.
  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 cbtrc encapsulates common code for reading .cbtrc files.
  14. package cbtrc
  15. import (
  16. "bufio"
  17. "bytes"
  18. "flag"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "path/filepath"
  23. "strings"
  24. )
  25. // Config represents a configuration.
  26. type Config struct {
  27. Project, Zone, Cluster string // required
  28. Creds string // optional
  29. }
  30. // RegisterFlags registers a set of standard flags for this config.
  31. // It should be called before flag.Parse.
  32. func (c *Config) RegisterFlags() {
  33. flag.StringVar(&c.Project, "project", c.Project, "project ID")
  34. flag.StringVar(&c.Zone, "zone", c.Zone, "CBT zone")
  35. flag.StringVar(&c.Cluster, "cluster", c.Cluster, "CBT cluster")
  36. flag.StringVar(&c.Creds, "creds", c.Creds, "if set, use application credentials in this file")
  37. }
  38. // CheckFlags checks that the required config values are set.
  39. func (c *Config) CheckFlags() error {
  40. var missing []string
  41. if c.Project == "" {
  42. missing = append(missing, "-project")
  43. }
  44. if c.Zone == "" {
  45. missing = append(missing, "-zone")
  46. }
  47. if c.Cluster == "" {
  48. missing = append(missing, "-cluster")
  49. }
  50. if len(missing) > 0 {
  51. return fmt.Errorf("Missing %s", strings.Join(missing, " and "))
  52. }
  53. return nil
  54. }
  55. // Filename returns the filename consulted for standard configuration.
  56. func Filename() string {
  57. // TODO(dsymonds): Might need tweaking for Windows.
  58. return filepath.Join(os.Getenv("HOME"), ".cbtrc")
  59. }
  60. // Load loads a .cbtrc file.
  61. // If the file is not present, an empty config is returned.
  62. func Load() (*Config, error) {
  63. filename := Filename()
  64. data, err := ioutil.ReadFile(filename)
  65. if err != nil {
  66. // silent fail if the file isn't there
  67. if os.IsNotExist(err) {
  68. return &Config{}, nil
  69. }
  70. return nil, fmt.Errorf("Reading %s: %v", filename, err)
  71. }
  72. c := new(Config)
  73. s := bufio.NewScanner(bytes.NewReader(data))
  74. for s.Scan() {
  75. line := s.Text()
  76. i := strings.Index(line, "=")
  77. if i < 0 {
  78. return nil, fmt.Errorf("Bad line in %s: %q", filename, line)
  79. }
  80. key, val := strings.TrimSpace(line[:i]), strings.TrimSpace(line[i+1:])
  81. switch key {
  82. default:
  83. return nil, fmt.Errorf("Unknown key in %s: %q", filename, key)
  84. case "project":
  85. c.Project = val
  86. case "zone":
  87. c.Zone = val
  88. case "cluster":
  89. c.Cluster = val
  90. case "creds":
  91. c.Creds = val
  92. }
  93. }
  94. return c, s.Err()
  95. }