Skip to content

Commit

Permalink
增加config-sync实时同步功能
Browse files Browse the repository at this point in the history
  • Loading branch information
typ431127 committed Mar 14, 2024
2 parents bf328de + 387a985 commit 495b853
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 81 deletions.
File renamed without changes.
29 changes: 1 addition & 28 deletions internal/cmd/fileconfig.go → internal/cmd/fileload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
"io"
"nacos-check/internal/nacos"
"nacos-check/pkg"
Expand All @@ -13,33 +12,7 @@ import (
"path/filepath"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "查看版本",
Run: func(cmd *cobra.Command, args []string) {
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
fmt.Println("Version: 0.7.3")
},
}

var configCmd = &cobra.Command{
Use: "config",
Short: "查看本地配置文件路径",
Run: func(cmd *cobra.Command, args []string) {
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
configfile := GetConfigFilePath()
fmt.Println("本地配置文件路径:", configfile)
},
}

func init() {
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(versionCmd)
}

func GetConfigFilePath() string {
func getConfigFilePath() string {
homedir, err := pkg.HomeDir()
if err != nil {
fmt.Println("获取系统家目录获取异常")
Expand Down
32 changes: 32 additions & 0 deletions internal/cmd/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "查看版本",
Run: func(cmd *cobra.Command, args []string) {
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
fmt.Println("Version: 0.7.3")
},
}

var configCmd = &cobra.Command{
Use: "config",
Short: "查看本地配置文件路径",
Run: func(cmd *cobra.Command, args []string) {
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
configfile := getConfigFilePath()
fmt.Println("本地配置文件路径:", configfile)
},
}

func init() {
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(versionCmd)
}
32 changes: 14 additions & 18 deletions internal/nacos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,25 @@ var (
NAMESPACELIST []NamespaceServer
GROUP string // 分组
GROUPLIST []string
USERNAME string // 账户
PASSWORD string // 密码
WRITEFILE string // prometheus 字段 文件路径
IPFILE string // ip hostname 解析文件
PARSEIP bool // 是否启用ip解析
CLUSTER bool // 集群状态
IPDATA map[string]string // 全部ip数据
EXITCODE int // 全局退出状态码
VERSION bool // 版本
WATCH bool // 监控
SECOND time.Duration // 监控服务间隔
V2UPGRADE bool // 2.0版本升级详情
EXPORTJSON bool // 导出json
WEB bool // 开启webapi
WEBPORT string // web端口
STDOUT string // 输出类型
USERNAME string // 账户
PASSWORD string // 密码
WRITEFILE string // prometheus 字段 文件路径
IPDATA map[string]string
IPFILE string // ip hostname 解析文件
PARSEIP bool // 是否启用ip解析
CLUSTER bool // 集群状态
EXITCODE int // 全局退出状态码
WATCH bool // 监控
SECOND time.Duration // 监控服务间隔
V2UPGRADE bool // 2.0版本升级详情
EXPORTJSON bool // 导出json // 开启webapi
WEBPORT string // web端口
STDOUT string // 输出类型
ADDLABEL map[string]string
SyncMap map[string]string
FileConfig struct {
ContextPath string
Sync []map[string]string
}
Na *Nacos
)

type Nacos struct {
Expand Down
35 changes: 0 additions & 35 deletions pkg/homedir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -48,40 +47,6 @@ func HomeDir() (string, error) {
return result, nil
}

// Expand expands the path to include the home directory if the path
// is prefixed with `~`. If it isn't prefixed with `~`, the path is
// returned as-is.
func Expand(path string) (string, error) {
if len(path) == 0 {
return path, nil
}

if path[0] != '~' {
return path, nil
}

if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
return "", errors.New("cannot expand user-specific home dir")
}

dir, err := HomeDir()
if err != nil {
return "", err
}

return filepath.Join(dir, path[1:]), nil
}

// Reset clears the cache, forcing the next call to Dir to re-detect
// the home directory. This generally never has to be called, but can be
// useful in tests if you're modifying the home directory via the HOME
// env var or something.
func Reset() {
cacheLock.Lock()
defer cacheLock.Unlock()
homedirCache = ""
}

func dirUnix() (string, error) {
homeEnv := "HOME"
if runtime.GOOS == "plan9" {
Expand Down

0 comments on commit 495b853

Please sign in to comment.