From 1d274a84e854994539bad6d73e4293a34b40f563 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Thu, 8 Sep 2022 16:58:08 +0800 Subject: [PATCH] feature(yaml): save yaml and open yaml file --- object/yaml.go | 34 ++++++++++++++++++++++++++++++++++ os/file.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 object/yaml.go create mode 100644 os/file.go diff --git a/object/yaml.go b/object/yaml.go new file mode 100644 index 0000000..a2b4515 --- /dev/null +++ b/object/yaml.go @@ -0,0 +1,34 @@ +package object + +import ( + "gopkg.in/yaml.v3" + "io/fs" + "io/ioutil" +) + +func SaveYMLFile(yamlObject interface{}, savePath string, perm fs.FileMode) (err error) { + + data, err := yaml.Marshal(&yamlObject) + if err != nil { + return err + } + + err = ioutil.WriteFile(savePath, data, perm) + if err != nil { + return err + } + + return err +} + +func OpenYMLFile(yamlFile string, yamlObject interface{}) (err error) { + + yamlFileData, err := ioutil.ReadFile(yamlFile) + if err != nil { + return err + } + + err = yaml.Unmarshal(yamlFileData, yamlObject) + + return err +} diff --git a/os/file.go b/os/file.go new file mode 100644 index 0000000..de58d17 --- /dev/null +++ b/os/file.go @@ -0,0 +1,36 @@ +package os + +import ( + "io" + "os" +) + +// https://www.golangprograms.com/files-directories-examples.html + +func CopyFile(src string, dst string) (err error) { + + fin, err := os.Open(src) + if err != nil { + return err + } + defer fin.Close() + + fOut, err := os.Create(dst) + if err != nil { + return err + } + defer fOut.Close() + + _, err = io.Copy(fOut, fin) + + if err != nil { + return err + } + + return err +} + +func MoveFile(src string, dst string) (err error) { + err = os.Rename(src, dst) + return err +}