Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 2887ed8

Browse files
committed
update utils
1 parent 86e3780 commit 2887ed8

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

conf.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"log"
65
"math/rand"
76
"os"
87
"strings"
@@ -31,7 +30,7 @@ func GetTokenFromEnv() []string {
3130

3231
func ValidateToken() {
3332
if len(tokenList) == 0 {
34-
log.Fatal("No token found!")
33+
logger.Fatal("No token found! Please set TOKEN environment variable.")
3534
}
3635
logger.Debug(fmt.Sprintf("Found %d available token(s).", len(tokenList)))
3736
}

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ func main() {
1010

1111
tokenList = GetTokenFromEnv()
1212
ValidateToken()
13-
1413
}

utils.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func Sum(array []int) int {
6+
total := 0
7+
for _, v := range array {
8+
total += v
9+
}
10+
return total
11+
}
12+
13+
var sizeUnits = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB"}
14+
15+
func SizeConvert(size float64, idx int) string {
16+
if size <= 0 {
17+
return "0"
18+
}
19+
for idx < len(sizeUnits)-1 && size > 1024 {
20+
size /= 1024
21+
idx++
22+
}
23+
return fmt.Sprintf("%.1f %s", size, sizeUnits[idx])
24+
}
25+
26+
var scaleUnits = []string{"", "k", "m"}
27+
28+
func ScaleConvert(n float64, useSmallScale bool) string {
29+
idx := 0
30+
condition := 100.0
31+
if !useSmallScale {
32+
condition = 1000.0
33+
}
34+
for idx < len(scaleUnits)-1 && n > condition {
35+
n /= 1000
36+
idx++
37+
}
38+
if idx == 0 {
39+
return fmt.Sprintf("%.0f", n)
40+
}
41+
return fmt.Sprintf("%.1f%s", n, scaleUnits[idx])
42+
}

0 commit comments

Comments
 (0)