Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ The cmd package contains logic for running synchronous and asynchronous commands
## [`random`](https://pkg.go.dev/github.com/ipfs/go-test/random "API documentation") package

The random package contains logic for generating random test data.

## Command Line Tools

Command line utilities are located in the [`cli`](https://github.com/ipfs/go-test/tree/main/cli) directory.
1 change: 1 addition & 0 deletions cli/random-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
random-data
37 changes: 37 additions & 0 deletions cli/random-data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# random-data - writes random data to stdout

`random-data` writes pseudo-random data to stdout for testing. The data can be written as raw bytes or base64 encodde.

## Install

```
go install github.com/ipfs/go-test/cli/random-data
```

## Usage

```sh
> random-bytes -help
NAME
./random-data - Write random data to stdout

USAGE
./random-data [options]

OPTIONS:
-b64
base-64 encode output
-seed int
random seed, 0 or unset for current time
-size int
number of bytes to generate
```

## Examples

```sh
random-data -size=64 -b64
vRujjyEvx8lYiELflaDINvkm5nfueWGCdzEOxhRtz7N2EQjoyrpoMdVVOrwAgNO0tVojDAgu0JpU4hKSsdVl8A==
```

Note: Specifying the same seed will produce the same results.
75 changes: 75 additions & 0 deletions cli/random-data/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"bufio"
"encoding/base64"
"flag"
"fmt"
"io"
"math/rand"
"os"

random "github.com/ipfs/go-test/random"
)

func main() {
var usage = `NAME
%s - Write random data to stdout

USAGE
%s [options]

OPTIONS:
`
flag.Usage = func() {
cmd := os.Args[0]
fmt.Fprintf(os.Stderr, usage, cmd, cmd)
flag.PrintDefaults()
}

var (
b64 bool
seed int64
size int64
)
flag.BoolVar(&b64, "b64", false, "base-64 encode output")
flag.Int64Var(&seed, "seed", 0, "random seed, 0 or unset for current time")
flag.Int64Var(&size, "size", 0, "number of bytes to generate")
flag.Parse()

if size < 1 {
fmt.Fprintln(os.Stderr, "missing value for size")
fmt.Fprintln(os.Stderr)
flag.Usage()
os.Exit(1)
}

err := writeData(seed, size, b64)
if err != nil {
fmt.Fprintln(os.Stderr, "missing value for size")
os.Exit(1)
}
fmt.Fprintln(os.Stdout)
}

func writeData(seed, size int64, b64 bool) error {
var rnd *rand.Rand
if seed == 0 {
rnd = random.NewRand()
} else {
rnd = random.NewSeededRand(seed)
}

var w io.Writer
if b64 {
b := base64.NewEncoder(base64.StdEncoding, os.Stdout)
defer b.Close()
w = b
} else {
b := bufio.NewWriter(os.Stdout)
defer b.Flush()
w = b
}
_, err := io.CopyN(w, rnd, size)
return err
}
1 change: 1 addition & 0 deletions cli/random-files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
random-files
21 changes: 12 additions & 9 deletions random/files/random-files/README.md → cli/random-files/README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
# random-files - create random filesystem hierarchies

random-files creates random filesystem hierarchies for testing
`random-files` creates random filesystem hierarchies for testing

## Install

```
go install github.com/ipfs/go-test/random/files/random-files
go install github.com/ipfs/go-test/cli/random-files
```

## Usage

```sh
> random-files -help
usage: ./random-files [options] <path>...
Write a random filesystem hierarchy to each <path>
NAME
random-files - Write a random filesystem hierarchy to each <path>

Options:
USAGE
random-files [options] <path>...

OPTIONS:
-depth int
depth of the directory tree including the root directory (default 2)
-dirs int
number of subdirectories at each depth (default 5)
-files int
number of files at each depth (default 10)
-filesize int
file fize, or the max file size id RandomSize is true (default 4096)
bytes of random data in each file (default 4096)
-q do not print files and directories
-random-dirs
randomize number of subdirectories, from 1 to -Dirs
randomize number of subdirectories, from 1 to -dirs
-random-files
randomize number of files, from 1 to -Files
randomize number of files, from 1 to -files
-random-size
randomize file size, from 1 to -FileSize (default true)
randomize file size, from 1 to -filesize (default true)
-seed int
random seed, 0 for current time
```
Expand Down
File renamed without changes.