file_helper.go 716 字节
Newer Older
P
Phodal Huang 已提交
1
package support
P
Phodal HUANG 已提交
2 3 4

import (
	"fmt"
5
	"github.com/phodal/coca/config"
P
Phodal HUANG 已提交
6
	"io/ioutil"
P
Phodal HUANG 已提交
7 8 9
	"os"
)

P
Phodal Huang 已提交
10 11 12
var reporterPath = config.CocaConfig.ReporterPath

func WriteToCocaFile(fileName string, payload string) {
13 14 15 16 17 18 19 20
	if _, err := os.Stat(reporterPath); os.IsNotExist(err) {

		err := os.Mkdir(reporterPath, os.ModePerm)
		if err != nil {
			fmt.Println(err)
		}
	}
	_ = ioutil.WriteFile(reporterPath+"/"+fileName, []byte(payload), os.ModePerm)
P
Phodal HUANG 已提交
21 22
}

P
Phodal Huang 已提交
23 24 25 26
func ReadCocaFile(fileName string) []byte {
	return ReadFile(reporterPath + "/" + fileName)
}

P
Phodal HUANG 已提交
27 28 29 30 31 32
func ReadFile(fileName string) []byte {
	contents, err := ioutil.ReadFile(fileName)
	if err != nil {
		_ = fmt.Errorf("Failed removing original file: %s", err)
		return nil
	}
P
Phodal HUANG 已提交
33 34
	return contents
}