mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 18:49:23 +08:00
init
This commit is contained in:
68
config/local.go
Normal file
68
config/local.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"os"
|
||||
)
|
||||
|
||||
type LocalConfig struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
Token string `json:"token"`
|
||||
Terminal bool `json:"terminal"`
|
||||
MaxRetries int `json:"maxRetries"`
|
||||
ReconnectInterval int `json:"reconnectInterval"`
|
||||
IgnoreUnsafeCert bool `json:"ignoreUnsafeCert"`
|
||||
}
|
||||
|
||||
func LoadConfig() (LocalConfig, error) {
|
||||
|
||||
var (
|
||||
endpoint string
|
||||
token string
|
||||
terminal bool
|
||||
path string
|
||||
maxRetries int
|
||||
reconnectInterval int
|
||||
ignoreUnsafeCert bool
|
||||
)
|
||||
|
||||
flag.StringVar(&endpoint, "e", "", "The endpoint URL")
|
||||
flag.StringVar(&token, "token", "", "The authentication token")
|
||||
flag.BoolVar(&terminal, "terminal", false, "Enable or disable terminal (default: false)")
|
||||
flag.StringVar(&path, "c", "agent.json", "Path to the configuration file")
|
||||
flag.IntVar(&maxRetries, "maxRetries", 10, "Maximum number of retries for WebSocket connection")
|
||||
flag.IntVar(&reconnectInterval, "reconnectInterval", 5, "Reconnect interval in seconds")
|
||||
flag.BoolVar(&ignoreUnsafeCert,"ignoreUnsafeCert", false, "Ignore unsafe certificate errors")
|
||||
flag.Parse()
|
||||
|
||||
// Ensure -c cannot coexist with other flags
|
||||
if path != "agent.json" && (endpoint != "" || token != "" || !terminal) {
|
||||
return LocalConfig{}, flag.ErrHelp
|
||||
}
|
||||
|
||||
// 必填项 Endpoint、Token 没有读取配置文件
|
||||
if endpoint == "" || token == "" {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return LocalConfig{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var localConfig LocalConfig
|
||||
if err := json.NewDecoder(file).Decode(&localConfig); err != nil {
|
||||
return LocalConfig{}, err
|
||||
}
|
||||
|
||||
return localConfig, nil
|
||||
}
|
||||
|
||||
return LocalConfig{
|
||||
Endpoint: endpoint,
|
||||
Token: token,
|
||||
Terminal: terminal,
|
||||
MaxRetries: maxRetries,
|
||||
ReconnectInterval: reconnectInterval,
|
||||
IgnoreUnsafeCert: ignoreUnsafeCert,
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user