feat: read config from env (#104)

* feat: read config from env

* ci: skip some tests
This commit is contained in:
UUBulb
2024-11-30 11:40:48 +08:00
committed by GitHub
parent 48093faf98
commit ea6f119e72
9 changed files with 181 additions and 114 deletions
+5 -24
View File
@@ -21,7 +21,6 @@ import (
"github.com/blang/semver"
"github.com/ebi-yade/altsvc-go"
"github.com/hashicorp/go-uuid"
"github.com/nezhahq/go-github-selfupdate/selfupdate"
"github.com/nezhahq/service"
ping "github.com/prometheus-community/pro-bing"
@@ -151,28 +150,12 @@ func preRun(configPath string) error {
}
if err := agentConfig.Read(configPath); err != nil {
return fmt.Errorf("打开配置文件失败:%v", err)
return fmt.Errorf("init config failed: %v", err)
}
monitor.InitConfig(&agentConfig)
monitor.CustomEndpoints = agentConfig.CustomIPApi
if agentConfig.ClientSecret == "" {
return errors.New("ClientSecret 不能为空")
}
if agentConfig.ReportDelay < 1 || agentConfig.ReportDelay > 4 {
return errors.New("report-delay 的区间为 1-4")
}
if agentConfig.UUID == "" {
if uuid, err := uuid.GenerateUUID(); err == nil {
agentConfig.UUID = uuid
return agentConfig.Save()
} else {
return fmt.Errorf("生成 UUID 失败:%v", err)
}
}
return nil
}
@@ -181,14 +164,9 @@ func main() {
Usage: "哪吒监控 Agent",
Version: version,
Flags: []cli.Flag{
&cli.BoolFlag{Name: "version", Aliases: []string{"v"}, Usage: "查看当前版本号"},
&cli.StringFlag{Name: "config", Aliases: []string{"c"}, Usage: "配置文件路径"},
},
Action: func(c *cli.Context) error {
if c.Bool("version") {
fmt.Println(c.App.Version)
return nil
}
if path := c.String("config"); path != "" {
if err := preRun(path); err != nil {
return err
@@ -391,7 +369,10 @@ func runService(action string, path string) {
if action == "install" {
initName := s.Platform()
println("Init system is:", initName)
if err := agentConfig.Read(path); err != nil {
log.Fatalf("init config failed: %v", err)
}
printf("Init system is: %s", initName)
}
if len(action) != 0 {
+5
View File
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"net"
"os"
"reflect"
"testing"
)
@@ -27,6 +28,10 @@ func Test(t *testing.T) {
}
func TestLookupIP(t *testing.T) {
if ci := os.Getenv("CI"); ci != "" { // skip if test on CI
return
}
ip, err := lookupIP("www.google.com")
fmt.Printf("ip: %v, err: %v\n", ip, err)
if err != nil {