Move hardcoded defaults to top of checkConfig()

This commit is contained in:
Amolith 2024-02-23 16:29:35 -05:00
parent 8cf4a4c284
commit 89e894401f
Signed by: Amolith
SSH Key Fingerprint: SHA256:JBKEeoO/72Fz03rtlzeO49PATFT2maMancH3opcT0h0
1 changed files with 23 additions and 17 deletions

View File

@ -10,7 +10,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"sync" "strconv"
"git.sr.ht/~amolith/willow/db" "git.sr.ht/~amolith/willow/db"
"git.sr.ht/~amolith/willow/project" "git.sr.ht/~amolith/willow/project"
@ -126,6 +126,20 @@ func main() {
} }
func checkConfig() error { func checkConfig() error {
defaultDBConn := "willow.sqlite"
defaultFetchInterval := 3600
defaultListen := "127.0.0.1:1313"
defaultConfig := fmt.Sprintf(`# Path to SQLite database
DBConn = "%s"
# How often to fetch new releases in seconds
## Minimum is %ds to avoid rate limits and unintentional abuse
FetchInterval = %d
[Server]
# Address to listen on
Listen = "%s"`, defaultDBConn, defaultFetchInterval, defaultFetchInterval, defaultListen)
file, err := os.Open(*flagConfig) file, err := os.Open(*flagConfig)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -135,15 +149,7 @@ func checkConfig() error {
} }
defer file.Close() defer file.Close()
_, err = file.WriteString(`# Path to SQLite database _, err = file.WriteString(defaultConfig)
DBConn = "willow.sqlite"
# How often to fetch new releases in seconds
FetchInterval = 3600
[Server]
# Address to listen on
Listen = "127.0.0.1:1313"
`)
if err != nil { if err != nil {
return err return err
} }
@ -162,19 +168,19 @@ Listen = "127.0.0.1:1313"
return err return err
} }
if config.FetchInterval < 10 { if config.FetchInterval < defaultFetchInterval {
fmt.Println("Fetch interval is set to", config.FetchInterval, "seconds, but the minimum is 10, using 10") fmt.Println("Fetch interval is set to", strconv.Itoa(config.FetchInterval), "seconds, but the minimum is", defaultFetchInterval, "seconds, using", strconv.Itoa(defaultFetchInterval)+"s")
config.FetchInterval = 10 config.FetchInterval = defaultFetchInterval
} }
if config.Server.Listen == "" { if config.Server.Listen == "" {
fmt.Println("No listen address specified, using 127.0.0.1:1313") fmt.Println("No listen address specified, using", defaultListen)
config.Server.Listen = "127.0.0.1:1313" config.Server.Listen = defaultListen
} }
if config.DBConn == "" { if config.DBConn == "" {
fmt.Println("No SQLite path specified, using \"willow.sqlite\"") fmt.Println("No SQLite path specified, using \"" + defaultDBConn + "\"")
config.DBConn = "willow.sqlite" config.DBConn = defaultDBConn
} }
return nil return nil