summaryrefslogtreecommitdiff
path: root/www/codeberg-pages-server/files/config-generater.go
blob: 9aa9793db05aa7d42e496006c9cb82cb9fc94b3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main

import (
    "bufio"
    "fmt"
    "os"
    "regexp"
    "strings"
)

var (
    linePattern  = regexp.MustCompile(`^ *--([a-zA-Z0-9_-]+)\ value.*\[\$([A-Z0-9_]+)\]`)
    descRegexp   = regexp.MustCompile(`^.*value\s+(.*)(\(default.*\)|\[.*\]).*$`)
    defValRegexp = regexp.MustCompile(`\(default: ([^)]+)\)`)
)

func main() {
    fmt.Println("# See https://codeberg.org/Codeberg/pages-server")
    fmt.Println()

    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        line := scanner.Text()
        matches := linePattern.FindStringSubmatch(line)
        if matches == nil {
            continue
        }

        optionName := matches[2]

        descMatches := descRegexp.FindStringSubmatch(line)
        var description string
        if len(descMatches) > 1 {
            description = descMatches[1]
        }
        description = strings.TrimRight(description, " \t")
        if strings.HasPrefix(description, "]") {
            description = strings.TrimPrefix(description, "]")
            description = strings.TrimLeft(description, " ")
        }

        defValMatches := defValRegexp.FindStringSubmatch(line)
        var defaultValue string
        if len(defValMatches) > 1 {
            defaultValue = defValMatches[1]
        }

        exportCmd := "export"
        switch optionName {
        case "FORBIDDEN_MIME_TYPES",
            "ALLOWED_CORS_DOMAINS",
            "BLACKLISTED_PATHS",
            "CONFIG_FILE",
            "PROFILING_ADDRESS",
            "ACME_EAB_KID",
            "ACME_EAB_HMAC",
            "DNS_PROVIDER":
            optionName = "#" + optionName
            exportCmd = "#" + exportCmd
        }

        if description != "" {
            fmt.Printf("# %s\n", description)
        } else {
            fmt.Println("#")
        }

        if defaultValue != "" {
            fmt.Printf("%s=%s\n", optionName, defaultValue)
            fmt.Printf("%s %s\n", exportCmd, optionName)
        } else {
            fmt.Printf("%s=\"\"\n", optionName)
            uncommented := strings.TrimPrefix(optionName, "#")
            fmt.Printf("%s %s\n", exportCmd, uncommented)
        }
        fmt.Println()
    }

    if err := scanner.Err(); err != nil {
        fmt.Fprintf(os.Stderr, "read error: %v\n", err)
        os.Exit(1)
    }
}