2017-12-21 10:41:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-03-28 03:16:04 +00:00
|
|
|
|
|
|
|
"github.com/manifoldco/promptui"
|
2017-12-21 10:41:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2018-03-28 03:16:04 +00:00
|
|
|
// 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()
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
// }
|
|
|
|
|
|
|
|
prompt := promptui.SelectWithAdd{
|
|
|
|
Label: "What's your source file path",
|
|
|
|
Items: []string{"/Users/ehlxr/ehlxr/blog/Hexo/source/resume/index.md"},
|
|
|
|
AddLabel: "Other",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, source, err := prompt.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Prompt failed %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt = promptui.SelectWithAdd{
|
|
|
|
Label: "What's your destination file path",
|
|
|
|
Items: []string{"/Users/ehlxr/ehlxr/blog/resume/data.json"},
|
|
|
|
AddLabel: "Other",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, dest, err := prompt.Run()
|
2017-12-21 10:41:34 +00:00
|
|
|
|
2018-03-28 03:16:04 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Prompt failed %v\n", err)
|
|
|
|
return
|
2017-12-22 08:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["show"] = "1"
|
2018-03-28 03:16:04 +00:00
|
|
|
m["content"] = string(readFile(source))
|
2017-12-22 08:01:46 +00:00
|
|
|
j, err := json.Marshal(m)
|
2017-12-21 10:41:34 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-28 03:16:04 +00:00
|
|
|
writeFile(dest, j)
|
2017-12-22 08:01:46 +00:00
|
|
|
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)
|
|
|
|
}
|