From ecf4b09c1cfc47c1495f32dda5fb15c8a5a4224d Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Tue, 12 Sep 2023 10:10:47 +0300 Subject: [PATCH] Enhance 'transfer-setting' to allow changing the log level --- artifactory/commands/transfer/settings.go | 45 ++++++++++++++++++--- artifactory/commands/transferfiles/utils.go | 32 ++++++++++++--- artifactory/utils/transfersettings.go | 3 +- go.mod | 2 + go.sum | 4 +- 5 files changed, 72 insertions(+), 14 deletions(-) diff --git a/artifactory/commands/transfer/settings.go b/artifactory/commands/transfer/settings.go index b034412de..97bd40614 100644 --- a/artifactory/commands/transfer/settings.go +++ b/artifactory/commands/transfer/settings.go @@ -2,7 +2,10 @@ package transfer import ( "fmt" + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" + "os" "strconv" + "strings" "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" "github.com/jfrog/jfrog-cli-core/v2/utils/config" @@ -25,6 +28,8 @@ func (tst *TransferSettingsCommand) Run() error { if err != nil { return err } + + // Set the worker threads value. var currThreadsNumber string if currSettings == nil { currThreadsNumber = strconv.Itoa(utils.DefaultThreads) @@ -32,14 +37,26 @@ func (tst *TransferSettingsCommand) Run() error { currThreadsNumber = strconv.Itoa(currSettings.ThreadsNumber) } var threadsNumberInput string - ioutils.ScanFromConsole("Set the maximum number of working threads", &threadsNumberInput, currThreadsNumber) + ioutils.ScanFromConsole("Set the maximum number of worker threads", &threadsNumberInput, currThreadsNumber) threadsNumber, err := strconv.Atoi(threadsNumberInput) if err != nil || threadsNumber < 1 || threadsNumber > MaxThreadsLimit { - return errorutils.CheckErrorf("the value must be a number between 1 and " + strconv.Itoa(MaxThreadsLimit)) + return errorutils.CheckErrorf("the worker threads value must be a number between 1 and " + strconv.Itoa(MaxThreadsLimit)) } - conf := &utils.TransferSettings{ThreadsNumber: threadsNumber} - err = utils.SaveTransferSettings(conf) - if err != nil { + + // Set the log level value. + currLogLevel := tst.getCurrLogLevel(*currSettings) + var logLevel string + ioutils.ScanFromConsole("Set the log level (DEBUG, INFO, WARN or ERROR)", &logLevel, currLogLevel) + logLevel = strings.ToUpper(logLevel) + if err = tst.validateLogLevelValue(logLevel); err != nil { + return err + } + + conf := &utils.TransferSettings{ + ThreadsNumber: threadsNumber, + LogLevel: logLevel, + } + if err = utils.SaveTransferSettings(conf); err != nil { return err } log.Output("The settings were saved successfully. It might take a few moments for the new settings to take effect.") @@ -47,6 +64,24 @@ func (tst *TransferSettingsCommand) Run() error { return nil } +func (tst *TransferSettingsCommand) getCurrLogLevel(settings utils.TransferSettings) string { + currLogLevel := settings.LogLevel + if currLogLevel == "" { + currLogLevel = os.Getenv(coreutils.LogLevel) + } + if currLogLevel == "" { + currLogLevel = "INFO" + } + return currLogLevel +} + +func (tst *TransferSettingsCommand) validateLogLevelValue(loglevel string) error { + if loglevel != "DEBUG" && loglevel != "INFO" && loglevel != "WARN" && loglevel != "ERROR" { + return errorutils.CheckErrorf("the log level value is invalid") + } + return nil +} + func (tst *TransferSettingsCommand) ServerDetails() (*config.ServerDetails, error) { // There's no need to report the usage of this command. return nil, nil diff --git a/artifactory/commands/transferfiles/utils.go b/artifactory/commands/transferfiles/utils.go index 668bebc1d..d46fba541 100644 --- a/artifactory/commands/transferfiles/utils.go +++ b/artifactory/commands/transferfiles/utils.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + corelog "github.com/jfrog/jfrog-cli-core/v2/utils/log" "io" "os" "path" @@ -338,17 +339,37 @@ func periodicallyUpdateThreadsAndStopStatus(pcWrapper *producerConsumerWrapper, log.Debug("Stopping the polling on the settings and stop files for the current phase.") return } - if err := updateThreads(pcWrapper, buildInfoRepo); err != nil { + + settings, err := utils.LoadTransferSettings() + if err != nil || settings == nil { + log.Error(err) + return + } + + updateWorkerThreads(*settings, pcWrapper, buildInfoRepo) + if err = updateLogLevel(*settings); err != nil { log.Error(err) } } } -func updateThreads(pcWrapper *producerConsumerWrapper, buildInfoRepo bool) error { - settings, err := utils.LoadTransferSettings() - if err != nil || settings == nil { - return err +func updateLogLevel(settings utils.TransferSettings) error { + envLogLevel := os.Getenv(coreutils.LogLevel) + if envLogLevel == "" { + envLogLevel = "INFO" } + if settings.LogLevel == envLogLevel { + return nil + } + log.Info("The log level was changed from", envLogLevel, "to", settings.LogLevel, ".") + if err := os.Setenv(coreutils.LogLevel, settings.LogLevel); err != nil { + return errorutils.CheckError(err) + } + log.Logger.SetLogLevel(corelog.GetCliLogLevel()) + return nil +} + +func updateWorkerThreads(settings utils.TransferSettings, pcWrapper *producerConsumerWrapper, buildInfoRepo bool) { calculatedNumberOfThreads := settings.CalcNumberOfThreads(buildInfoRepo) if curThreads != calculatedNumberOfThreads { if pcWrapper != nil { @@ -360,7 +381,6 @@ func updateThreads(pcWrapper *producerConsumerWrapper, buildInfoRepo bool) error } else { log.Debug("No change to the number of threads have been detected.") } - return nil } // Interrupt the transfer by populating the stopSignal channel with the Interrupt signal if the '~/.jfrog/transfer/stop' file exists. diff --git a/artifactory/utils/transfersettings.go b/artifactory/utils/transfersettings.go index cf3bc198c..532f92720 100644 --- a/artifactory/utils/transfersettings.go +++ b/artifactory/utils/transfersettings.go @@ -22,7 +22,8 @@ const ( ) type TransferSettings struct { - ThreadsNumber int `json:"threadsNumber,omitempty"` + ThreadsNumber int `json:"threadsNumber,omitempty"` + LogLevel string `json:"logLevel,omitempty"` } func (ts *TransferSettings) CalcNumberOfThreads(buildInfoRepo bool) int { diff --git a/go.mod b/go.mod index 8ed9ccd71..e5cbf52bb 100644 --- a/go.mod +++ b/go.mod @@ -93,6 +93,8 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect ) +replace github.com/jfrog/jfrog-client-go => github.com/eyalbe4/jfrog-client-go v1.28.1-0.20230912065839-75d4b0572910 + // replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20230905120411-62d1bdd4eb38 // replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.2.6-0.20230418122323-2bf299dd6d27 diff --git a/go.sum b/go.sum index c0825b56f..326f334d1 100644 --- a/go.sum +++ b/go.sum @@ -100,6 +100,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/eyalbe4/jfrog-client-go v1.28.1-0.20230912065839-75d4b0572910 h1:Q3nV0xHcu2HtzHC6f3pYWwtlrphOoVcfEVisxXuKZTQ= +github.com/eyalbe4/jfrog-client-go v1.28.1-0.20230912065839-75d4b0572910/go.mod h1:362+oa7uTTYurzBs1L0dmUTlLo7uhpAU/pwM5Zb9clg= github.com/forPelevin/gomoji v1.1.8 h1:JElzDdt0TyiUlecy6PfITDL6eGvIaxqYH1V52zrd0qQ= github.com/forPelevin/gomoji v1.1.8/go.mod h1:8+Z3KNGkdslmeGZBC3tCrwMrcPy5GRzAD+gL9NAwMXg= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= @@ -198,8 +200,6 @@ github.com/jfrog/build-info-go v1.9.10 h1:uXnDLVxpqxoAMpXcki00QaBB+M2BoGMMpHODPk github.com/jfrog/build-info-go v1.9.10/go.mod h1:ujJ8XQZMdT2tMkLSMJNyDd1pCY+duwHdjV+9or9FLIg= github.com/jfrog/gofrog v1.3.0 h1:o4zgsBZE4QyDbz2M7D4K6fXPTBJht+8lE87mS9bw7Gk= github.com/jfrog/gofrog v1.3.0/go.mod h1:IFMc+V/yf7rA5WZ74CSbXe+Lgf0iApEQLxRZVzKRUR0= -github.com/jfrog/jfrog-client-go v1.32.1 h1:RQmuPSLsF5222vZJzwkgHSZMMJF83ExS7SwIvh4P+H8= -github.com/jfrog/jfrog-client-go v1.32.1/go.mod h1:362+oa7uTTYurzBs1L0dmUTlLo7uhpAU/pwM5Zb9clg= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=