arnaucube
arnaucube/konstruiPublic Self-hosted repository
konstrui/fileOperations.go
fileOperations.goRaw
  1package main
  2
  3import (
  4	"bufio"
  5	"encoding/json"
  6	"io/ioutil"
  7	"os"
  8	"strings"
  9)
 10
 11//dataEntry is the map used to create the array of maps, where the templatejson data is stored
 12type dataEntry map[string]string
 13
 14func readFile(folderPath string, filename string) string {
 15	dat, err := ioutil.ReadFile(folderPath + "/" + filename)
 16	check(err)
 17	return string(dat)
 18}
 19
 20func getDataFromJson(path string) []dataEntry {
 21	var entries []dataEntry
 22	file, err := ioutil.ReadFile(path)
 23	check(err)
 24	content := string(file)
 25	var rawEntries []*json.RawMessage
 26	json.Unmarshal([]byte(content), &rawEntries)
 27	for i := 0; i < len(rawEntries); i++ {
 28		rawEntryMarshaled, err := json.Marshal(rawEntries[i])
 29		check(err)
 30		var newDataEntry map[string]string
 31		json.Unmarshal(rawEntryMarshaled, &newDataEntry)
 32		entries = append(entries, newDataEntry)
 33	}
 34
 35	return entries
 36}
 37
 38func generateFromTemplateAndData(templateContent string, entries []dataEntry) string {
 39
 40	var newContent string
 41
 42	for i := 0; i < len(entries); i++ {
 43		var entryContent string
 44		entryContent = templateContent
 45		//first, get the map keys
 46		var keys []string
 47		for key, _ := range entries[i] {
 48			keys = append(keys, key)
 49		}
 50		//now, replace the keys with the values
 51		for j := 0; j < len(keys); j++ {
 52			entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
 53		}
 54
 55		newContent = newContent + entryContent
 56	}
 57	return newContent
 58}
 59func getTemplateParameters(line string) (string, string) {
 60	var templatePath string
 61	var data string
 62	line = strings.Replace(line, "<timmy-template ", "", -1)
 63	line = strings.Replace(line, "></timmy-template>", "", -1)
 64	attributes := strings.Split(line, " ")
 65	//fmt.Println(attributes)
 66	for i := 0; i < len(attributes); i++ {
 67		attSplitted := strings.Split(attributes[i], "=")
 68		if attSplitted[0] == "html" {
 69			templatePath = strings.Replace(attSplitted[1], `"`, "", -1)
 70		}
 71		if attSplitted[0] == "data" {
 72			data = strings.Replace(attSplitted[1], `"`, "", -1)
 73		}
 74	}
 75	return templatePath, data
 76}
 77
 78func useTemplate(templatePath string, dataPath string) string {
 79	templateContent := readFile(rawFolderPath, templatePath)
 80	entries := getDataFromJson(rawFolderPath + "/" + dataPath)
 81	generated := generateFromTemplateAndData(templateContent, entries)
 82	return generated
 83}
 84
 85func putTemplates(folderPath string, filename string) string {
 86	var fileContent string
 87	f, err := os.Open(folderPath + "/" + filename)
 88	check(err)
 89	scanner := bufio.NewScanner(f)
 90	lineCount := 1
 91	for scanner.Scan() {
 92		currentLine := scanner.Text()
 93		if strings.Contains(currentLine, "<timmy-template") && strings.Contains(currentLine, "</timmy-template>") {
 94			templatePath, data := getTemplateParameters(currentLine)
 95			fileContent = fileContent + useTemplate(templatePath, data)
 96		} else {
 97			fileContent = fileContent + currentLine
 98		}
 99		lineCount++
100	}
101	return fileContent
102}
103
104func writeFile(path string, newContent string) {
105	err := ioutil.WriteFile(path, []byte(newContent), 0644)
106	check(err)
107}