From 8a23d668ec001027aaf2edaa6c07de7ab4c2b9c4 Mon Sep 17 00:00:00 2001 From: wubijie Date: Mon, 4 Nov 2024 14:14:09 +0800 Subject: [PATCH] add HostConfig load func --- configmanage/server/internal/configinfo.go | 2 +- configmanage/server/internal/hostfile.go | 10 ++++++++++ configmanage/server/service/host.go | 15 +++++++++++++++ configmanage/server/service/host_test.go | 13 +++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/configmanage/server/internal/configinfo.go b/configmanage/server/internal/configinfo.go index b34e77b4..b08ca97a 100644 --- a/configmanage/server/internal/configinfo.go +++ b/configmanage/server/internal/configinfo.go @@ -3,7 +3,7 @@ package internal import "openeuler.org/PilotGo/configmanage-plugin/db" type ConfigInfo struct { - ID int `gorm:"unique;autoIncrement:true"` + ID int `gorm:"autoIncrement:true"` UUID string `gorm:"type:varchar(50);primary_key" json:"uuid"` Type string `json:"type"` Description string `json:"description"` diff --git a/configmanage/server/internal/hostfile.go b/configmanage/server/internal/hostfile.go index f5b1f280..c6ea6dd3 100644 --- a/configmanage/server/internal/hostfile.go +++ b/configmanage/server/internal/hostfile.go @@ -25,3 +25,13 @@ type HostFile struct { func (hf *HostFile) Add() error { return db.MySQL().Save(&hf).Error } + +func GetHostFileByInfoUUID(uuid string, isindex interface{}) (HostFile, error) { + var file HostFile + if isindex != nil { + err := db.MySQL().Model(&HostFile{}).Where("config_info_uuid=? && is_index = ?", uuid, isindex).Find(&file).Error + return file, err + } + err := db.MySQL().Model(&HostFile{}).Where("config_info_uuid=?", uuid).Find(&file).Error + return file, err +} diff --git a/configmanage/server/service/host.go b/configmanage/server/service/host.go index aa3ff251..941711d2 100644 --- a/configmanage/server/service/host.go +++ b/configmanage/server/service/host.go @@ -53,3 +53,18 @@ func (hc *HostConfig) Record() error { hf := hc.toHostFile() return hf.Add() } + +func (hc *HostConfig) Load() error { + // 加载正在使用的某配置文件 + hf, err := internal.GetHostFileByInfoUUID(hc.ConfigInfoUUID, true) + if err != nil { + return err + } + hc.UUID = hf.UUID + hc.Path = hf.Path + hc.Name = hf.Name + hc.Content = hf.Content + hc.Version = hf.Version + hc.IsActive = hf.IsActive + return nil +} diff --git a/configmanage/server/service/host_test.go b/configmanage/server/service/host_test.go index 32559fc7..8350b775 100644 --- a/configmanage/server/service/host_test.go +++ b/configmanage/server/service/host_test.go @@ -27,3 +27,16 @@ func TestHostConfig_Record(t *testing.T) { os.Exit(-1) } } + +func TestHostConfig_Load(t *testing.T) { + // 设置测试数据 + hc := &HostConfig{ + ConfigInfoUUID: "158e0acf-159b-4876-83b1-fa5f3d6460b1", + } + err := hc.Load() + if err != nil { + fmt.Printf("record error: %s\n", err) + os.Exit(-1) + } + fmt.Printf("hc: %v\n", hc) +} -- Gitee