aboutsummaryrefslogtreecommitdiff
path: root/cmd/prognosis/resolver.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-08-13 13:04:10 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-08-13 13:04:10 +0200
commite14a7db4ffe3c4e0f15f6b37a980501a8d74d26b (patch)
tree670ef0897839871a64d3a3bb2e17e242e7d6c385 /cmd/prognosis/resolver.go
downloadprognosis-e14a7db4ffe3c4e0f15f6b37a980501a8d74d26b.tar.gz
prognosis-e14a7db4ffe3c4e0f15f6b37a980501a8d74d26b.zip
Initial commit: prognosis, the Go implementation
An hour-by-hour forecast for the terminal, with official IMGW warnings for Polish locations. Replaces the Python version, whose cache file format it keeps so the two can coexist until this reaches parity. Open-Meteo provides the forecast, geocoding and pollen; GUGiK turns coordinates into a TERYT powiat code; IMGW supplies the warnings, filtered to that powiat rather than the whole country. Only the two lookups that never change are cached. Forecasts never are. Silence is never allowed to read as all-clear: "no warnings in force" and "the check failed" are reported as distinct states. Place names are resolved without guessing. A name matching several places is refused with a numbered list carrying each candidate's region and coordinates, and -pick N chooses one and remembers it. A stray positional beside -l is an error, so an unquoted "Wiry, PL" cannot silently resolve to somewhere else. The cache is written one entry per line with sorted keys, and treated as disposable but not worthless: an entry that will not parse is skipped and the rest kept, and a file that will not parse at all is moved to cache.json.bad rather than overwritten. No third-party dependencies. `make ci` is the gate: gofmt clean, vet, tests.
Diffstat (limited to 'cmd/prognosis/resolver.go')
-rw-r--r--cmd/prognosis/resolver.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/cmd/prognosis/resolver.go b/cmd/prognosis/resolver.go
new file mode 100644
index 0000000..095cc20
--- /dev/null
+++ b/cmd/prognosis/resolver.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "bufio"
+ "context"
+ "net"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+)
+
+// configureResolver teaches Go's resolver where the nameservers are on Android.
+//
+// Android has no /etc/resolv.conf. Go's pure-Go resolver falls back to
+// localhost, so every lookup fails with "dial tcp [::1]:53: connection
+// refused". Termux does ship one, at $PREFIX/etc/resolv.conf, which Go never
+// consults. Reading it here uses whatever nameservers are actually configured
+// rather than hardcoding any.
+//
+// A no-op everywhere /etc/resolv.conf exists, which is every other platform we
+// build for.
+func configureResolver() {
+ if _, err := os.Stat("/etc/resolv.conf"); err == nil {
+ return
+ }
+ prefix := os.Getenv("PREFIX")
+ if prefix == "" {
+ return
+ }
+ servers := nameservers(filepath.Join(prefix, "etc", "resolv.conf"))
+ if len(servers) == 0 {
+ return
+ }
+ net.DefaultResolver = &net.Resolver{
+ PreferGo: true,
+ Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
+ d := net.Dialer{Timeout: 5 * time.Second}
+ var err error
+ for _, s := range servers {
+ var conn net.Conn
+ conn, err = d.DialContext(ctx, network, net.JoinHostPort(s, "53"))
+ if err == nil {
+ return conn, nil
+ }
+ }
+ return nil, err
+ },
+ }
+}
+
+func nameservers(path string) []string {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil
+ }
+ defer f.Close()
+ var out []string
+ sc := bufio.NewScanner(f)
+ for sc.Scan() {
+ fields := strings.Fields(sc.Text())
+ if len(fields) >= 2 && fields[0] == "nameserver" {
+ out = append(out, fields[1])
+ }
+ }
+ return out
+}