feat: basic structure

This commit is contained in:
Noah Hsu
2022-06-06 21:48:53 +08:00
parent b76060570e
commit fced60c2b5
21 changed files with 520 additions and 4 deletions

30
pkg/utils/file.go Normal file
View File

@ -0,0 +1,30 @@
package utils
import (
log "github.com/sirupsen/logrus"
"os"
"path/filepath"
)
// Exists determine whether the file exists
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// CreatNestedFile create nested file
func CreatNestedFile(path string) (*os.File, error) {
basePath := filepath.Dir(path)
if !Exists(basePath) {
err := os.MkdirAll(basePath, 0700)
if err != nil {
log.Errorf("can't create foler%s", err)
return nil, err
}
}
return os.Create(path)
}

24
pkg/utils/json.go Normal file
View File

@ -0,0 +1,24 @@
package utils
import (
json "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"io/ioutil"
)
var Json = json.ConfigCompatibleWithStandardLibrary
// WriteToJson write struct to json file
func WriteToJson(src string, conf interface{}) bool {
data, err := Json.MarshalIndent(conf, "", " ")
if err != nil {
log.Errorf("failed convert Conf to []byte:%s", err.Error())
return false
}
err = ioutil.WriteFile(src, data, 0777)
if err != nil {
log.Errorf("failed to write json file:%s", err.Error())
return false
}
return true
}