Skip to content

Commit

Permalink
Merge pull request #7 from dominikwinter/6-support-for-subcommands-in…
Browse files Browse the repository at this point in the history
…-cli

add support for subcommands
  • Loading branch information
dominikwinter authored Jan 28, 2024
2 parents 90aad14 + bee5a05 commit 4e13521
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
bin
25 changes: 15 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -58,7 +58,7 @@ make install
4. Run the application:

```bash
genpno
pno
```

## How to Contribute
Expand Down
44 changes: 44 additions & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -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)
}
42 changes: 0 additions & 42 deletions cmd/genpno/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/dominikwinter/genpno
module github.com/dominikwinter/pno

go 1.21.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package gen

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/generator/country.go → internal/gen/country.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package gen

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/generator/date.go → internal/gen/date.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package gen

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/generator/format.go → internal/gen/format.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package gen

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion internal/generator/gender.go → internal/gen/gender.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package gen

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package gen

import "fmt"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generator
package gen

import (
"strconv"
Expand Down
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"os"

"github.com/dominikwinter/pno/cmd"
)

func main() {
const usage = `Usage of pno <command> [<args>]:
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)
}
}

0 comments on commit 4e13521

Please sign in to comment.