From fe806f3afa75889bbf698a0db12c10230231f506 Mon Sep 17 00:00:00 2001 From: wubijie Date: Tue, 26 Nov 2024 16:45:01 +0800 Subject: [PATCH] add config load() for path-config --- configmanage/server/internal/pathfile.go | 11 ++++++++++ configmanage/server/service/path.go | 27 ++++++++++++++++++++++++ configmanage/server/service/path_test.go | 13 ++++++++++++ 3 files changed, 51 insertions(+) diff --git a/configmanage/server/internal/pathfile.go b/configmanage/server/internal/pathfile.go index e4a6519f..aeee4ba3 100644 --- a/configmanage/server/internal/pathfile.go +++ b/configmanage/server/internal/pathfile.go @@ -49,3 +49,14 @@ func (pf *PathFile) Add() error { pf.Hostuuid, ).Error } + +// 根据配置uuid获取用户自己创建的配置信息 +func GetPathFileByInfoUUID(uuid string, isindex interface{}) (PathFile, error) { + var file PathFile + if isindex != nil { + err := db.MySQL().Model(&PathFile{}).Where("config_info_uuid=? and is_from_host=0 and is_index = ?", uuid, isindex).Find(&file).Error + return file, err + } + err := db.MySQL().Model(&PathFile{}).Where("config_info_uuid=? and is_from_host=0 ", uuid).Find(&file).Error + return file, err +} diff --git a/configmanage/server/service/path.go b/configmanage/server/service/path.go index 59f20e85..6bde8b3a 100644 --- a/configmanage/server/service/path.go +++ b/configmanage/server/service/path.go @@ -42,7 +42,34 @@ func (pc *PathConfig) toPathFile() PathFile { } } +func toPathConfig(pf *PathFile) PathConfig { + return PathConfig{ + UUID: pf.UUID, + ConfigInfoUUID: pf.ConfigInfoUUID, + Path: pf.Path, + Name: pf.Name, + Content: pf.Content, + Version: pf.Version, + IsActive: pf.IsActive, + } +} + func (pc *PathConfig) Record() error { pf := pc.toPathFile() return pf.Add() } + +func (pc *PathConfig) Load() error { + // 加载正在使用的某配置文件 + pf, err := internal.GetPathFileByInfoUUID(pc.ConfigInfoUUID, true) + if err != nil { + return err + } + pc.UUID = pf.UUID + pc.Path = pf.Path + pc.Name = pf.Name + pc.Content = pf.Content + pc.Version = pf.Version + pc.IsActive = pf.IsActive + return nil +} diff --git a/configmanage/server/service/path_test.go b/configmanage/server/service/path_test.go index 7d41a52c..9d065921 100644 --- a/configmanage/server/service/path_test.go +++ b/configmanage/server/service/path_test.go @@ -27,3 +27,16 @@ func TestPathConfig_Record(t *testing.T) { os.Exit(-1) } } + +func TestPathConfig_Load(t *testing.T) { + // 设置测试数据 + pc := &PathConfig{ + ConfigInfoUUID: "158e0acf-159b-4876-83b1-fa5f3d6460b1", + } + err := pc.Load() + if err != nil { + fmt.Printf("record error: %s\n", err) + os.Exit(-1) + } + fmt.Printf("pc: %v\n", pc) +} -- Gitee