-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6b53993
Showing
32 changed files
with
1,702 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"go/format" | ||
"html/template" | ||
"io/ioutil" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
//go:embed template/* | ||
var templateDir embed.FS | ||
|
||
type configNode struct { | ||
Name string | ||
KeyName string | ||
FieldName string | ||
StructName string | ||
Children map[string]*configNode | ||
} | ||
|
||
func printNode(n *configNode, indent string) { | ||
fmt.Println(indent, n.Name, ":", n.KeyName) | ||
if len(n.Children) > 0 { | ||
indent = strings.ReplaceAll(indent, "--", " ") | ||
indent += "|--" | ||
for _, v := range n.Children { | ||
printNode(v, indent) | ||
} | ||
} | ||
} | ||
|
||
func firstLower(s string) string { | ||
if len(s) == 0 { | ||
return s | ||
} | ||
|
||
return strings.ToLower(s[:1]) + s[1:] | ||
} | ||
|
||
func generate(dir string) error { | ||
root := &configNode{Name: "Root", Children: make(map[string]*configNode)} | ||
all := viper.AllKeys() | ||
sort.Strings(all) | ||
for _, k := range all { | ||
// skip imports key | ||
if k == "pp_imports" { | ||
continue | ||
} | ||
current := root | ||
ss := strings.Split(k, ".") | ||
for i, p := range ss { | ||
if current.Children[p] != nil { | ||
current = current.Children[p] | ||
} else { | ||
StructName := "" | ||
KeyName := "" | ||
FieldName := strings.Title(p) | ||
FieldName = strings.ReplaceAll(FieldName, "_", "") | ||
|
||
if i == len(ss)-1 { | ||
KeyName = strings.Join(ss[:i+1], ".") | ||
} else { | ||
v := strings.Join(ss[:i+1], " ") | ||
v = strings.Title(v) | ||
v = strings.ReplaceAll(v, " ", "") | ||
StructName = v | ||
StructName = strings.ReplaceAll(StructName, "_", "") | ||
} | ||
|
||
new := &configNode{ | ||
Name: strings.Title(p), | ||
FieldName: FieldName, | ||
StructName: firstLower(StructName), | ||
KeyName: KeyName, | ||
Children: make(map[string]*configNode), | ||
} | ||
current.Children[p] = new | ||
current = new | ||
} | ||
} | ||
} | ||
|
||
fmt.Println("") | ||
fmt.Println(">>>> CONFIG LIST <<<<") | ||
printNode(root, "|--") | ||
|
||
funcMap := template.FuncMap{ | ||
"ToTitle": strings.Title, | ||
} | ||
|
||
tmpl, err := template.New("config.tmpl").Funcs(funcMap).ParseFS(templateDir, "template/config.tmpl") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b := &bytes.Buffer{} | ||
err = tmpl.Execute(b, root) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
var buf []byte | ||
if buf, err = format.Source(b.Bytes()); err != nil { | ||
return err | ||
} | ||
|
||
if err = ioutil.WriteFile(dir+"/config.go", buf, 0644); err != nil { //nolint | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
func stringInSlice(a string, list []string) bool { | ||
for _, b := range list { | ||
if b == a { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func main() { | ||
configPath := os.Args[1] | ||
if configPath == "" { | ||
panic("empty config path") | ||
} | ||
dir := filepath.Dir(configPath) | ||
files, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
panic(err) | ||
} | ||
viper.AddConfigPath(dir) | ||
for _, file := range files { | ||
fileName := file.Name() | ||
ext := filepath.Ext(fileName) | ||
if len(ext) < 2 { | ||
continue | ||
} | ||
if !stringInSlice(ext[1:], viper.SupportedExts) { | ||
continue | ||
} | ||
|
||
viper.SetConfigFile(dir + "/" + fileName) | ||
err := viper.MergeInConfig() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
generate(dir) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{{ $currentNode := $.Name }} | ||
{{if eq $currentNode "Root"}} | ||
{{"//nolint"}} | ||
{{"package config"}} | ||
{{end}} | ||
|
||
{{range .Children}} | ||
{{if not .StructName}} | ||
{{if eq $currentNode "Root"}} | ||
const {{.FieldName}} = "{{.KeyName}}" | ||
{{end}} | ||
{{else}} | ||
type {{.StructName}} struct { | ||
{{range .Children}} | ||
{{.FieldName}} {{if .StructName}}{{.StructName}}{{else}}string{{end}} | ||
{{end}} | ||
} | ||
{{end}} | ||
|
||
{{if .StructName}} | ||
var {{.StructName | ToTitle}} = {{.StructName}}{ | ||
{{range .Children}} | ||
{{.FieldName}}: {{if .StructName}}{{.StructName | ToTitle}}{{else}}"{{.KeyName}}"{{end}}, | ||
{{end}} | ||
} | ||
{{end}} | ||
|
||
{{template "config.tmpl" .}} | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[build] | ||
noJSConfigInAssets = false | ||
useResourceCacheWhen = "fallback" | ||
writeStats = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[caches] | ||
[caches.assets] | ||
dir = ":resourceDir/_gen" | ||
maxAge = -1 | ||
[caches.getcsv] | ||
dir = ":cacheDir/:project" | ||
maxAge = -1 | ||
[caches.getjson] | ||
dir = ":cacheDir/:project" | ||
maxAge = -1 | ||
[caches.images] | ||
dir = ":resourceDir/_gen" | ||
maxAge = -1 | ||
[caches.modules] | ||
dir = ":cacheDir/modules" | ||
maxAge = -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
//nolint | ||
package config | ||
|
||
const Baseurl = "baseurl" | ||
|
||
type build struct { | ||
Nojsconfiginassets string | ||
|
||
Useresourcecachewhen string | ||
|
||
Writestats string | ||
} | ||
|
||
var Build = build{ | ||
|
||
Nojsconfiginassets: "build.nojsconfiginassets", | ||
|
||
Useresourcecachewhen: "build.useresourcecachewhen", | ||
|
||
Writestats: "build.writestats", | ||
} | ||
|
||
type caches struct { | ||
Assets cachesAssets | ||
|
||
Getcsv cachesGetcsv | ||
|
||
Getjson cachesGetjson | ||
|
||
Images cachesImages | ||
|
||
Modules cachesModules | ||
} | ||
|
||
var Caches = caches{ | ||
|
||
Assets: CachesAssets, | ||
|
||
Getcsv: CachesGetcsv, | ||
|
||
Getjson: CachesGetjson, | ||
|
||
Images: CachesImages, | ||
|
||
Modules: CachesModules, | ||
} | ||
|
||
type cachesAssets struct { | ||
Dir string | ||
|
||
Maxage string | ||
} | ||
|
||
var CachesAssets = cachesAssets{ | ||
|
||
Dir: "caches.assets.dir", | ||
|
||
Maxage: "caches.assets.maxage", | ||
} | ||
|
||
type cachesGetcsv struct { | ||
Dir string | ||
|
||
Maxage string | ||
} | ||
|
||
var CachesGetcsv = cachesGetcsv{ | ||
|
||
Dir: "caches.getcsv.dir", | ||
|
||
Maxage: "caches.getcsv.maxage", | ||
} | ||
|
||
type cachesGetjson struct { | ||
Dir string | ||
|
||
Maxage string | ||
} | ||
|
||
var CachesGetjson = cachesGetjson{ | ||
|
||
Dir: "caches.getjson.dir", | ||
|
||
Maxage: "caches.getjson.maxage", | ||
} | ||
|
||
type cachesImages struct { | ||
Dir string | ||
|
||
Maxage string | ||
} | ||
|
||
var CachesImages = cachesImages{ | ||
|
||
Dir: "caches.images.dir", | ||
|
||
Maxage: "caches.images.maxage", | ||
} | ||
|
||
type cachesModules struct { | ||
Dir string | ||
|
||
Maxage string | ||
} | ||
|
||
var CachesModules = cachesModules{ | ||
|
||
Dir: "caches.modules.dir", | ||
|
||
Maxage: "caches.modules.maxage", | ||
} | ||
|
||
const Footnotereturnlinkcontents = "footnotereturnlinkcontents" | ||
|
||
type params struct { | ||
Authorname string | ||
|
||
Debug string | ||
|
||
Githubuser string | ||
|
||
Listoffoo string | ||
|
||
Sidebarrecentlimit string | ||
|
||
Subtitle string | ||
} | ||
|
||
var Params = params{ | ||
|
||
Authorname: "params.authorname", | ||
|
||
Debug: "params.debug", | ||
|
||
Githubuser: "params.githubuser", | ||
|
||
Listoffoo: "params.listoffoo", | ||
|
||
Sidebarrecentlimit: "params.sidebarrecentlimit", | ||
|
||
Subtitle: "params.subtitle", | ||
} | ||
|
||
type permalinks struct { | ||
Posts string | ||
} | ||
|
||
var Permalinks = permalinks{ | ||
|
||
Posts: "permalinks.posts", | ||
} | ||
|
||
const Title = "title" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
baseURL = 'https://yoursite.example.com/' | ||
footnoteReturnLinkContents = '↩' | ||
title = 'My Hugo Site' | ||
[params] | ||
AuthorName = 'Jon Doe' | ||
GitHubUser = 'spf13' | ||
ListOfFoo = ['foo1', 'foo2'] | ||
SidebarRecentLimit = 5 | ||
Subtitle = 'Hugo is Absurdly Fast!' | ||
Debug = true | ||
[permalinks] | ||
posts = '/:year/:month/:title/' |
Oops, something went wrong.