Skip to content

Commit 913034d

Browse files
authored
Move cli apps (#17)
* Move random-files tool into cli directory * update README.md * Add random-data cli utility
1 parent 2d31cf0 commit 913034d

File tree

7 files changed

+130
-9
lines changed

7 files changed

+130
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ The cmd package contains logic for running synchronous and asynchronous commands
1212
## [`random`](https://pkg.go.dev/github.com/ipfs/go-test/random "API documentation") package
1313

1414
The random package contains logic for generating random test data.
15+
16+
## Command Line Tools
17+
18+
Command line utilities are located in the [`cli`](https://github.com/ipfs/go-test/tree/main/cli) directory.

cli/random-data/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random-data

cli/random-data/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# random-data - writes random data to stdout
2+
3+
`random-data` writes pseudo-random data to stdout for testing. The data can be written as raw bytes or base64 encodde.
4+
5+
## Install
6+
7+
```
8+
go install github.com/ipfs/go-test/cli/random-data
9+
```
10+
11+
## Usage
12+
13+
```sh
14+
> random-bytes -help
15+
NAME
16+
./random-data - Write random data to stdout
17+
18+
USAGE
19+
./random-data [options]
20+
21+
OPTIONS:
22+
-b64
23+
base-64 encode output
24+
-seed int
25+
random seed, 0 or unset for current time
26+
-size int
27+
number of bytes to generate
28+
```
29+
30+
## Examples
31+
32+
```sh
33+
random-data -size=64 -b64
34+
vRujjyEvx8lYiELflaDINvkm5nfueWGCdzEOxhRtz7N2EQjoyrpoMdVVOrwAgNO0tVojDAgu0JpU4hKSsdVl8A==
35+
```
36+
37+
Note: Specifying the same seed will produce the same results.

cli/random-data/main.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"encoding/base64"
6+
"flag"
7+
"fmt"
8+
"io"
9+
"math/rand"
10+
"os"
11+
12+
random "github.com/ipfs/go-test/random"
13+
)
14+
15+
func main() {
16+
var usage = `NAME
17+
%s - Write random data to stdout
18+
19+
USAGE
20+
%s [options]
21+
22+
OPTIONS:
23+
`
24+
flag.Usage = func() {
25+
cmd := os.Args[0]
26+
fmt.Fprintf(os.Stderr, usage, cmd, cmd)
27+
flag.PrintDefaults()
28+
}
29+
30+
var (
31+
b64 bool
32+
seed int64
33+
size int64
34+
)
35+
flag.BoolVar(&b64, "b64", false, "base-64 encode output")
36+
flag.Int64Var(&seed, "seed", 0, "random seed, 0 or unset for current time")
37+
flag.Int64Var(&size, "size", 0, "number of bytes to generate")
38+
flag.Parse()
39+
40+
if size < 1 {
41+
fmt.Fprintln(os.Stderr, "missing value for size")
42+
fmt.Fprintln(os.Stderr)
43+
flag.Usage()
44+
os.Exit(1)
45+
}
46+
47+
err := writeData(seed, size, b64)
48+
if err != nil {
49+
fmt.Fprintln(os.Stderr, "missing value for size")
50+
os.Exit(1)
51+
}
52+
fmt.Fprintln(os.Stdout)
53+
}
54+
55+
func writeData(seed, size int64, b64 bool) error {
56+
var rnd *rand.Rand
57+
if seed == 0 {
58+
rnd = random.NewRand()
59+
} else {
60+
rnd = random.NewSeededRand(seed)
61+
}
62+
63+
var w io.Writer
64+
if b64 {
65+
b := base64.NewEncoder(base64.StdEncoding, os.Stdout)
66+
defer b.Close()
67+
w = b
68+
} else {
69+
b := bufio.NewWriter(os.Stdout)
70+
defer b.Flush()
71+
w = b
72+
}
73+
_, err := io.CopyN(w, rnd, size)
74+
return err
75+
}

cli/random-files/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random-files

random/files/random-files/README.md renamed to cli/random-files/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
# random-files - create random filesystem hierarchies
22

3-
random-files creates random filesystem hierarchies for testing
3+
`random-files` creates random filesystem hierarchies for testing
44

55
## Install
66

77
```
8-
go install github.com/ipfs/go-test/random/files/random-files
8+
go install github.com/ipfs/go-test/cli/random-files
99
```
1010

1111
## Usage
1212

1313
```sh
1414
> random-files -help
15-
usage: ./random-files [options] <path>...
16-
Write a random filesystem hierarchy to each <path>
15+
NAME
16+
random-files - Write a random filesystem hierarchy to each <path>
1717

18-
Options:
18+
USAGE
19+
random-files [options] <path>...
20+
21+
OPTIONS:
1922
-depth int
2023
depth of the directory tree including the root directory (default 2)
2124
-dirs int
2225
number of subdirectories at each depth (default 5)
2326
-files int
2427
number of files at each depth (default 10)
2528
-filesize int
26-
file fize, or the max file size id RandomSize is true (default 4096)
29+
bytes of random data in each file (default 4096)
2730
-q do not print files and directories
2831
-random-dirs
29-
randomize number of subdirectories, from 1 to -Dirs
32+
randomize number of subdirectories, from 1 to -dirs
3033
-random-files
31-
randomize number of files, from 1 to -Files
34+
randomize number of files, from 1 to -files
3235
-random-size
33-
randomize file size, from 1 to -FileSize (default true)
36+
randomize file size, from 1 to -filesize (default true)
3437
-seed int
3538
random seed, 0 for current time
3639
```
File renamed without changes.

0 commit comments

Comments
 (0)