file_rw_helper.go 792 字节
Newer Older
P
Phodal Huang 已提交
1
package cmd_util
P
Phodal HUANG 已提交
2 3 4

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

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

func WriteToCocaFile(fileName string, payload string) {
14
	if _, err := os.Stat(reporterPath); os.IsNotExist(err) {
P
Phodal Huang 已提交
15 16 17
		mkdirErr := os.Mkdir(reporterPath, os.ModePerm)
		if mkdirErr != nil {
			fmt.Println(mkdirErr)
18 19
		}
	}
P
Phodal Huang 已提交
20
	_ = ioutil.WriteFile(filepath.FromSlash(reporterPath+"/"+fileName), []byte(payload), os.ModePerm)
P
Phodal HUANG 已提交
21 22
}

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

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
}