本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
962B

  1. package filesystem
  2. import (
  3. stdioutil "io/ioutil"
  4. "os"
  5. "gopkg.in/src-d/go-git.v4/config"
  6. "gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
  7. "gopkg.in/src-d/go-git.v4/utils/ioutil"
  8. )
  9. type ConfigStorage struct {
  10. dir *dotgit.DotGit
  11. }
  12. func (c *ConfigStorage) Config() (conf *config.Config, err error) {
  13. cfg := config.NewConfig()
  14. f, err := c.dir.Config()
  15. if err != nil {
  16. if os.IsNotExist(err) {
  17. return cfg, nil
  18. }
  19. return nil, err
  20. }
  21. defer ioutil.CheckClose(f, &err)
  22. b, err := stdioutil.ReadAll(f)
  23. if err != nil {
  24. return nil, err
  25. }
  26. if err = cfg.Unmarshal(b); err != nil {
  27. return nil, err
  28. }
  29. return cfg, err
  30. }
  31. func (c *ConfigStorage) SetConfig(cfg *config.Config) (err error) {
  32. if err = cfg.Validate(); err != nil {
  33. return err
  34. }
  35. f, err := c.dir.ConfigWriter()
  36. if err != nil {
  37. return err
  38. }
  39. defer ioutil.CheckClose(f, &err)
  40. b, err := cfg.Marshal()
  41. if err != nil {
  42. return err
  43. }
  44. _, err = f.Write(b)
  45. return err
  46. }
上海开阖软件有限公司 沪ICP备12045867号-1