config.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 := loadRawDiskConfig(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 Set(key string, value interface{}) error {
  35. existing, err := readConfigs(nil, false, true, CloudConfigFile)
  36. if err != nil {
  37. return err
  38. }
  39. _, modified := getOrSetVal(key, existing, value)
  40. return WriteToFile(modified, CloudConfigFile)
  41. }