konstrui/fileOperations.go
fileOperations.goRaw
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "os"
8 "strings"
9
10 "github.com/Jeffail/gabs"
11 "github.com/fatih/color"
12)
13
14//dataEntry is the map used to create the array of maps, where the templatejson data is stored
15type dataEntry map[string]string
16
17func copyDirRaw(fromDir string, currentDir string, newDir string) {
18 filesList, _ := ioutil.ReadDir("./" + fromDir + "/" + currentDir)
19 fmt.Println(fromDir + "/" + currentDir)
20 c.Green(newDir + "/" + currentDir)
21 os.MkdirAll(newDir+"/"+currentDir, os.ModePerm)
22 for _, f := range filesList {
23 fileNameSplitted := strings.Split(f.Name(), ".")
24 if len(fileNameSplitted) > 1 {
25 //is a file
26 copyFileRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
27 } else {
28 //is a directory
29 copyDirRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
30 }
31 }
32}
33
34func copyFileRaw(fromDir string, fName string, newDir string) {
35 c.Yellow("copying raw " + fromDir + "/" + fName)
36 fileContent := readFile(fromDir + "/" + fName)
37 writeFile(newDir+"/"+fName, fileContent)
38}
39
40func readFile(path string) string {
41 dat, err := ioutil.ReadFile(path)
42 check(err)
43 return string(dat)
44}
45
46func getDataFromJson(path string) ([]dataEntry, *gabs.Container) {
47 var entries []dataEntry
48 file, err := ioutil.ReadFile(path)
49 check(err)
50 content := string(file)
51 var rawEntries []*json.RawMessage
52 json.Unmarshal([]byte(content), &rawEntries)
53 for i := 0; i < len(rawEntries); i++ {
54 rawEntryMarshaled, err := json.Marshal(rawEntries[i])
55 check(err)
56 var newDataEntry map[string]string
57 json.Unmarshal(rawEntryMarshaled, &newDataEntry)
58 entries = append(entries, newDataEntry)
59 }
60
61 jsonData := jsonGabs(path)
62 return entries, jsonData
63}
64
65func jsonGabs(path string) *gabs.Container {
66 file, err := ioutil.ReadFile(path)
67 check(err)
68 jsonParsed, err := gabs.ParseJSON(file)
69 img := "img"
70 fmt.Println(jsonParsed.S(img))
71 return jsonParsed
72}
73
74func writeFile(path string, newContent string) {
75 err := ioutil.WriteFile(path, []byte(newContent), 0644)
76 check(err)
77
78 color.Green(path + ":")
79 color.Blue(newContent)
80}
81
82/*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
83 var entryContent string
84 entryContent = templateContent
85 //first, get the map keys
86 var keys []string
87 for key, _ := range entry {
88 keys = append(keys, key)
89 }
90 //now, replace the keys with the values
91 for j := 0; j < len(keys); j++ {
92 entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
93 }
94 return entryContent
95}*/
96func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry, *gabs.Container) {
97 templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
98 data, jsonData := getDataFromJson(rawFolderPath + "/" + page.Data)
99 return templateContent, data, jsonData
100}