81 lines
1.9 KiB
Python
81 lines
1.9 KiB
Python
botcfg = '''package botcfg
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Config struct {
|
|
TGToken string
|
|
AllowedIDs []int64
|
|
WritePort int
|
|
ControlPort int
|
|
PreferIface string
|
|
}
|
|
|
|
func Load() *Config {
|
|
cfg := &Config{
|
|
WritePort: 8016,
|
|
ControlPort: 8017,
|
|
PreferIface: "WLAN",
|
|
}
|
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
srcDir := filepath.Dir(filename)
|
|
rootDir := filepath.Dir(filepath.Dir(filepath.Dir(srcDir)))
|
|
|
|
envPaths := []string{
|
|
filepath.Join(rootDir, "bot-go", ".env"),
|
|
filepath.Join(rootDir, ".env"),
|
|
}
|
|
var f *os.File
|
|
var err error
|
|
for _, envPath := range envPaths {
|
|
f, err = os.Open(envPath)
|
|
if err == nil { break }
|
|
}
|
|
if err != nil { return cfg }
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line == "" || strings.HasPrefix(line, "#") { continue }
|
|
parts := strings.SplitN(line, "=", 2)
|
|
if len(parts) != 2 { continue }
|
|
key := strings.TrimSpace(parts[0])
|
|
val := strings.TrimSpace(parts[1])
|
|
if os.Getenv(key) == "" { os.Setenv(key, val) }
|
|
}
|
|
|
|
cfg.TGToken = os.Getenv("TG_BOT_TOKEN")
|
|
allowedStr := os.Getenv("TG_ALLOWED_CHAT_IDS")
|
|
if allowedStr != "" {
|
|
for _, s := range strings.Split(allowedStr, ",") {
|
|
s = strings.TrimSpace(s)
|
|
if id, err := strconv.ParseInt(s, 10, 64); err == nil { cfg.AllowedIDs = append(cfg.AllowedIDs, id) }
|
|
}
|
|
}
|
|
if p := os.Getenv("WRITE_PORT"); p != "" {
|
|
if port, err := strconv.Atoi(p); err == nil { cfg.WritePort = port }
|
|
}
|
|
if p := os.Getenv("BOT_PORT"); p != "" {
|
|
if port, err := strconv.Atoi(p); err == nil { cfg.ControlPort = port }
|
|
}
|
|
if p := os.Getenv("PREFER_IFACE"); p != "" { cfg.PreferIface = p }
|
|
|
|
return cfg
|
|
}
|
|
'''
|
|
|
|
import os
|
|
dst = r"E:\GitHub\blog\bot-go\internal\botcfg"
|
|
os.makedirs(dst, exist_ok=True)
|
|
with open(os.path.join(dst, "config.go"), "w", encoding="utf-8") as f:
|
|
f.write(botcfg)
|
|
print("botcfg created")
|