Skip to content

Commit

Permalink
Add --quiet flag to suppress usual output messages
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Apr 18, 2024
1 parent be4fd78 commit c50871c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 40 deletions.
38 changes: 23 additions & 15 deletions cmd/flag/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type CommonFlagValues struct {
ShowVersion bool
ShowHelp bool
DebugMode bool
Quiet bool
logLevelInput string
LogLevel log.Level
LogLevelUpdated bool
Expand All @@ -36,15 +37,17 @@ func SetCommonFlags(command *cobra.Command, noResource bool) {
command.Flags().BoolVarP(&commonFlagValues.ShowVersion, "version", "v", false, "Print version")
command.Flags().BoolVarP(&commonFlagValues.ShowHelp, "help", "h", false, "Print help")
command.Flags().BoolVarP(&commonFlagValues.DebugMode, "debug", "d", false, "Enable debug mode")
command.Flags().BoolVarP(&commonFlagValues.Quiet, "quiet", "q", false, "Suppress usual output messages")
command.Flags().StringVar(&commonFlagValues.logLevelInput, "log_level", "", "Set log level")
command.Flags().IntVarP(&commonFlagValues.SessionID, "session", "s", os.Getppid(), "Set session ID")

if !noResource {
command.Flags().StringVarP(&commonFlagValues.Resource, "resource", "R", "", "Set resource server")
}

command.MarkFlagsMutuallyExclusive("debug", "version")
command.MarkFlagsMutuallyExclusive("quiet", "version")
command.MarkFlagsMutuallyExclusive("log_level", "version")
command.MarkFlagsMutuallyExclusive("debug", "quiet", "log_level")

if !noResource {
command.MarkFlagsMutuallyExclusive("resource", "version")
Expand All @@ -70,6 +73,20 @@ func GetCommonFlagValues(command *cobra.Command) *CommonFlagValues {
return &commonFlagValues
}

func setLogLevel(command *cobra.Command) {
myCommonFlagValues := GetCommonFlagValues(command)

if myCommonFlagValues.Quiet {
log.SetLevel(log.FatalLevel)
} else if myCommonFlagValues.DebugMode {
log.SetLevel(log.DebugLevel)
} else {
if myCommonFlagValues.LogLevelUpdated {
log.SetLevel(myCommonFlagValues.LogLevel)
}
}
}

func ProcessCommonFlags(command *cobra.Command) (bool, error) {
logger := log.WithFields(log.Fields{
"package": "flag",
Expand All @@ -79,13 +96,7 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) {
myCommonFlagValues := GetCommonFlagValues(command)
retryFlagValues := GetRetryFlagValues()

if myCommonFlagValues.DebugMode {
log.SetLevel(log.DebugLevel)
} else {
if myCommonFlagValues.LogLevelUpdated {
log.SetLevel(myCommonFlagValues.LogLevel)
}
}
setLogLevel(command)

if myCommonFlagValues.ShowHelp {
command.Usage()
Expand Down Expand Up @@ -141,19 +152,16 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) {
commons.SetDefaultConfigIfEmpty()
}

// re-configure level
setLogLevel(command)

err := commons.LoadAndOverwriteConfigFromEnv()
if err != nil {
return false, xerrors.Errorf("failed to load config from environment: %w", err) // stop here
}

// re-configure level
if myCommonFlagValues.DebugMode {
log.SetLevel(log.DebugLevel)
} else {
if myCommonFlagValues.LogLevelUpdated {
log.SetLevel(myCommonFlagValues.LogLevel)
}
}
setLogLevel(command)

if retryFlagValues.RetryChild {
// read from stdin
Expand Down
16 changes: 10 additions & 6 deletions cmd/subcmd/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func processCpCommand(command *cobra.Command, args []string) error {
return xerrors.Errorf("failed to input missing fields: %w", err)
}

commonFlagValues := flag.GetCommonFlagValues(command)
recursiveFlagValues := flag.GetRecursiveFlagValues()
forceFlagValues := flag.GetForceFlagValues()
progressFlagValues := flag.GetProgressFlagValues()
Expand Down Expand Up @@ -102,7 +103,7 @@ func processCpCommand(command *cobra.Command, args []string) error {
return xerrors.Errorf("failed to make new target path for copy %s to %s: %w", sourcePath, targetPath, err)
}

err = copyOne(parallelJobManager, inputPathMap, sourcePath, newTargetDirPath, recursiveFlagValues, forceFlagValues, differentialTransferFlagValues)
err = copyOne(parallelJobManager, inputPathMap, sourcePath, newTargetDirPath, commonFlagValues, recursiveFlagValues, forceFlagValues, differentialTransferFlagValues)
if err != nil {
return xerrors.Errorf("failed to perform copy %s to %s: %w", sourcePath, targetPath, err)
}
Expand All @@ -127,7 +128,7 @@ func processCpCommand(command *cobra.Command, args []string) error {
return nil
}

func copyOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[string]bool, sourcePath string, targetPath string, recursiveFlagValues *flag.RecursiveFlagValues, forceFlagValues *flag.ForceFlagValues, differentialTransferFlagValues *flag.DifferentialTransferFlagValues) error {
func copyOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[string]bool, sourcePath string, targetPath string, commonFlagValues *flag.CommonFlagValues, recursiveFlagValues *flag.RecursiveFlagValues, forceFlagValues *flag.ForceFlagValues, differentialTransferFlagValues *flag.DifferentialTransferFlagValues) error {
logger := log.WithFields(log.Fields{
"package": "subcmd",
"function": "copyOne",
Expand Down Expand Up @@ -183,14 +184,16 @@ func copyOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[st
if differentialTransferFlagValues.DifferentialTransfer {
if differentialTransferFlagValues.NoHash {
if targetEntry.Size == sourceEntry.Size {
fmt.Printf("skip copying a file %s. The file already exists!\n", targetFilePath)
commons.Printf("skip copying a file %s. The file already exists!\n", targetFilePath)
logger.Debugf("skip copying a file %s. The file already exists!", targetFilePath)
return nil
}
} else {
if targetEntry.Size == sourceEntry.Size {
// compare hash
if len(sourceEntry.CheckSum) > 0 && bytes.Equal(sourceEntry.CheckSum, targetEntry.CheckSum) {
fmt.Printf("skip copying a file %s. The file with the same hash already exists!\n", targetFilePath)
commons.Printf("skip copying a file %s. The file with the same hash already exists!\n", targetFilePath)
logger.Debugf("skip copying a file %s. The file with the same hash already exists!", targetFilePath)
return nil
}
}
Expand All @@ -200,7 +203,8 @@ func copyOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[st
// ask
overwrite := commons.InputYN(fmt.Sprintf("file %s already exists. Overwrite?", targetFilePath))
if !overwrite {
fmt.Printf("skip copying a file %s. The file already exists!\n", targetFilePath)
commons.Printf("skip copying a file %s. The file already exists!\n", targetFilePath)
logger.Debugf("skip copying a file %s. The file already exists!", targetFilePath)
return nil
}
}
Expand Down Expand Up @@ -235,7 +239,7 @@ func copyOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[st

commons.MarkPathMap(inputPathMap, targetDirPath)

err = copyOne(parallelJobManager, inputPathMap, entry.Path, targetDirPath, recursiveFlagValues, forceFlagValues, differentialTransferFlagValues)
err = copyOne(parallelJobManager, inputPathMap, entry.Path, targetDirPath, commonFlagValues, recursiveFlagValues, forceFlagValues, differentialTransferFlagValues)
if err != nil {
return xerrors.Errorf("failed to perform copy %s to %s: %w", entry.Path, targetPath, err)
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/subcmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,14 @@ func getOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[str

if trxStatusFileExist {
// incomplete file - resume downloading
fmt.Printf("resume downloading a data object %s\n", targetFilePath)
commons.Printf("resume downloading a data object %s\n", targetFilePath)
logger.Debugf("resume downloading a data object %s", targetFilePath)
} else if differentialTransferFlagValues.DifferentialTransfer {
// trx status not exist
if differentialTransferFlagValues.NoHash {
if targetEntry.Size() == sourceEntry.Size {
fmt.Printf("skip downloading a data object %s. The file already exists!\n", targetFilePath)
commons.Printf("skip downloading a data object %s. The file already exists!\n", targetFilePath)
logger.Debugf("skip downloading a data object %s. The file already exists!", targetFilePath)
return nil
}

Expand All @@ -337,7 +339,8 @@ func getOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[str
}

if bytes.Equal(sourceEntry.CheckSum, hash) {
fmt.Printf("skip downloading a data object %s. The file with the same hash already exists!\n", targetFilePath)
commons.Printf("skip downloading a data object %s. The file with the same hash already exists!\n", targetFilePath)
logger.Debugf("skip downloading a data object %s. The file with the same hash already exists!", targetFilePath)
return nil
}
}
Expand All @@ -351,7 +354,8 @@ func getOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[str
// ask
overwrite := commons.InputYN(fmt.Sprintf("file %s already exists. Overwrite?", targetFilePath))
if !overwrite {
fmt.Printf("skip downloading a data object %s. The file already exists!\n", targetFilePath)
commons.Printf("skip downloading a data object %s. The file already exists!\n", targetFilePath)
logger.Debugf("skip downloading a data object %s. The file already exists!", targetFilePath)
return nil
}
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/subcmd/init.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package subcmd

import (
"fmt"

"github.com/cyverse/gocommands/cmd/flag"
"github.com/cyverse/gocommands/commons"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -78,7 +76,7 @@ func processInitCommand(command *cobra.Command, args []string) error {
return xerrors.Errorf("failed to save iCommands Environment: %w", err)
}
} else {
fmt.Println("gocommands is already configured for following account:")
commons.Println("gocommands is already configured for following account:")
err := commons.PrintAccount()
if err != nil {
return xerrors.Errorf("failed to print account info: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/subcmd/lsmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func listMetaForPath(fs *irodsclient_fs.FileSystem, targetPath string, listFlagV
}

if len(metas) == 0 {
fmt.Printf("Found no metadata\n")
commons.Printf("Found no metadata\n")
} else {
err = printMetas(metas, listFlagValues)
if err != nil {
Expand All @@ -112,7 +112,7 @@ func listMetaForUser(fs *irodsclient_fs.FileSystem, username string, listFlagVal
}

if len(metas) == 0 {
fmt.Printf("Found no metadata\n")
commons.Printf("Found no metadata\n")
} else {
err = printMetas(metas, listFlagValues)
if err != nil {
Expand All @@ -130,7 +130,7 @@ func listMetaForResource(fs *irodsclient_fs.FileSystem, resource string, listFla
}

if len(metas) == 0 {
fmt.Printf("Found no metadata\n")
commons.Printf("Found no metadata\n")
} else {
err = printMetas(metas, listFlagValues)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/subcmd/lsticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func listTicket(fs *irodsclient_fs.FileSystem, listFlagValues *flag.ListFlagValu
}

if len(tickets) == 0 {
fmt.Printf("Found no tickets\n")
commons.Printf("Found no tickets\n")
} else {
err = printTickets(fs, tickets, listFlagValues)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions cmd/subcmd/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ func putOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[str
if differentialTransferFlagValues.DifferentialTransfer {
if differentialTransferFlagValues.NoHash {
if targetEntry.Size == sourceStat.Size() {
fmt.Printf("skip uploading a file %s. The file already exists!\n", targetFilePath)
commons.Printf("skip uploading a file %s. The file already exists!\n", targetFilePath)
logger.Debugf("skip uploading a file %s. The file already exists!", targetFilePath)
return nil
}
} else {
Expand All @@ -337,7 +338,8 @@ func putOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[str
}

if bytes.Equal(hash, targetEntry.CheckSum) {
fmt.Printf("skip uploading a file %s. The file with the same hash already exists!\n", targetFilePath)
commons.Printf("skip uploading a file %s. The file with the same hash already exists!\n", targetFilePath)
logger.Debugf("skip uploading a file %s. The file with the same hash already exists!", targetFilePath)
return nil
}
}
Expand All @@ -348,7 +350,8 @@ func putOne(parallelJobManager *commons.ParallelJobManager, inputPathMap map[str
// ask
overwrite := commons.InputYN(fmt.Sprintf("file %s already exists. Overwrite?", targetFilePath))
if !overwrite {
fmt.Printf("skip uploading a file %s. The data object already exists!\n", targetFilePath)
commons.Printf("skip uploading a file %s. The data object already exists!\n", targetFilePath)
logger.Debugf("skip uploading a file %s. The data object already exists!", targetFilePath)
return nil
}
}
Expand Down
12 changes: 7 additions & 5 deletions commons/bundle_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (manager *BundleTransferManager) Schedule(source string, dir bool, size int
// handle dir
exist := manager.filesystem.ExistsDir(targePath)
if exist {
fmt.Printf("skip adding a dir %s to the bundle. The dir already exists!\n", source)
Printf("skip adding a dir %s to the bundle. The dir already exists!\n", source)
logger.Debugf("skip adding a dir %s to the bundle. The dir already exists!", source)
return nil
}
Expand All @@ -325,7 +325,7 @@ func (manager *BundleTransferManager) Schedule(source string, dir bool, size int

if manager.noHashForComparison {
if targetEntry.Size == size {
fmt.Printf("skip adding a file %s to the bundle. The file already exists!\n", source)
Printf("skip adding a file %s to the bundle. The file already exists!\n", source)
logger.Debugf("skip adding a file %s to the bundle. The file already exists!", source)
return nil
}
Expand All @@ -341,7 +341,7 @@ func (manager *BundleTransferManager) Schedule(source string, dir bool, size int
}

if bytes.Equal(hash, targetEntry.CheckSum) {
fmt.Printf("skip adding a file %s to the bundle. The file with the same hash already exists!\n", source)
Printf("skip adding a file %s to the bundle. The file with the same hash already exists!\n", source)
logger.Debugf("skip adding a file %s to the bundle. The file with the same hash already exists!", source)
return nil
}
Expand Down Expand Up @@ -1272,7 +1272,8 @@ func CleanUpOldLocalBundles(localTempDirPath string, force bool) {
}
}

fmt.Printf("deleted %d old local bundles in %s\n", deletedCount, localTempDirPath)
Printf("deleted %d old local bundles in %s\n", deletedCount, localTempDirPath)
logger.Debugf("deleted %d old local bundles in %s", deletedCount, localTempDirPath)
}

func CleanUpOldIRODSBundles(fs *irodsclient_fs.FileSystem, irodsTempDirPath string, removeDir bool, force bool) {
Expand Down Expand Up @@ -1311,7 +1312,8 @@ func CleanUpOldIRODSBundles(fs *irodsclient_fs.FileSystem, irodsTempDirPath stri
}
}

fmt.Printf("deleted %d old irods bundles in %s\n", deletedCount, irodsTempDirPath)
Printf("deleted %d old irods bundles in %s\n", deletedCount, irodsTempDirPath)
logger.Debugf("deleted %d old irods bundles in %s", deletedCount, irodsTempDirPath)

if removeDir {
if IsStagingDirInTargetPath(irodsTempDirPath) {
Expand Down
21 changes: 21 additions & 0 deletions commons/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package commons

import (
"fmt"

log "github.com/sirupsen/logrus"
)

func Println(a ...any) (n int, err error) {
if log.GetLevel() > log.InfoLevel {
return fmt.Println(a...)
}
return 0, nil
}

func Printf(format string, a ...any) (n int, err error) {
if log.GetLevel() > log.InfoLevel {
return fmt.Printf(format, a...)
}
return 0, nil
}

0 comments on commit c50871c

Please sign in to comment.