From afb873ed2bb5cffbdaf0d0a8743aa8a4008dc04f Mon Sep 17 00:00:00 2001 From: ehlxr Date: Thu, 21 Dec 2017 18:41:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=99=E7=82=B9=E6=9B=B4=E6=96=B0=EF=BC=9A20?= =?UTF-8?q?17-12-21=2018:41:34?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/tojson.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 common/tojson.go diff --git a/common/tojson.go b/common/tojson.go new file mode 100644 index 0000000..1cde39c --- /dev/null +++ b/common/tojson.go @@ -0,0 +1,47 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "os" +) + +func main() { + source := flag.String("source", "/Users/ehlxr/ehlxr/blog/Hexo/source/resume/index.md", "source file path") + dest := flag.String("dest", "/Users/ehlxr/ehlxr/blog/resume/data.json", "destination file path") + flag.Parse() + + f, err := os.Open(*source) + if err != nil { + panic(err) + } + defer f.Close() + fd, err := ioutil.ReadAll(f) + if err != nil { + panic(err) + } + + m := make(map[string]interface{}) + m["show"] = "1" + m["content"] = string(fd) + j, err := json.Marshal(m) + if err != nil { + panic(err) + } + + fmt.Println(string(j)) + writeFile(*dest, j) +} + +func writeFile(fn string, b []byte) { + file, err := os.OpenFile(fn, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777) + defer file.Close() + + if err != nil { + panic(err) + } + + file.Write(b) +}