This commit is contained in:
Hamcha 2019-05-09 23:38:59 +02:00
parent ee8f29387c
commit cd6bc78bf7
Signed by: hamcha
GPG key ID: A40413D21021EAEE

59
main.go Normal file
View file

@ -0,0 +1,59 @@
package main // import "git.fromouter.space/zygovh/caddy-runner"
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type config struct {
Backends map[string]struct {
Servers map[string]struct{ URL string }
}
Frontends map[string]struct {
EntryPoints []string
Backend string
Routes map[string]struct{ Rule string }
}
}
const PREFIX = "# +rule"
func main() {
dir := flag.String("dir", "/opt/caddy/conf", "Path to Caddy configuration files")
suffix := flag.String("cfgext", ".conf", "File extension for matching configuration files")
flag.Parse()
checkErr(filepath.Walk(*dir, func(path string, info os.FileInfo, err error) error {
if strings.ToLower(filepath.Ext(path)) != strings.ToLower(*suffix) {
return nil
}
// Read file
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// Split by linebreak
lines := strings.Split(string(data), "\n")
for _, line := range lines {
// Skip lines without marked comments
index := strings.Index(line, PREFIX)
if index < 0 {
return nil
}
rule := strings.TrimSpace(line[index+len(PREFIX):])
}
return nil
}), "error while scanning configuration files")
}
func checkErr(err error, errfmt string, args ...interface{}) {
if err != nil {
fmt.Fprintf(os.Stderr, "[FATAL ERROR] "+errfmt+":\n "+err.Error(), args...)
}
}