diff --git a/.gitignore b/.gitignore index ba077a4..3a5ba85 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.DS_Store bin diff --git a/Makefile b/Makefile index f83cc6a..7757812 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,31 @@ -.PHONY: default test build install clean release - +.PHONY: default default: build +.PHONY: run run: - go run cmd/genpno/main.go + go run +.PHONY: test test: go test -v --race -cpu 2 -cover -shuffle on -vet '' ./... +.PHONY: build build: - go build -o bin/genpno cmd/genpno/main.go + go build -trimpath -o ./bin/pno +.PHONY: install install: - go install bin/genpno + install -b -S -v ./bin/pno ${GOPATH}/bin/. +.PHONY: clean clean: go mod tidy +.PHONY: release release: mkdir -p bin - CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o bin/genpno-macos-arm64 cmd/genpno/main.go - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/genpno-linux-amd64 cmd/genpno/main.go - CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -o bin/genpno-linux-386 cmd/genpno/main.go - CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o bin/genpno-windows-amd64.exe cmd/genpno/main.go - CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -o bin/genpno-windows-386.exe cmd/genpno/main.go + CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o bin/pno-macos-arm64 + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/pno-linux-amd64 + CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -o bin/pno-linux-386 + CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o bin/pno-windows-amd64.exe + CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -o bin/pno-windows-386.exe diff --git a/README.md b/README.md index 9a06b6d..c55a356 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,16 @@ a) Download the binary from the releases page or b) Build the binary from source: ```bash -go install github.com/dominikwinter/genpno/cmd/genpno@latest +go install github.com/dominikwinter/pno@latest ``` You can now run it with some options: ```bash -genpno -h +pno gen -h ``` Which will list possible options: ``` -Usage of genpno: +Usage of pno gen: -c string Country code (default random) -d string @@ -39,13 +39,13 @@ Usage of genpno: 1. Clone the repository: ```bash -git clone https://github.com/dominikwinter/genpno.git +git clone https://github.com/dominikwinter/pno.git ``` 2. Navigate to the project directory: ```bash -cd genpno +cd pno ``` 3. Build and install the application: @@ -58,7 +58,7 @@ make install 4. Run the application: ```bash -genpno +pno ``` ## How to Contribute diff --git a/cmd/gen.go b/cmd/gen.go new file mode 100644 index 0000000..d528fe9 --- /dev/null +++ b/cmd/gen.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "flag" + "fmt" + + "github.com/dominikwinter/pno/internal/gen" +) + +func Run(args []string) { + fs := flag.NewFlagSet("pno gen", flag.ExitOnError) + + helpFlag := fs.Bool("h", false, "Help") + verboseFlag := fs.Bool("v", false, "Verbose") + + dateFlag := fs.String("d", "", "Date (format: yyyymmdd) (default random)") + countryFlag := fs.String("c", "", "Country code (default random)") + genderFlag := fs.String("g", "", "Gender (m/f) (default random)") + formatFlag := fs.String("f", "", `Output forma (default 3): + 1. yymmddccgn + 2. yymmdd-ccgn + 3. yyyymmddccgn + 4. yyyymmdd-ccgn`) + + fs.Parse(args) + + if *helpFlag { + fs.Usage() + return + } + + pno, err := gen.GeneratePno(*dateFlag, *countryFlag, *genderFlag, *formatFlag) + + if err != nil { + if *verboseFlag { + panic(err) + } else { + fmt.Println(err) + return + } + } + + fmt.Println(pno) +} diff --git a/cmd/genpno/main.go b/cmd/genpno/main.go deleted file mode 100644 index 5cc46a6..0000000 --- a/cmd/genpno/main.go +++ /dev/null @@ -1,42 +0,0 @@ -package main - -import ( - "flag" - "fmt" - - "github.com/dominikwinter/genpno/internal/generator" -) - -func main() { - helpFlag := flag.Bool("h", false, "Help") - verboseFlag := flag.Bool("v", false, "Verbose") - - dateFlag := flag.String("d", "", "Date (format: yyyymmdd) (default random)") - countryFlag := flag.String("c", "", "Country code (default random)") - genderFlag := flag.String("g", "", "Gender (m/f) (default random)") - formatFlag := flag.String("f", "", `Output forma (default 3): - 1. yymmddccgn - 2. yymmdd-ccgn - 3. yyyymmddccgn - 4. yyyymmdd-ccgn`) - - flag.Parse() - - if *helpFlag { - flag.Usage() - return - } - - pno, err := generator.GeneratePno(*dateFlag, *countryFlag, *genderFlag, *formatFlag) - - if err != nil { - if *verboseFlag { - panic(err) - } else { - fmt.Println(err) - return - } - } - - fmt.Println(pno) -} diff --git a/go.mod b/go.mod index b468047..948e07a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/dominikwinter/genpno +module github.com/dominikwinter/pno go 1.21.6 diff --git a/internal/generator/checksum.go b/internal/gen/checksum.go similarity index 97% rename from internal/generator/checksum.go rename to internal/gen/checksum.go index 6db8511..144aa4a 100644 --- a/internal/generator/checksum.go +++ b/internal/gen/checksum.go @@ -1,4 +1,4 @@ -package generator +package gen import ( "fmt" diff --git a/internal/generator/country.go b/internal/gen/country.go similarity index 96% rename from internal/generator/country.go rename to internal/gen/country.go index 3cb49a1..edc7a8b 100644 --- a/internal/generator/country.go +++ b/internal/gen/country.go @@ -1,4 +1,4 @@ -package generator +package gen import ( "fmt" diff --git a/internal/generator/date.go b/internal/gen/date.go similarity index 96% rename from internal/generator/date.go rename to internal/gen/date.go index 87708e7..356eb3a 100644 --- a/internal/generator/date.go +++ b/internal/gen/date.go @@ -1,4 +1,4 @@ -package generator +package gen import ( "fmt" diff --git a/internal/generator/format.go b/internal/gen/format.go similarity index 96% rename from internal/generator/format.go rename to internal/gen/format.go index 3b4e9c9..401622e 100644 --- a/internal/generator/format.go +++ b/internal/gen/format.go @@ -1,4 +1,4 @@ -package generator +package gen import "fmt" diff --git a/internal/generator/gender.go b/internal/gen/gender.go similarity index 97% rename from internal/generator/gender.go rename to internal/gen/gender.go index 61d04f6..5a0a035 100644 --- a/internal/generator/gender.go +++ b/internal/gen/gender.go @@ -1,4 +1,4 @@ -package generator +package gen import ( "fmt" diff --git a/internal/generator/generate.go b/internal/gen/generate.go similarity index 98% rename from internal/generator/generate.go rename to internal/gen/generate.go index 88c5299..5d67a5e 100644 --- a/internal/generator/generate.go +++ b/internal/gen/generate.go @@ -1,4 +1,4 @@ -package generator +package gen import "fmt" diff --git a/internal/generator/generate_test.go b/internal/gen/generate_test.go similarity index 98% rename from internal/generator/generate_test.go rename to internal/gen/generate_test.go index 9cd0045..83a8fdb 100644 --- a/internal/generator/generate_test.go +++ b/internal/gen/generate_test.go @@ -1,4 +1,4 @@ -package generator +package gen import ( "strconv" diff --git a/main.go b/main.go new file mode 100644 index 0000000..fa35831 --- /dev/null +++ b/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "os" + + "github.com/dominikwinter/pno/cmd" +) + +func main() { + const usage = `Usage of pno []: + + commands: + gen Generate a personal number + + args: + -h Help` + + if len(os.Args) < 2 { + fmt.Fprintln(os.Stderr, usage) + + os.Exit(1) + } + + command, args := os.Args[1], os.Args[2:] + + switch command { + case "gen": + cmd.Run(args) + default: + fmt.Fprintf(os.Stderr, "Unrecognized command %s.\n\n%s\n", command, usage) + os.Exit(1) + } +}