Skip to content

Commit 62bcdac

Browse files
committed
feat: ✨ initial commit
0 parents  commit 62bcdac

File tree

12 files changed

+532
-0
lines changed

12 files changed

+532
-0
lines changed

.github/dependabot.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 2
2+
3+
updates:
4+
- package-ecosystem: github-actions
5+
directory: /
6+
schedule:
7+
interval: monthly

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
ci:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Setup Go
17+
uses: actions/setup-go@v3
18+
19+
- name: build
20+
run: go build .
21+
22+
- name: lint
23+
uses: golangci/[email protected]
24+
with:
25+
version: latest
26+
27+
- name: test
28+
run: go test -v ./...

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
.DS_Store
8+
9+
# Test binary, built with `go test -c`
10+
*.test
11+
12+
# Output of the go coverage tool, specifically when used with LiteIDE
13+
*.out
14+
15+
# Dependency directories (remove the comment below to include it)
16+
vendor/

.gitlint

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Edit this file as you like.
2+
#
3+
# All these sections are optional. Each section with the exception of [general] represents
4+
# one rule and each key in it is an option for that specific rule.
5+
#
6+
# Rules and sections can be referenced by their full name or by id. For example
7+
# section "[body-max-line-length]" could also be written as "[B1]". Full section names are
8+
# used in here for clarity.
9+
#
10+
[general]
11+
# Ignore certain rules, this example uses both full name and id
12+
# ignore=title-trailing-punctuation, T3
13+
14+
# verbosity should be a value between 1 and 3, the commandline -v flags take precedence over this
15+
# verbosity = 2
16+
17+
# By default gitlint will ignore merge, revert, fixup and squash commits.
18+
# ignore-merge-commits=true
19+
# ignore-revert-commits=true
20+
# ignore-fixup-commits=true
21+
# ignore-squash-commits=true
22+
23+
# Ignore any data send to gitlint via stdin
24+
# ignore-stdin=true
25+
26+
# Fetch additional meta-data from the local repository when manually passing a
27+
# commit message to gitlint via stdin or --commit-msg. Disabled by default.
28+
# staged=true
29+
30+
# Enable debug mode (prints more output). Disabled by default.
31+
# debug=true
32+
33+
# Enable community contributed rules
34+
# See http://jorisroovers.github.io/gitlint/contrib_rules for details
35+
contrib=contrib-title-conventional-commits
36+
37+
# Set the extra-path where gitlint will search for user defined rules
38+
# See http://jorisroovers.github.io/gitlint/user_defined_rules for details
39+
# extra-path=examples/
40+
41+
# This is an example of how to configure the "title-max-length" rule and
42+
# set the line-length it enforces to 80
43+
# [title-max-length]
44+
# line-length=50
45+
46+
# Conversely, you can also enforce minimal length of a title with the
47+
# "title-min-length" rule:
48+
# [title-min-length]
49+
# min-length=5
50+
51+
# [title-must-not-contain-word]
52+
# Comma-separated list of words that should not occur in the title. Matching is case
53+
# insensitive. It's fine if the keyword occurs as part of a larger word (so "WIPING"
54+
# will not cause a violation, but "WIP: my title" will.
55+
# words=wip
56+
57+
# [title-match-regex]
58+
# python-style regex that the commit-msg title must match
59+
# Note that the regex can contradict with other rules if not used correctly
60+
# (e.g. title-must-not-contain-word).
61+
# regex=^US[0-9]*
62+
63+
# [body-max-line-length]
64+
# line-length=72
65+
66+
# [body-min-length]
67+
# min-length=5
68+
69+
#[body-is-missing]
70+
# Whether to ignore this rule on merge commits (which typically only have a title)
71+
# default = True
72+
#ignore-merge-commits=false
73+
74+
# [body-changed-file-mention]
75+
# List of files that need to be explicitly mentioned in the body when they are changed
76+
# This is useful for when developers often erroneously edit certain files or git submodules.
77+
# By specifying this rule, developers can only change the file when they explicitly reference
78+
# it in the commit message.
79+
# files=gitlint/rules.py,README.md
80+
81+
# [body-match-regex]
82+
# python-style regex that the commit-msg body must match.
83+
# E.g. body must end in My-Commit-Tag: foo
84+
# regex=My-Commit-Tag: foo$
85+
86+
# [author-valid-email]
87+
# python-style regex that the commit author email address must match.
88+
# For example, use the following regex if you only want to allow email addresses from foo.com
89+
# regex=[^@][email protected]
90+
91+
# [ignore-by-title]
92+
# Ignore certain rules for commits of which the title matches a regex
93+
# E.g. Match commit titles that start with "Release"
94+
# regex=^Release(.*)
95+
96+
# Ignore certain rules, you can reference them by their id or by their full name
97+
# Use 'all' to ignore all rules
98+
ignore=B6,CC1
99+
100+
# [ignore-by-body]
101+
# Ignore certain rules for commits of which the body has a line that matches a regex
102+
# E.g. Match bodies that have a line that that contain "release"
103+
# regex=(.*)release(.*)
104+
#
105+
# Ignore certain rules, you can reference them by their id or by their full name
106+
# Use 'all' to ignore all rules
107+
# ignore=T1,body-min-length
108+
109+
# [ignore-body-lines]
110+
# Ignore certain lines in a commit body that match a regex.
111+
# E.g. Ignore all lines that start with 'Co-Authored-By'
112+
# regex=^Co-Authored-By
113+
114+
# This is a contrib rule - a community contributed rule. These are disabled by default.
115+
# You need to explicitly enable them one-by-one by adding them to the "contrib" option
116+
# under [general] section above.
117+
# [contrib-title-conventional-commits]
118+
# Specify allowed commit types. For details see: https://www.conventionalcommits.org/
119+
#types = feat,fix,refactor,docs,ci,chore

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
repos:
2+
- repo: git://github.com/dnephin/pre-commit-golang
3+
rev: v0.4.0
4+
hooks:
5+
- id: go-fmt
6+
- id: go-vet
7+
- id: go-lint
8+
- id: go-imports
9+
- id: go-mod-tidy
10+
- repo: https://github.com/jorisroovers/gitlint
11+
rev: v0.15.1
12+
hooks:
13+
- id: gitlint

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Ben
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

cmd/cli.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strconv"
8+
9+
"github.com/iwpnd/pw"
10+
"github.com/urfave/cli/v2"
11+
)
12+
13+
func main() {
14+
app := &cli.App{
15+
Name: "pw",
16+
Usage: "create a secure password from the command line",
17+
Action: func(ctx *cli.Context) error {
18+
l := ctx.Args().Get(0)
19+
v, err := strconv.Atoi(l)
20+
if err != nil {
21+
panic("AAH!")
22+
}
23+
fmt.Println(pw.NewPassword(v))
24+
return nil
25+
},
26+
}
27+
28+
if err := app.Run(os.Args); err != nil {
29+
log.Fatal(err)
30+
}
31+
}

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/iwpnd/pw
2+
3+
go 1.19
4+
5+
require github.com/urfave/cli/v2 v2.11.2
6+
7+
require (
8+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
9+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
10+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
11+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
4+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/urfave/cli/v2 v2.11.2 h1:FVfNg4m3vbjbBpLYxW//WjxUoHvJ9TlppXcqY9Q9ZfA=
6+
github.com/urfave/cli/v2 v2.11.2/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
7+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
8+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=

pw.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package pw
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
// UpperChars ...
9+
const UpperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10+
11+
// LowerChars ...
12+
const LowerChars = "abcdefghijklmnopqrstuvwxyz"
13+
14+
// SpecialChars ...
15+
const SpecialChars = "!@#$%^&*()-_=+,.?/:;{}[]~"
16+
17+
// NumberChars ...
18+
const NumberChars = "0123456789"
19+
20+
// AllChars ...
21+
const AllChars = UpperChars + LowerChars + SpecialChars + NumberChars
22+
23+
// Option type
24+
type Option func() string
25+
26+
func randomFromChars(length int, chars string) string {
27+
rand.Seed(time.Now().UnixNano())
28+
min := 0
29+
max := len(chars) - 1
30+
31+
p := ""
32+
for i := 0; i < length; i++ {
33+
n := min + rand.Intn(max-min+1)
34+
p = p + string(chars[n])
35+
}
36+
37+
return p
38+
}
39+
40+
// WithUpperCase option to use upper case characters in the password
41+
func WithUpperCase() Option {
42+
return func() string {
43+
return UpperChars
44+
}
45+
}
46+
47+
// WithLowerCase option to use lower case characters in the password
48+
func WithLowerCase() Option {
49+
return func() string {
50+
return LowerChars
51+
}
52+
}
53+
54+
// WithNumbers option to use number characters in the password
55+
func WithNumbers() Option {
56+
return func() string {
57+
return NumberChars
58+
}
59+
}
60+
61+
// WithSpecial option to use special characters in the password
62+
func WithSpecial() Option {
63+
return func() string {
64+
return SpecialChars
65+
}
66+
}
67+
68+
// NewPassword to create a new password with length. Defaults characters to include
69+
// all characters. Options can be passed to customize the password.
70+
func NewPassword(length int, options ...Option) string {
71+
if len(options) != 0 {
72+
c := ""
73+
74+
for _, option := range options {
75+
c = c + option()
76+
}
77+
78+
return randomFromChars(length, c)
79+
}
80+
81+
return randomFromChars(length, AllChars)
82+
}

0 commit comments

Comments
 (0)