updates
This commit is contained in:
@@ -4,11 +4,18 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/page"
|
||||
"github.com/hacdias/caddy-hugo/utils"
|
||||
"github.com/spf13/hugo/parser"
|
||||
)
|
||||
|
||||
type settings struct {
|
||||
Settings interface{}
|
||||
Keys []string
|
||||
}
|
||||
|
||||
// Execute the page
|
||||
func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if r.Method == "POST" {
|
||||
@@ -27,9 +34,14 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
return 500, err
|
||||
}
|
||||
|
||||
// configIndex := getConfigNames(config)
|
||||
|
||||
cnf := new(settings)
|
||||
cnf.Settings = getConfigNames(config, "")
|
||||
|
||||
page := new(page.Page)
|
||||
page.Title = "settings"
|
||||
page.Body = config
|
||||
page.Title = "Settings"
|
||||
page.Body = cnf
|
||||
return page.Render("settings", w)
|
||||
}
|
||||
|
||||
@@ -54,21 +66,6 @@ func getConfigFrontMatter() string {
|
||||
return frontmatter
|
||||
}
|
||||
|
||||
func getConfig(frontmatter string) (interface{}, error) {
|
||||
content := getConfigFileContent(frontmatter)
|
||||
|
||||
switch frontmatter {
|
||||
case "yaml":
|
||||
return parser.HandleYAMLMetaData(content)
|
||||
case "json":
|
||||
return parser.HandleJSONMetaData(content)
|
||||
case "toml":
|
||||
return parser.HandleTOMLMetaData(content)
|
||||
}
|
||||
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
func getConfigFileContent(frontmatter string) []byte {
|
||||
file, err := ioutil.ReadFile("config." + frontmatter)
|
||||
|
||||
@@ -79,3 +76,66 @@ func getConfigFileContent(frontmatter string) []byte {
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
// make it generic to frontmatter. everything bellow -> new file
|
||||
func getConfig(frontmatter string) (interface{}, error) {
|
||||
content := getConfigFileContent(frontmatter)
|
||||
// config := []string{}
|
||||
|
||||
// get the config into a map
|
||||
if frontmatter == "yaml" {
|
||||
return parser.HandleYAMLMetaData(content)
|
||||
} else if frontmatter == "json" {
|
||||
return parser.HandleJSONMetaData(content)
|
||||
} else if frontmatter == "toml" {
|
||||
return parser.HandleTOMLMetaData(content)
|
||||
}
|
||||
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
type conf struct {
|
||||
Name string
|
||||
Master string
|
||||
Content interface{}
|
||||
SubContent bool
|
||||
}
|
||||
|
||||
func getConfigNames(config interface{}, master string) interface{} {
|
||||
var mapsNames []string
|
||||
var stringsNames []string
|
||||
|
||||
for index, element := range config.(map[string]interface{}) {
|
||||
if utils.IsMap(element) {
|
||||
mapsNames = append(mapsNames, index)
|
||||
} else {
|
||||
stringsNames = append(stringsNames, index)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(mapsNames)
|
||||
sort.Strings(stringsNames)
|
||||
names := append(stringsNames, mapsNames...)
|
||||
|
||||
settings := make([]interface{}, len(names))
|
||||
|
||||
for index := range names {
|
||||
c := new(conf)
|
||||
c.Name = names[index]
|
||||
c.Master = master
|
||||
c.SubContent = false
|
||||
|
||||
i := config.(map[string]interface{})[names[index]]
|
||||
|
||||
if utils.IsMap(i) {
|
||||
c.Content = getConfigNames(i, c.Name)
|
||||
c.SubContent = true
|
||||
} else {
|
||||
c.Content = i
|
||||
}
|
||||
|
||||
settings[index] = c
|
||||
}
|
||||
|
||||
return settings
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user