From 1c1360ec23e12632f40d7838a5c372a7a6588bd0 Mon Sep 17 00:00:00 2001 From: icey-yu <1186114839@qq.com> Date: Fri, 27 Dec 2024 16:06:09 +0800 Subject: [PATCH] feat: config center --- pkg/common/cmd/root.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 8fba87b9..6521e551 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -152,9 +152,22 @@ func (r *RootCmd) updateConfigFromEtcd(opts *CmdOpts) error { } update := func(configFileName string, configStruct any) error { + ctx := context.TODO() key := disetcd.BuildKey(configFileName) - etcdRes, err := r.etcdClient.Get(context.TODO(), key) - if err != nil || etcdRes.Count == 0 { + etcdRes, err := r.etcdClient.Get(ctx, key) + if err != nil { + log.ZWarn(ctx, "root cmd updateConfigFromEtcd, etcd Get err: %v", errs.Wrap(err)) + return nil + } + if etcdRes.Count == 0 { + data, err := json.Marshal(configStruct) + if err != nil { + return errs.ErrArgs.WithDetail(err.Error()).Wrap() + } + _, err = r.etcdClient.Put(ctx, disetcd.BuildKey(configFileName), string(data)) + if err != nil { + log.ZWarn(ctx, "root cmd updateConfigFromEtcd, etcd Put err: %v", errs.Wrap(err)) + } return nil } err = json.Unmarshal(etcdRes.Kvs[0].Value, configStruct)