From 1615cdfc38eedaa77aa445ac89ac91161e092d2c Mon Sep 17 00:00:00 2001 From: tbs60 Date: Wed, 23 Oct 2024 15:41:29 +0800 Subject: [PATCH] feat: support set log dir by json file, issue: #310 --- .../bk_dist/shadertool/command/process.go | 1 + .../bk_dist/shadertool/common/types.go | 54 +++++++++++++++++++ .../bk_dist/ubttool/command/process.go | 2 + 3 files changed, 57 insertions(+) diff --git a/src/backend/booster/bk_dist/shadertool/command/process.go b/src/backend/booster/bk_dist/shadertool/command/process.go index 859d0fc24..da7c2e3ec 100644 --- a/src/backend/booster/bk_dist/shadertool/command/process.go +++ b/src/backend/booster/bk_dist/shadertool/command/process.go @@ -26,6 +26,7 @@ import ( // mainProcess do the make process: func mainProcess(c *commandCli.Context) error { + common.FreshEnvFromProjectSetting() initialLogDir(getLogDir(c.String(FlagLogDir))) common.SetLogLevel(c.String(FlagLog)) diff --git a/src/backend/booster/bk_dist/shadertool/common/types.go b/src/backend/booster/bk_dist/shadertool/common/types.go index ff59c8b6f..1c5c925d7 100644 --- a/src/backend/booster/bk_dist/shadertool/common/types.go +++ b/src/backend/booster/bk_dist/shadertool/common/types.go @@ -10,10 +10,17 @@ package common import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/common/env" + dcFile "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/common/file" dcSDK "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/common/sdk" dcUtil "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/common/util" "github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/blog" + "github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/codec" ) // define const vars @@ -176,3 +183,50 @@ func SetLogLevel(level string) { blog.SetStderrLevel(blog.StderrLevelInfo) } } + +// ++ 增加公共函数,用于从配置文件获取环境变量变设置到当前 +func getProjectSettingFile() (string, error) { + exepath := dcUtil.GetExcPath() + if exepath != "" { + jsonfile := filepath.Join(exepath, "bk_project_setting.json") + if dcFile.Stat(jsonfile).Exist() { + return jsonfile, nil + } + } + + return "", fmt.Errorf("not found project setting file") +} + +func resolveApplyJSON(filename string) (*ApplyParameters, error) { + data, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + + var t ApplyParameters + if err = codec.DecJSON(data, &t); err != nil { + return nil, err + } + + return &t, nil +} + +func FreshEnvFromProjectSetting() error { + projectSettingFile, err := getProjectSettingFile() + if err != nil { + return err + } + + settings, err := resolveApplyJSON(projectSettingFile) + if err != nil { + return err + } + + for k, v := range settings.Env { + os.Setenv(k, v) + } + + return nil +} + +// -- diff --git a/src/backend/booster/bk_dist/ubttool/command/process.go b/src/backend/booster/bk_dist/ubttool/command/process.go index b05055515..3c4f46a5c 100644 --- a/src/backend/booster/bk_dist/ubttool/command/process.go +++ b/src/backend/booster/bk_dist/ubttool/command/process.go @@ -22,11 +22,13 @@ import ( "github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/blog" "github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/conf" + shaderToolComm "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/shadertool/common" commandCli "github.com/urfave/cli" ) // mainProcess do the make process: func mainProcess(c *commandCli.Context) error { + shaderToolComm.FreshEnvFromProjectSetting() initialLogDir(getLogDir(c.String(FlagLogDir))) setLogLevel(c.String(FlagLog))