package conf import ( "context" "fmt" "github.com/joho/godotenv" "kpl/pkg/logx" "os" "strconv" ) var CONF = &conf{} type Serv00 struct { Host string Port int Username string Password string Cmd string } type conf struct { Proxy string Serv00s []Serv00 Serv00IntervalSec int HgUrls []string HgIntervalSec int } func Init(ctx context.Context) func(context.Context) { _ = godotenv.Load() index := 1 for { url := os.Getenv(fmt.Sprint("HG_URL", index)) if url == "" { break } CONF.HgUrls = append(CONF.HgUrls, url) index++ } index = 1 for { host := os.Getenv(fmt.Sprint("SERV00_HOST", index)) username := os.Getenv(fmt.Sprint("SERV00_USERNAME", index)) password := os.Getenv(fmt.Sprint("SERV00_PASSWORD", index)) cmd := os.Getenv(fmt.Sprint("SERV00_CMD", index)) if host == "" { break } if username == "" || password == "" || cmd == "" { continue } CONF.Serv00s = append(CONF.Serv00s, Serv00{ Host: host, Port: 22, Username: username, Password: password, Cmd: cmd, }) index++ } CONF.Proxy = os.Getenv("PROXY") hgIntervalSec := os.Getenv("HG_INTERVAL_SEC") if hgIntervalSec == "" { CONF.HgIntervalSec = 8 } else { parseInt, err := strconv.ParseInt(hgIntervalSec, 10, 64) if err != nil { CONF.HgIntervalSec = 8 } else { CONF.HgIntervalSec = int(parseInt) } } serv00IntervalSec := os.Getenv("SERV00_INTERVAL_SEC") if serv00IntervalSec == "" { CONF.Serv00IntervalSec = 300 } else { parseInt, err := strconv.ParseInt(serv00IntervalSec, 10, 64) if err != nil { CONF.Serv00IntervalSec = 300 } else { CONF.Serv00IntervalSec = int(parseInt) } } logx.WithContext(ctx).Infof("config loaded successfully") return func(ctx context.Context) { } }