代码拉取完成,页面将自动刷新
package main
import (
"io/ioutil"
"log"
"net/url"
"os"
)
type ConfigStore interface {
Get(key string) string
WatchToUpdate(config *Config, key string)
Pull(config *Config) error
Commit(config *Config, operation func() error) error
}
type FileStore struct {
path string
}
func NewFileStore(uri *url.URL) (ConfigStore, error) {
if _, err := os.Stat(uri.Path); os.IsNotExist(err) {
return nil, err
}
return &FileStore{
path: uri.Path,
}, nil
}
func (s *FileStore) Get(key string) string {
bytes, err := ioutil.ReadFile(key)
if err != nil {
log.Println("filestore:", err)
}
if len(bytes) > 0 {
return string(bytes)
}
return ""
}
func (s *FileStore) WatchToUpdate(config *Config, key string) {
// twiddle our thumbs. we're not going to watch file changes. yet?
}
func (s *FileStore) Pull(config *Config) error {
configData := s.Get(s.path)
if configData != "" {
err := config.Load([]byte(configData))
if err != nil {
log.Println("filestore: Invalid JSON from config store file", s.path)
return err
}
}
return nil
}
func (s *FileStore) Commit(config *Config, operation func() error) error {
if err := operation(); err != nil {
return err
}
if err := ioutil.WriteFile(s.path, config.Dump(), 0644); err != nil {
return err
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。