Skip to content

Commit

Permalink
Merge pull request #62 from kozmod/feature/61_random.ASCII
Browse files Browse the repository at this point in the history
[#61] add `{{ random.ASCII 8 }}` for templates
  • Loading branch information
kozmod committed Feb 16, 2023
2 parents 92a478e + 2ad9a2c commit c0c6db9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,11 @@ dirs:
#### Custom template functions
| Function | args | Description |
|:------------------|:------------:|:------------------------------------------------------------------------------|
| `random.Alpha` | length `int` | Generates a random alphabetical `(A-Z, a-z)` string of a desired length. |
| `random.AlphaNum` | length `int` | Generates a random alphanumeric `(0-9, A-Z, a-z)` string of a desired length. |
| Function | args | Description |
|:------------------|:------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `random.Alpha` | length `int` | Generates a random alphabetical `(A-Z, a-z)` string of a desired length. |
| `random.AlphaNum` | length `int` | Generates a random alphanumeric `(0-9, A-Z, a-z)` string of a desired length. |
| `random.ASCII` | length `int` | Generates a random string of a desired length, containing the set of printable characters from the 7-bit ASCII set. This includes space (’ ‘), but no other whitespace character. |

Custom template functions adds the elements of the argument map to the
template's [function map]](https://pkg.go.dev/text/template#hdr-Functions).
Expand Down Expand Up @@ -495,6 +496,7 @@ out:

2 directories, 6 files
```

`cmd` action maintains "short" declaration syntax

```yaml
Expand All @@ -504,6 +506,7 @@ cmd:
- pwd
- ls -a
```
```console
% progen -v -dr
2023-02-15 17:56:58 INFO application working directory: /Users/user_1/GoProjects/progen
Expand All @@ -512,7 +515,6 @@ cmd:
2023-02-15 17:56:58 INFO execute [dir: .]: ls -a
```
#### <a name="cmd_pipe"></a> 'Pipe mode'
The `cmd.pipe` option set "pipe mode" for `cmd.exec` section:
Expand Down
1 change: 1 addition & 0 deletions internal/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
EqualsSign = "="
LessThan = "<"
SpacedPipe = " | "
Tilda = "~"

NewLine = '\n'
)
Expand Down
11 changes: 11 additions & 0 deletions internal/entity/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ func Test_RandomFn(t *testing.T) {
assert.Regexp(t, "^[[:alnum:]]*$", s)
})
})
t.Run("ASCII", func(t *testing.T) {
t.Run("generate_zero", func(t *testing.T) {
s := f.ASCII(0)
assert.Empty(t, s)
})
t.Run("generate_50", func(t *testing.T) {
s := f.ASCII(50)
assert.Len(t, s, 50)
assert.Regexp(t, "^[[:print:]]*$", s)
})
})
}

func Test_MissingKye(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions internal/entity/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package entity
import (
"hash/maphash"
"math/rand"
"unicode"
)

// Default set, matches "[a-zA-Z0-9_.-]"
Expand All @@ -16,13 +17,30 @@ const (
)

var (
_lettersASCII string

mapHashSrc = rand.New(rand.NewSource(int64(new(maphash.Hash).Sum64())))

TemplateFnsMap = map[string]any{
"random": func() any { return RandomFn{} },
}
)

func init() {
var (
lower = int32(Space[0])
upper = int32(Tilda[0])
)

chars := make([]rune, 0, int(upper)-int(lower))
for r := lower; r <= upper; r++ {
if unicode.IsGraphic(r) {
chars = append(chars, r)
}
}
_lettersASCII = string(chars)
}

// RandomFn has to generate random string value
type RandomFn struct{}

Expand All @@ -36,6 +54,12 @@ func (RandomFn) AlphaNum(n int) string {
return randomString(n, _lettersAlphaNum)
}

// ASCII Generates a random string of a desired length, containing the set of printable characters from the 7-bit ASCII set.
// This includes space (’ ‘), but no other whitespace character
func (RandomFn) ASCII(n int) string {
return randomString(n, _lettersASCII)
}

func randomString(n int, set string) string {
src := mapHashSrc
b := make([]byte, n)
Expand Down

0 comments on commit c0c6db9

Please sign in to comment.