github.com/scorpionis/hub@v2.2.1+incompatible/github/config_service.go (about)

     1  package github
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  )
     7  
     8  func newConfigService() *configService {
     9  	return &configService{
    10  		Encoder: &yamlConfigEncoder{},
    11  		Decoder: &yamlConfigDecoder{},
    12  	}
    13  }
    14  
    15  type configService struct {
    16  	Encoder configEncoder
    17  	Decoder configDecoder
    18  }
    19  
    20  func (s *configService) Save(filename string, c *Config) error {
    21  	err := os.MkdirAll(filepath.Dir(filename), 0771)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	w, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	defer w.Close()
    31  
    32  	return s.Encoder.Encode(w, c)
    33  }
    34  
    35  func (s *configService) Load(filename string, c *Config) error {
    36  	r, err := os.Open(filename)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	defer r.Close()
    41  
    42  	return s.Decoder.Decode(r, c)
    43  }