示例#1
0
文件: util.go 项目: johnnylee/goutil
// Load into v the json file at the give path. The path elements, `pathElem`
// are expanded by `fileutil.ExpandPath`.
func Load(v interface{}, pathElem ...string) error {
	path := fileutil.ExpandPath(pathElem...)

	buf, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}

	return json.Unmarshal(buf, &v)
}
示例#2
0
文件: util.go 项目: johnnylee/goutil
// Store the data in v into a give file path. The path elements `pathElem` are
// expanded by `fileutil.ExpandPath`.
func Store(v interface{}, pathElem ...string) error {
	path := fileutil.ExpandPath(pathElem...)

	buf, err := json.Marshal(v)
	if err != nil {
		return err
	}

	var out bytes.Buffer
	if err = json.Indent(&out, buf, "", "\t"); err != nil {
		return err
	}

	return ioutil.WriteFile(path, out.Bytes(), 0600)
}
示例#3
0
文件: util.go 项目: johnnylee/goutil
func SetFilePath(pathElem ...string) (err error) {
	path := fileutil.ExpandPath(pathElem...)

	// Make sure the directory exists.
	logDir := filepath.Dir(path)
	if err = os.MkdirAll(logDir, 0777); err != nil {
		return err
	}

	// Open the log file.
	f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		return err
	}

	// Set the output.
	log.SetOutput(f)

	return nil
}