config.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package config
  2. import (
  3. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  4. "github.com/rancher/os/util"
  5. )
  6. func Merge(bytes []byte) error {
  7. data, err := readConfigs(bytes, false, true)
  8. if err != nil {
  9. return err
  10. }
  11. existing, err := readConfigs(nil, false, true, CloudConfigFile)
  12. if err != nil {
  13. return err
  14. }
  15. return WriteToFile(util.Merge(existing, data), CloudConfigFile)
  16. }
  17. func Export(private, full bool) (string, error) {
  18. rawCfg := loadRawConfig("", full)
  19. if !private {
  20. rawCfg = filterPrivateKeys(rawCfg)
  21. }
  22. bytes, err := yaml.Marshal(rawCfg)
  23. return string(bytes), err
  24. }
  25. func Get(key string) (interface{}, error) {
  26. cfg := LoadConfig()
  27. data := map[interface{}]interface{}{}
  28. if err := util.ConvertIgnoreOmitEmpty(cfg, &data); err != nil {
  29. return nil, err
  30. }
  31. v, _ := getOrSetVal(key, data, nil)
  32. return v, nil
  33. }
  34. func GetCmdline(key string) interface{} {
  35. cmdline := readCmdline()
  36. v, _ := getOrSetVal(key, cmdline, nil)
  37. return v
  38. }
  39. func Set(key string, value interface{}) error {
  40. existing, err := readConfigs(nil, false, true, CloudConfigFile)
  41. if err != nil {
  42. return err
  43. }
  44. _, modified := getOrSetVal(key, existing, value)
  45. c := &CloudConfig{}
  46. if err = util.Convert(modified, c); err != nil {
  47. return err
  48. }
  49. return WriteToFile(modified, CloudConfigFile)
  50. }