Check in
This commit is contained in:
parent
ee8f29387c
commit
cd6bc78bf7
1 changed files with 59 additions and 0 deletions
59
main.go
Normal file
59
main.go
Normal 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...)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue