Skip to content

Commit

Permalink
Enhance 'transfer-setting' to allow changing the log level
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalbe4 committed Sep 12, 2023
1 parent d02cf30 commit ecf4b09
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
45 changes: 40 additions & 5 deletions artifactory/commands/transfer/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,28 +28,60 @@ 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)
} else {
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.")
log.Output(fmt.Sprintf("Note - For Build Info repositories, the number of worker threads will be limited to %d.", utils.MaxBuildInfoThreads))
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
Expand Down
32 changes: 26 additions & 6 deletions artifactory/commands/transferfiles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
corelog "github.com/jfrog/jfrog-cli-core/v2/utils/log"
"io"
"os"
"path"
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion artifactory/utils/transfersettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit ecf4b09

Please sign in to comment.