Skip to content

Commit 3e1b734

Browse files
committed
add commands and remove old code
1 parent b10bbd8 commit 3e1b734

File tree

17 files changed

+104
-533
lines changed

17 files changed

+104
-533
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.22.3
44

55
require (
66
github.com/fatih/color v1.18.0
7+
github.com/google/uuid v1.6.0
8+
github.com/matoous/go-nanoid/v2 v2.1.0
79
github.com/urfave/cli/v2 v2.27.5
810
)
911

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
22
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
46
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
7+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9+
github.com/matoous/go-nanoid/v2 v2.1.0 h1:P64+dmq21hhWdtvZfEAofnvJULaRR1Yib0+PnU669bE=
10+
github.com/matoous/go-nanoid/v2 v2.1.0/go.mod h1:KlbGNQ+FhrUNIHUxZdL63t7tl4LaPkZNpUULS8H4uVM=
511
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
612
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
713
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
814
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
915
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
16+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
17+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1018
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
1119
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
20+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
21+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1222
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
1323
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
1424
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
@@ -17,3 +27,5 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
1727
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1828
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
1929
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
30+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
31+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
app := &cli.App{
1313
Name: "random",
1414
Description: "CLI tool to generate random data",
15-
Commands: []*cli.Command{commands.StringCommand, commands.ShuffleCommand, commands.PickCommand},
15+
Commands: []*cli.Command{commands.StringCommand, commands.ShuffleCommand, commands.PickCommand, commands.IdCommand},
1616
}
1717

1818
// Run CLI

readme.md

Lines changed: 3 additions & 244 deletions
Original file line numberDiff line numberDiff line change
@@ -1,255 +1,14 @@
11
# Random Generator CLI
22

3-
<p>
4-
<!-- NPM version badge -->
5-
<a href="https://www.npmjs.com/package/random-generator-cli">
6-
<img src="https://img.shields.io/npm/v/random-generator-cli" alt="version"/>
7-
</a>
8-
9-
<!-- Github "Test Main" workflow status -->
10-
<a href="https://github.com/AmrSaber/random-cli/actions">
11-
<img src="https://github.com/AmrSaber/random-cli/workflows/Test%20Master/badge.svg" alt="Test Master Status"/>
12-
</a>
13-
14-
<!-- Github "Test Dev" workflow status -->
15-
<a href="https://github.com/AmrSaber/random-cli/actions">
16-
<img src="https://github.com/AmrSaber/random-cli/workflows/Test%20Dev/badge.svg" alt="Test Dev Status"/>
17-
</a>
18-
19-
<!-- NPM weekly downloads -->
20-
<a href="https://www.npmjs.com/package/random-generator-cli">
21-
<img src="https://img.shields.io/npm/dw/random-generator-cli" alt="weekly downloads"/>
22-
</a>
23-
24-
<!-- License -->
25-
<a href="https://github.com/AmrSaber/random-cli/blob/master/LICENSE">
26-
<img src="https://img.shields.io/npm/l/random-generator-cli" alt="license"/>
27-
</a>
28-
</p>
3+
> Old package published on npm has been removed since it was broken and I don't intend to fix it.
294
305
Generates and prints a secure random string (and other types) to terminal.
316

32-
> We use a cryptographically secure pseudo random number generator (CSPRNG, using `crypto`) for all the randomization and not `Math.random`.
33-
34-
## Install
35-
Install the package globally to be able to use it in the terminal.
36-
37-
```bash
38-
npm i -g random-generator-cli
39-
```
40-
41-
## Examples
42-
```
43-
$ random string
44-
LQQF5iTLVEawRGKqs4BN
45-
46-
$ random array
47-
3 7 10 1 2 4 6 8 5 9
48-
49-
$ random number 1 10
50-
9
51-
52-
$ random float
53-
0.24374674185057454423031231272034347057342529296875
54-
55-
$ random boolean
56-
true
57-
58-
$ random pick a b c d e
59-
d
60-
61-
$ random shuffle a b c d e
62-
b d a e c
63-
```
64-
657
## Usage
668
After installation, use `random <command>`, you can use `random <command> -h` to show help message related to that command, or use `random -h` to list all the available commands and options.
679

6810
### Commands
69-
Available commands are [`string`, `array`, `number`, `float`, `boolean`, `pick`, `shuffle`], each of theses commands will be explained next.
70-
71-
#### `string`
72-
Prints a random string with given length and type, also has the alias `str`.
73-
74-
String options:
75-
| Option | Alias | Type | Default | Description |
76-
|---------|-------|--------|-----------|---------------------------------|
77-
| length | l | Number | `20` | String length |
78-
| type | t | Number | `ascii` | String type (see table below) |
79-
80-
And the supported types are:
81-
| Type | Description | Example |
82-
|-----------------|------------------------------------------------------|----------------------|
83-
| ascii (default) | Uppercase letters, lowercase letters, and numbers | 8AlB9IUu7ptWqkp3tbeQ |
84-
| letters | Uppercase letters and lowercase letters | pPOzEvraFpVFJgCXPdXR |
85-
| numbers | Numbers (each character from 0 to 9) | 71110932458602457566 |
86-
| extended | Same as ascii plus the characters `+-_$#/@!` | fX#InRyripY@+f!Q7o7R |
87-
| base64 | Base 64 string | XU0tg2OW43GJCh5IrrX= |
88-
| hex | Hex number (with uppercase and lowercase characters) | 7bCCB51BEb9bDd61f6af |
89-
90-
Examples
91-
```
92-
$ random string -c 2
93-
vDboPtnabvsuEztusNAl
94-
YMasHVICXbbQxCLVMMuF
95-
96-
$ random string -l 15 -c 3 -t numbers
97-
366208018712690
98-
991841686515201
99-
575580372097167
100-
101-
$ random str -t extendedarray 15 --pad
102-
14 11 12 15 13 03 08 07 05 06 02 01 09 04 10
103-
zrk+MQP$_j5pWUWxBV!m
104-
```
105-
106-
#### `array [length]`
107-
Prints a shuffled array that starts at 1 by default, also accepts optional length argument that defaults to 10, and has the alias `arr`.
108-
109-
Note: the flag `-0` can be used to indicate that the array should start at 0.
110-
111-
Array options:
112-
| Option | Alias | Type | Default | Description |
113-
|-----------|-------|--------|-----------|--------------------------------------------------------------|
114-
| start | s | Number | `1` | Array starting position, overrides `-0` if both are provided |
115-
| end | e | Number | - | Array ending, overrides `length` |
116-
| delimiter | d | String | `' '` | The delimiter that is printed between array numbers |
117-
118-
Array flags:
119-
| Flag | Alias | Default | Description |
120-
|-------------|-------|-----------|----------------------------------------------------------------------|
121-
| startAtZero | 0 | `false` | Array elements start at 0, equivalent to `-s 0` |
122-
| pad | - | `false` | Add zero-padding to small number, so all number have the same length |
123-
124-
Examples:
125-
```
126-
$ random array
127-
3 9 2 6 4 1 10 7 8 5
128-
129-
$ random array 5
130-
4 1 5 3 2
131-
132-
$ random array 5 -0
133-
2 0 4 1 3
134-
135-
$ random arr 5 -s 5
136-
5 6 8 9 7
137-
138-
$ random arr -s 5 -e 10
139-
5 9 6 10 7 8
140-
141-
$ random arr 5 -d ', '
142-
4, 2, 5, 3, 1
143-
144-
$ random array 15 --pad
145-
14 11 12 15 13 03 08 07 05 06 02 01 09 04 10
146-
147-
$ random array 5 --pad
148-
1 2 5 4 3
149-
```
150-
151-
#### `number <min> <max>`
152-
Prints a random number between `min` and `max`, also has the aliases: [`num`, `integer`, `int`].
153-
154-
Example
155-
```
156-
$ random number 1 20
157-
17
158-
159-
$ random num 0 5
160-
3
161-
162-
$ random int 100 1000
163-
240
164-
```
165-
166-
#### `float`
167-
Prints a float number between 0 (inclusive) and 1 (exclusive).
168-
169-
Float options:
170-
| Option | Alias | Type | Default | Description |
171-
|-----------|-------|--------|-----------|----------------------------------------------------|
172-
| precision | p | Number | `10` | The desired precision of the floating point number |
173-
174-
Example
175-
```
176-
$ random float
177-
0.24374674185057454423031231272034347057342529296875
178-
179-
$ random float -p 5
180-
0.62194
181-
```
182-
183-
#### `boolean`
184-
Prints a random boolean, has the alias `bool`.
185-
186-
Boolean options:
187-
| Option | Alias | Type | Default | Description |
188-
|--------|-------|--------|--------------|---------------------------------------------------|
189-
| type | t | String | `true-false` | Boolean type, types are listed in the table below |
190-
191-
Boolean types:
192-
| Type | Possible values | Example |
193-
|----------------------|-------------------|---------|
194-
| true-false (default) | [`true`, `false`] | true |
195-
| yes-no | [`yes`, `no`] | no |
196-
| numeric | [`1`, `0`] | 1 |
197-
198-
Example
199-
```
200-
$ random boolean
201-
false
202-
203-
$ random boolean
204-
true
205-
```
206-
207-
#### `pick <items..>`
208-
Prints a random item(s) from the given items
209-
210-
Boolean options:
211-
| Option | Alias | Type | Default | Description |
212-
|-----------|-------|--------|---------|--------------------------------------------------|
213-
| number | n | Number | `1` | The number of items to chose, without repetition |
214-
| delimiter | d | String | `' '` | Delimiter that separates the chosen items |
215-
216-
Example
217-
```
218-
$ random pick a1 a2 a3 a4
219-
a3
220-
221-
$ random pick a1 a2 a3 a4 -n 2
222-
a4 a2
223-
224-
$ random pick a1 a2 a3 a4 -n 2 -d ', '
225-
a2, a3
226-
```
227-
228-
#### `shuffle <items..>`
229-
Shuffles the given array of items
230-
231-
Boolean options:
232-
| Option | Alias | Type | Default | Description |
233-
|-----------|-------|--------|---------|---------------------------------------------|
234-
| delimiter | d | String | `' '` | Delimiter that separates the shuffled items |
235-
236-
Example
237-
```
238-
$ random shuffle a1 a2 a3 a4
239-
a2 a3 a4 a1
240-
241-
$ random shuffle a1 a2 a3 a4 -d '|'
242-
a1|a4|a3|a2
243-
```
244-
245-
### Global Options
246-
247-
| Option | Alias | Type | Default | Description |
248-
|---------|-------|--------|---------|-----------------------------|
249-
| help | h | - | - | Show help message |
250-
| version | v | - | - | Show package version |
251-
| count | c | Number | 1 | Number of generated strings |
252-
11+
Available commands are [`string`, `pick`, `shuffle`, `id`].
25312

25413
## Feature requests and bug reports
255-
If you have a bug or have a goode idea for a new feature, please [open an issue](https://github.com/AmrSaber/rand-string/issues).
14+
If you have a bug or have a goode idea for a new feature, please [open an issue](https://github.com/AmrSaber/rand-string/issues).

src/commands/id.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"random/src/common"
6+
"slices"
7+
"strings"
8+
9+
"github.com/google/uuid"
10+
gonanoid "github.com/matoous/go-nanoid/v2"
11+
"github.com/urfave/cli/v2"
12+
)
13+
14+
var IdCommand = &cli.Command{
15+
Name: "id",
16+
Description: "Prints a random ID",
17+
18+
Flags: []cli.Flag{
19+
common.CountFlag,
20+
21+
&cli.StringFlag{
22+
Name: "type",
23+
Aliases: []string{"t"},
24+
Usage: fmt.Sprintf("Type of the id, must be one of [%s]", strings.Join(common.ID_TYPES, ", ")),
25+
Value: common.ID_TYPE_UUID4,
26+
Action: func(ctx *cli.Context, s string) error {
27+
if !slices.Contains(common.ID_TYPES, s) {
28+
return fmt.Errorf("invalid id type %q", s)
29+
}
30+
31+
return nil
32+
},
33+
},
34+
},
35+
36+
Action: func(ctx *cli.Context) error {
37+
count := ctx.Int("count")
38+
idType := ctx.String("type")
39+
40+
ids := make([]string, 0, count)
41+
42+
for range count {
43+
var id string
44+
var err error
45+
46+
if idType == common.ID_TYPE_UUID4 {
47+
id = uuid.New().String()
48+
}
49+
50+
if idType == common.ID_TYPE_UUID7 {
51+
var _id uuid.UUID
52+
_id, err = uuid.NewV7()
53+
54+
id = _id.String()
55+
56+
}
57+
58+
if idType == common.ID_TYPE_NANO {
59+
id, err = gonanoid.New()
60+
}
61+
62+
if err != nil {
63+
return fmt.Errorf("error generating nanoid: %w", err)
64+
}
65+
66+
ids = append(ids, id)
67+
}
68+
69+
// Print the generated random ids
70+
common.Std.Println(strings.Join(ids, "\n"))
71+
72+
return nil
73+
},
74+
}

src/common/constants.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ var (
3535

3636
const DEFAULT_DELIMITER = " "
3737
const DEFAULT_STRING_LENGTH = 20
38+
39+
const (
40+
ID_TYPE_UUID4 = "uuidv4"
41+
ID_TYPE_UUID7 = "uuidv7"
42+
ID_TYPE_NANO = "nanoid"
43+
)
44+
45+
var ID_TYPES = []string{
46+
ID_TYPE_UUID4,
47+
ID_TYPE_UUID7,
48+
ID_TYPE_NANO,
49+
}

0 commit comments

Comments
 (0)