mirror of
https://github.com/rjNemo/go-pass-gen
synced 2026-06-12 13:46:49 +00:00
🎨 improv folder structure
This commit is contained in:
parent
1458d55d32
commit
e28df4b158
11 changed files with 67 additions and 66 deletions
19
Makefile
19
Makefile
|
|
@ -1,4 +1,5 @@
|
||||||
EXECPATH = ./build/dist
|
EXEC_PATH = ./build/dist
|
||||||
|
BUILD_CMD = go build -o $(EXEC_PATH) -ldflags="-s -w"
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
golangci-lint run
|
golangci-lint run
|
||||||
|
|
@ -7,13 +8,16 @@ dev:
|
||||||
air
|
air
|
||||||
|
|
||||||
build:
|
build:
|
||||||
go build -ldflags="-s -w" -o $(EXECPATH) .
|
$(BUILD_CMD) ./cmd/cli
|
||||||
|
|
||||||
run: build
|
run: build
|
||||||
$(EXECPATH) new
|
$(EXEC_PATH) new
|
||||||
|
|
||||||
run-web:
|
build-web:
|
||||||
go run main.go --web=t
|
$(BUILD_CMD) ./cmd/server
|
||||||
|
|
||||||
|
run-web: build-web
|
||||||
|
$(EXEC_PATH)
|
||||||
|
|
||||||
web:
|
web:
|
||||||
cd client && npm run start
|
cd client && npm run start
|
||||||
|
|
@ -21,4 +25,7 @@ web:
|
||||||
test:
|
test:
|
||||||
go test -json -count=1 ./... -coverpkg=./... -coverprofile coverage.out -covermode=atomic | gotestfmt && go tool cover -html coverage.out && rm coverage.out
|
go test -json -count=1 ./... -coverpkg=./... -coverprofile coverage.out -covermode=atomic | gotestfmt && go tool cover -html coverage.out && rm coverage.out
|
||||||
|
|
||||||
.PHONY: lint run dev run-web test build
|
clean:
|
||||||
|
rm -rf ./build/
|
||||||
|
|
||||||
|
.PHONY: lint run dev run-web test build build-web clean web
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,11 @@ type Server struct {
|
||||||
Router *chi.Mux
|
Router *chi.Mux
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() *Server {
|
func New() *Server {
|
||||||
s := &Server{Router: chi.NewRouter()}
|
s := &Server{Router: chi.NewRouter()}
|
||||||
s.Router.Use(middleware.Logger)
|
s.Router.Use(middleware.Logger)
|
||||||
s.Router.Use(cors.Handler(cors.Options{
|
s.Router.Use(cors.Handler(cors.Options{
|
||||||
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
|
AllowedOrigins: []string{"https://*", "http://*"},
|
||||||
AllowedOrigins: []string{"https://*", "http://*"},
|
|
||||||
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
|
|
||||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||||
ExposedHeaders: []string{"Link"},
|
ExposedHeaders: []string{"Link"},
|
||||||
|
|
@ -29,6 +27,7 @@ func NewServer() *Server {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServeHTTP implements Handler for Server
|
||||||
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
s.Router.ServeHTTP(w, r)
|
s.Router.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package cmd
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -9,9 +9,16 @@ import (
|
||||||
"github.com/rjNemo/go-pass-gen/passgen"
|
"github.com/rjNemo/go-pass-gen/passgen"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// length of the generated password.
|
||||||
|
length int
|
||||||
|
// withNumbers is set to true if the new password must contain numbers.
|
||||||
|
withNumbers bool
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
newPasswordCommand.Flags().IntVarP(&Length, "length", "l", 6, "password length")
|
newPasswordCommand.Flags().IntVarP(&length, "length", "l", 6, "password length")
|
||||||
newPasswordCommand.Flags().BoolVarP(&WithNumbers, "numbers", "n", false, "password should contain numbers")
|
newPasswordCommand.Flags().BoolVarP(&withNumbers, "numbers", "n", false, "password should contain numbers")
|
||||||
rootCommand.AddCommand(newPasswordCommand)
|
rootCommand.AddCommand(newPasswordCommand)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,7 +27,7 @@ var newPasswordCommand = &cobra.Command{Use: "new",
|
||||||
Short: "New Password",
|
Short: "New Password",
|
||||||
Long: "Create a secure password",
|
Long: "Create a secure password",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
opts := passgen.Options{Length: Length, WithNumbers: WithNumbers}
|
opts := passgen.Options{Length: length, WithNumbers: withNumbers}
|
||||||
pg := passgen.New(opts.SetDefaults())
|
pg := passgen.New(opts.SetDefaults())
|
||||||
password := pg.NewPassword()
|
password := pg.NewPassword()
|
||||||
display(password)
|
display(password)
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
// Package cmd defines the command-line interface to passgen
|
// Package app defines the command-line interface to passgen
|
||||||
package cmd
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
9
cmd/cli/main.go
Normal file
9
cmd/cli/main.go
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/rjNemo/go-pass-gen/app"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app.Execute()
|
||||||
|
}
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
package cmd
|
|
||||||
|
|
||||||
var (
|
|
||||||
// Length of the generated password.
|
|
||||||
Length int
|
|
||||||
// WithNumbers is set to true if the new password must contain numbers.
|
|
||||||
WithNumbers bool
|
|
||||||
)
|
|
||||||
14
cmd/server/main.go
Normal file
14
cmd/server/main.go
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/rjNemo/go-pass-gen/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
const port = ":8080"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Printf("Start passgen server on http://localhost%s\n", port)
|
||||||
|
log.Fatal(api.New().Start(port))
|
||||||
|
}
|
||||||
28
main.go
28
main.go
|
|
@ -1,28 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/rjNemo/go-pass-gen/api"
|
|
||||||
"github.com/rjNemo/go-pass-gen/cmd"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
web := flag.Bool("web", false, "")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if *web {
|
|
||||||
serveWeb()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cli()
|
|
||||||
}
|
|
||||||
|
|
||||||
func cli() {
|
|
||||||
cmd.Execute()
|
|
||||||
}
|
|
||||||
|
|
||||||
func serveWeb() {
|
|
||||||
log.Fatal(api.NewServer().Start(":8080"))
|
|
||||||
}
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
package passgen
|
|
||||||
|
|
||||||
const (
|
|
||||||
// LOWERCASE characters if latin alphabet
|
|
||||||
LOWERCASE string = "abcdefghijklmnopqrstuvwxyz"
|
|
||||||
// UPPERCASE characters if latin alphabet
|
|
||||||
UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
// NUMBERS arabic
|
|
||||||
NUMBERS = "0123456789"
|
|
||||||
)
|
|
||||||
|
|
@ -5,6 +5,17 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// lowercase characters in latin alphabet
|
||||||
|
lowercase string = "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
|
||||||
|
// uppercase characters in latin alphabet
|
||||||
|
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
|
// numbers arabic
|
||||||
|
numbers = "0123456789"
|
||||||
|
)
|
||||||
|
|
||||||
// PasswordGenerator handles passwords creation.
|
// PasswordGenerator handles passwords creation.
|
||||||
type PasswordGenerator struct {
|
type PasswordGenerator struct {
|
||||||
characters []rune
|
characters []rune
|
||||||
|
|
@ -35,9 +46,9 @@ func (p PasswordGenerator) generatePassword(length int) string {
|
||||||
|
|
||||||
// shuffleCharacters randomizes the characters.
|
// shuffleCharacters randomizes the characters.
|
||||||
func (p PasswordGenerator) shuffleCharacters(withNumbers bool) []rune {
|
func (p PasswordGenerator) shuffleCharacters(withNumbers bool) []rune {
|
||||||
letters := []rune(UPPERCASE + LOWERCASE)
|
letters := []rune(uppercase + lowercase)
|
||||||
if withNumbers {
|
if withNumbers {
|
||||||
letters = append(letters, []rune(NUMBERS)...)
|
letters = append(letters, []rune(numbers)...)
|
||||||
}
|
}
|
||||||
rand.Shuffle(len(letters), func(i, j int) { letters[i], letters[j] = letters[j], letters[i] })
|
rand.Shuffle(len(letters), func(i, j int) { letters[i], letters[j] = letters[j], letters[i] })
|
||||||
return letters
|
return letters
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,10 @@ func TestGeneratePasswordWithLettersAndNumbers(t *testing.T) {
|
||||||
pg := passgen.New(opts)
|
pg := passgen.New(opts)
|
||||||
|
|
||||||
if password := pg.NewPassword(); !containNumbers(password) {
|
if password := pg.NewPassword(); !containNumbers(password) {
|
||||||
t.Errorf("Expected password to contain NUMBERS, got %q", password)
|
t.Errorf("Expected password to contain numbers, got %q", password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func containNumbers(str string) bool {
|
func containNumbers(str string) bool {
|
||||||
return strings.ContainsAny(str, passgen.NUMBERS)
|
return strings.ContainsAny(str, "0123456789")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue