config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2020 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package config
  25. import (
  26. "context"
  27. "encoding/json"
  28. "io/ioutil"
  29. "os"
  30. "path/filepath"
  31. "github.com/pkg/errors"
  32. "github.com/docker/api/context/store"
  33. )
  34. type dirKey struct{}
  35. // WithDir sets the config directory path in the context
  36. func WithDir(ctx context.Context, path string) context.Context {
  37. return context.WithValue(ctx, dirKey{}, path)
  38. }
  39. // Dir returns the config directory path
  40. func Dir(ctx context.Context) string {
  41. cd, _ := ctx.Value(dirKey{}).(string)
  42. return cd
  43. }
  44. // LoadFile loads the docker configuration
  45. func LoadFile(dir string) (*File, error) {
  46. f := &File{}
  47. err := loadFile(configFilePath(dir), &f)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return f, nil
  52. }
  53. // WriteCurrentContext writes the selected current context to the Docker
  54. // configuration file. Note, the validity of the context is not checked.
  55. func WriteCurrentContext(dir string, name string) error {
  56. m := map[string]interface{}{}
  57. path := configFilePath(dir)
  58. err := loadFile(path, &m)
  59. if err != nil {
  60. return err
  61. }
  62. // Match existing CLI behavior
  63. if name == store.DefaultContextName {
  64. delete(m, currentContextKey)
  65. } else {
  66. m[currentContextKey] = name
  67. }
  68. return writeFile(path, m)
  69. }
  70. func writeFile(path string, content map[string]interface{}) error {
  71. d, err := json.MarshalIndent(content, "", "\t")
  72. if err != nil {
  73. return errors.Wrap(err, "unable to marshal config")
  74. }
  75. err = ioutil.WriteFile(path, d, 0644)
  76. return errors.Wrap(err, "unable to write config file")
  77. }
  78. func loadFile(path string, dest interface{}) error {
  79. data, err := ioutil.ReadFile(path)
  80. if err != nil {
  81. if os.IsNotExist(err) {
  82. // Not an error if there is no config, we're just using defaults
  83. return nil
  84. }
  85. return errors.Wrap(err, "unable to read config file")
  86. }
  87. err = json.Unmarshal(data, dest)
  88. return errors.Wrap(err, "unable to unmarshal config")
  89. }
  90. func configFilePath(dir string) string {
  91. return filepath.Join(dir, ConfigFileName)
  92. }
  93. // File contains the current context from the docker configuration file
  94. type File struct {
  95. CurrentContext string `json:"currentContext,omitempty"`
  96. }