-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (114 loc) · 3.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
crypto_rand "crypto/rand"
"encoding/binary"
"flag"
"fmt"
"math"
"math/rand"
"os"
"regexp"
"strconv"
"time"
"github.com/schollz/progressbar/v3"
)
func printUnitHelp() {
fmt.Println("Accepted storage size units:")
fmt.Println(" KB|K - Kilobyte")
fmt.Println(" MB|M - Megabyte")
fmt.Println(" GB|G - Gigabyte")
fmt.Println(" TB|T - Terabyte")
fmt.Println(" PB|P - Petabyte")
fmt.Println(" EB|E - Exabyte")
fmt.Println(" ZB|Z - Yottabyte")
os.Exit(2)
}
func getSize(fileSize string) int {
var units = [8]string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB"}
var unitsShort = [8]string{"B", "K", "M", "G", "T", "P", "E", "Z"}
rSizeString, _ := regexp.Compile(`^\d*`)
fileSizeString := rSizeString.FindString(fileSize)
rUnit, _ := regexp.Compile(`\D*$`)
unit := rUnit.FindString(fileSize)
var chosenUnitIndex int
for i := range units {
if unit == units[i] {
chosenUnitIndex = i
break
}
if unit == unitsShort[i] {
chosenUnitIndex = i
break
}
if i == len(units)-1 && chosenUnitIndex == 0 {
fmt.Println("Unit '" + unit + "' unknown")
printUnitHelp()
}
}
fileSizeInt, err := strconv.Atoi(fileSizeString)
if err != nil {
fmt.Println("Unable to parse file size")
os.Exit(2)
}
if chosenUnitIndex == 0 {
return fileSizeInt
} else {
return int((math.Pow(1024, float64(chosenUnitIndex))) * float64(fileSizeInt))
}
}
func main() {
fileSize := flag.String("s", "1M", "Set the size of the dummy file you want to create")
fileName := flag.String("o", "dummy", "Set where you want to output the dummy file")
flag.Parse()
// Generate crypto seed for random
var seed [8]byte
_, err := crypto_rand.Read(seed[:])
if err != nil {
panic("cannot seed math/rand package with cryptographically secure random number generator")
}
cRand := rand.New(rand.NewSource(int64(binary.BigEndian.Uint64(seed[:]))))
bytesToGenerate := getSize(*fileSize)
buffer := 1048576 // 1 MB
f, err := os.OpenFile(*fileName, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic("Unable to create '" + *fileName + "', make sure path exist, check permissions and try again")
}
defer f.Close()
bar := progressbar.NewOptions(bytesToGenerate,
progressbar.OptionShowBytes(true),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetDescription("Generating file '"+*fileName+"' ("+*fileSize+")"),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
start := time.Now()
for {
if bytesToGenerate > buffer {
token := make([]byte, buffer)
_, err = cRand.Read(token)
if err != nil {
panic("Unable to generate random data")
}
f.Write(token)
bar.Add(buffer)
bytesToGenerate -= buffer
} else {
token := make([]byte, bytesToGenerate)
cRand.Read(token)
if err != nil {
panic("Unable to generate random data")
}
f.Write(token)
bar.Add(bytesToGenerate)
bytesToGenerate = 0
}
if bytesToGenerate <= 0 {
break
}
}
fmt.Println("\n'" + *fileName + "' was generated in " + time.Since(start).String())
}