Files
ProjectOctopus/agent-go/config/nacosConfig.go
2023-03-20 15:55:27 +08:00

84 lines
1.8 KiB
Go

package config
import (
"agent-go/g"
"encoding/json"
"fmt"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"io/ioutil"
)
type OctopusAgentNacosConfig struct {
Spring Spring `json:"spring"`
Server Server `json:"server"`
}
type Server struct {
Port int64 `json:"port"`
}
type Spring struct {
Application Application `json:"application"`
Profiles Profiles `json:"profiles"`
Cloud Cloud `json:"cloud"`
}
type Application struct {
Name string `json:"name"`
}
type Cloud struct {
Nacos Nacos `json:"nacos"`
}
type Nacos struct {
Config Config `json:"config"`
}
type Config struct {
Group string `json:"group"`
ConfigRetryTime int64 `json:"config-retry-time"`
FileExtension string `json:"file-extension"`
MaxRetry int64 `json:"max-retry"`
ServerAddr string `json:"server-addr"`
Timeout int64 `json:"timeout"`
ConfigLongPollTimeout int64 `json:"config-long-poll-timeout"`
ExtensionConfigs []ExtensionConfig `json:"extension-configs"`
}
type ExtensionConfig struct {
Group string `json:"group"`
DataID string `json:"data-id"`
}
type Profiles struct {
Active string `json:"active"`
}
var log = g.G.LOG
func InitNacos(configFileName string) {
data, err := ioutil.ReadFile(configFileName)
if err != nil {
log.Fatal("error reading YAML file: ", zap.Error(err))
}
var config OctopusAgentNacosConfig
if err := yaml.Unmarshal(data, &config); err != nil {
log.Fatal("error parsing YAML data: ", zap.Error(err))
}
// 将结构体转换为 JSON 字符串
jsonData, err := json.Marshal(config)
if err != nil {
fmt.Println("error:", err)
return
}
// 输出 JSON 字符串
log.Info(string(jsonData))
}