go-utils/utils/resume/main.go

59 lines
1.1 KiB
Go
Raw Normal View History

2017-12-21 10:41:34 +00:00
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()
2017-12-22 08:01:46 +00:00
fmt.Printf("is these right? (n/no cancel)\n source file path: %s \n destination file path: %s\n", *source, *dest)
var in string
fmt.Scanln(&in)
if in == "no" || in == "n" {
fmt.Println("bye!")
os.Exit(0)
}
m := make(map[string]interface{})
m["show"] = "1"
m["content"] = string(readFile(*source))
j, err := json.Marshal(m)
2017-12-21 10:41:34 +00:00
if err != nil {
panic(err)
}
2017-12-22 08:01:46 +00:00
writeFile(*dest, j)
fmt.Println("Done !")
}
func readFile(path string) []byte {
f, err := os.Open(path)
2017-12-21 10:41:34 +00:00
if err != nil {
panic(err)
}
2017-12-22 08:01:46 +00:00
defer f.Close()
fd, err := ioutil.ReadAll(f)
2017-12-21 10:41:34 +00:00
if err != nil {
panic(err)
}
2017-12-22 08:01:46 +00:00
return fd
2017-12-21 10:41:34 +00:00
}
2017-12-22 08:01:46 +00:00
func writeFile(path string, b []byte) {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
2017-12-21 10:41:34 +00:00
defer file.Close()
if err != nil {
panic(err)
}
file.Write(b)
}