github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/transferfiles/state/utils.go (about) 1 package state 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "strconv" 7 "strings" 8 "sync/atomic" 9 "time" 10 11 "github.com/jfrog/build-info-go/utils" 12 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" 13 "github.com/jfrog/jfrog-client-go/utils/errorutils" 14 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 15 ) 16 17 const ( 18 secondsInMinute = 60 19 secondsInHour = 60 * secondsInMinute 20 secondsInDay = 24 * secondsInHour 21 22 oldTransferDirectoryStructureErrorFormat = `unsupported transfer directory structure found. 23 This structure was created by a previous run of the transfer-files command, but is no longer supported by this JFrog CLI version. 24 You may either downgrade JFrog CLI to the version that was used before, or remove the transfer directory which is located under the JFrog CLI home directory (%s). 25 26 Note: Deleting the transfer directory will remove all your transfer history, which means the transfer will start from scratch` 27 ) 28 29 func ConvertTimeToRFC3339(timeToConvert time.Time) string { 30 return timeToConvert.Format(time.RFC3339) 31 } 32 33 func ConvertRFC3339ToTime(timeToConvert string) (time.Time, error) { 34 return time.Parse(time.RFC3339, timeToConvert) 35 } 36 37 func ConvertTimeToEpochMilliseconds(timeToConvert time.Time) string { 38 return strconv.FormatInt(timeToConvert.UnixMilli(), 10) 39 } 40 41 // SecondsToLiteralTime converts a number of seconds to an easy-to-read string. 42 // Prefix is not taken into account if the time is less than a minute. 43 func SecondsToLiteralTime(secondsToConvert int64, prefix string) string { 44 daysTime := secondsToConvert / secondsInDay 45 daysTimeInSecs := daysTime * secondsInDay 46 hoursTime := (secondsToConvert - daysTimeInSecs) / secondsInHour 47 if daysTime >= 1 { 48 return getTimeAmountWithRemainder(daysTime, hoursTime, day, hour, prefix) 49 } 50 51 hoursTimeInSecs := hoursTime * secondsInHour 52 minutesTime := (secondsToConvert - hoursTimeInSecs) / secondsInMinute 53 if hoursTime >= 1 { 54 return getTimeAmountWithRemainder(hoursTime, minutesTime, hour, minute, prefix) 55 } 56 57 if minutesTime >= 1 { 58 return getTimeAmountWithRemainder(minutesTime, 0, minute, "", prefix) 59 } 60 return "Less than a minute" 61 } 62 63 // Get the time amount as string, with the remainder added only if it is non-zero. 64 // For example "About 2 hours and 1 minute" 65 func getTimeAmountWithRemainder(mainAmount, remainderAmount int64, mainType, remainderType timeTypeSingular, prefix string) string { 66 timeStr := prefix + getTimeSingularOrPlural(mainAmount, mainType) 67 if remainderAmount > 0 { 68 timeStr += " and " + getTimeSingularOrPlural(remainderAmount, remainderType) 69 } 70 return timeStr 71 } 72 73 // Returns the time amount followed by its type, with 's' for plural if needed. 74 // For example '1 hour' or '2 hours'. 75 func getTimeSingularOrPlural(timeAmount int64, timeType timeTypeSingular) string { 76 result := fmt.Sprintf("%d %s", timeAmount, timeType) 77 if timeAmount > 1 { 78 result += "s" 79 } 80 return result 81 } 82 83 func GetRepositoryTransferDir(repoKey string) (string, error) { 84 reposDir, err := coreutils.GetJfrogTransferRepositoriesDir() 85 if err != nil { 86 return "", err 87 } 88 repoHash, err := getRepositoryHash(repoKey) 89 if err != nil { 90 return "", err 91 } 92 93 repoDir := filepath.Join(reposDir, repoHash) 94 err = fileutils.CreateDirIfNotExist(repoDir) 95 if err != nil { 96 return "", err 97 } 98 99 return repoDir, nil 100 } 101 102 func getRepositoryHash(repoKey string) (string, error) { 103 checksums, err := utils.CalcChecksums(strings.NewReader(repoKey), utils.SHA1) 104 if err = errorutils.CheckError(err); err != nil { 105 return "", err 106 } 107 return checksums[utils.SHA1], nil 108 } 109 110 func GetJfrogTransferRepoSubDir(repoKey, subDirName string) (string, error) { 111 transferDir, err := GetRepositoryTransferDir(repoKey) 112 if err != nil { 113 return "", err 114 } 115 return filepath.Join(transferDir, subDirName), nil 116 } 117 118 func GetOldTransferDirectoryStructureError() error { 119 transferDir, err := coreutils.GetJfrogTransferDir() 120 if err != nil { 121 return err 122 } 123 return errorutils.CheckErrorf(oldTransferDirectoryStructureErrorFormat, transferDir) 124 } 125 126 // Atomically add to an int64 variable. 127 // addr - Pointer to int64 variable 128 // delta - The change to do 129 func atomicallyAddInt64(addr *int64, delta int64) { 130 atomic.AddInt64(addr, delta) 131 } 132 133 // Atomically add to an uint64 variable. 134 // addr - Pointer to uint64 variable 135 // delta - The change to do 136 // increase - True to increment, false to decrement 137 func atomicallyAddUint64(addr *uint64, delta uint64, increase bool) { 138 if increase { 139 atomic.AddUint64(addr, delta) 140 } else { 141 atomic.AddUint64(addr, ^(delta - 1)) 142 } 143 }