diff --git a/cmd/flag/common.go b/cmd/flag/common.go index 6c1d762..a2722e9 100644 --- a/cmd/flag/common.go +++ b/cmd/flag/common.go @@ -15,6 +15,7 @@ type CommonFlagValues struct { ShowVersion bool ShowHelp bool DebugMode bool + Quiet bool logLevelInput string LogLevel log.Level LogLevelUpdated bool @@ -36,6 +37,7 @@ 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") @@ -43,8 +45,9 @@ func SetCommonFlags(command *cobra.Command, noResource bool) { 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") @@ -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", @@ -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() @@ -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 diff --git a/cmd/subcmd/cp.go b/cmd/subcmd/cp.go index 0c0a851..c60f9b0 100644 --- a/cmd/subcmd/cp.go +++ b/cmd/subcmd/cp.go @@ -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() @@ -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) } @@ -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", @@ -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 } } @@ -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 } } @@ -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) } diff --git a/cmd/subcmd/get.go b/cmd/subcmd/get.go index 60c6ed1..a5ba687 100644 --- a/cmd/subcmd/get.go +++ b/cmd/subcmd/get.go @@ -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 } @@ -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 } } @@ -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 } } diff --git a/cmd/subcmd/init.go b/cmd/subcmd/init.go index 2e148f0..2c6bcff 100644 --- a/cmd/subcmd/init.go +++ b/cmd/subcmd/init.go @@ -1,8 +1,6 @@ package subcmd import ( - "fmt" - "github.com/cyverse/gocommands/cmd/flag" "github.com/cyverse/gocommands/commons" "github.com/spf13/cobra" @@ -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) diff --git a/cmd/subcmd/lsmeta.go b/cmd/subcmd/lsmeta.go index bbb68a0..5e9ae4d 100644 --- a/cmd/subcmd/lsmeta.go +++ b/cmd/subcmd/lsmeta.go @@ -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 { @@ -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 { @@ -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 { diff --git a/cmd/subcmd/lsticket.go b/cmd/subcmd/lsticket.go index 161bb1b..5a1be53 100644 --- a/cmd/subcmd/lsticket.go +++ b/cmd/subcmd/lsticket.go @@ -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 { diff --git a/cmd/subcmd/put.go b/cmd/subcmd/put.go index b5db397..3dc3e4d 100644 --- a/cmd/subcmd/put.go +++ b/cmd/subcmd/put.go @@ -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 { @@ -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 } } @@ -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 } } diff --git a/commons/bundle_transfer.go b/commons/bundle_transfer.go index 4a8c409..acc6c20 100644 --- a/commons/bundle_transfer.go +++ b/commons/bundle_transfer.go @@ -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 } @@ -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 } @@ -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 } @@ -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) { @@ -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) { diff --git a/commons/print.go b/commons/print.go new file mode 100644 index 0000000..dc1b891 --- /dev/null +++ b/commons/print.go @@ -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 +}