Skip to content

Commit

Permalink
Add Location as a field of YMDFlag (#5)
Browse files Browse the repository at this point in the history
* Add Location as a field of YMDFlag, refactor with public helpers

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* Add examples and Taskfile

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* More interface refinement

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* Remove Location field and simplify interface, limiting where auto-date happens

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* add yyyymmdd validation tests

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* moar tests

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* Trying things out

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* Move to newer test action

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* add clarifying comment

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* fix linter issue

Signed-off-by: Kevin Miller <kevin@neomantra.net>

* More action cleanup

* task update also updates tests

---------

Signed-off-by: Kevin Miller <kevin@neomantra.net>
Co-authored-by: Kevin Miller <kevin@neomantra.net>
Signed-off-by: Kevin Miller <kevin@neomantra.net>
  • Loading branch information
neomantra and Kevin Miller committed Aug 21, 2023
1 parent afee0ea commit 1f21db8
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 112 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
contents: read # for actions/checkout to fetch code
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
steps:
- uses: actions/checkout@7884fcad6b5d53d10323aee724dc68d8b9096a2e
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5
uses: golangci/golangci-lint-action@v3
with:
only-new-issues: true
50 changes: 24 additions & 26 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
name: Units tests
on: [push, pull_request]
name: Build/Test Package

on: [push]

jobs:
checks:
name: test
build:

runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21' ]

steps:
- uses: actions/checkout@61b9e3751b92087fd0b06925ba6dd6314e06f089
- name: Setup go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753
with:
go-version: 1.20
- uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run tests
run: go test -v -covermode=count -coverprofile=coverage.out
- name: Convert coverage to lcov
uses: jandelgado/gcov2lcov-action@c680c0f7c7442485f1749eb2a13e54a686e76eb5
- name: Coveralls
uses: coverallsapp/github-action@f350da2c033043742f89e8c0b7b5145a1616da6d
with:
github-token: ${{ secrets.github_token }}
path-to-lcov: coverage.lcov
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install dependencies
run: go get .
- name: Test with Go
run: go test -json > TestResults-${{ matrix.go-version }}.json
- name: Upload Go test results
uses: actions/upload-artifact@v3
with:
name: Go-results-${{ matrix.go-version }}
path: TestResults-${{ matrix.go-version }}.json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
.task
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ go get github.com/neomantra/ymdflag

https://pkg.go.dev/github.com/neomantra/ymdflag

## Examples ##

There are examples in the [`examples/` directory](./examples). Here's a quick sketch:

```go
package main

import (
"github.com/neomantra/ymdflag"
"github.com/spf13/pflag"
)

func main() {
var ymd ymdflag.YMDFlag
pflag.VarP(&ymd, "date", "d", "YYYYMMDD date; defaults to today in local time")
pflag.Parse()
println("time of date:", ymd.AsTime().String())
}
```
----

## Credits and License
Expand Down
54 changes: 54 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: '3'

tasks:
default:
deps:
- test
- build

tidy:
cmds:
- go mod tidy

update:
cmds:
- go get -u -t ./...

clean:
cmds:
- rm -f bin/*

test:
cmds:
- go test -v
sources:
- "**/*.go"

build:
deps:
- build:examples

build:examples:
deps:
- build:pflag-simple
- build:pflag-start-end

build:pflag-simple:
deps: [tidy]
cmds:
- go build -o bin/pflag-simple examples/pflag-simple/main.go
sources:
- examples/pflag-simple/main.go
- "*.go"
generates:
- bin/plfag-simple

build:pflag-start-end:
deps: [tidy]
cmds:
- go build -o bin/plfag-start-end examples/pflag-start-end/main.go
sources:
- examples/pflag-start-end/main.go
- "*.go"
generates:
- bin/plfag-start-end
15 changes: 15 additions & 0 deletions examples/pflag-simple/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2023 Neomantra BV

package main

import (
"github.com/neomantra/ymdflag"
"github.com/spf13/pflag"
)

func main() {
var ymd ymdflag.YMDFlag
pflag.VarP(&ymd, "date", "d", "YYYYMMDD date; defaults to today in local time")
pflag.Parse()
println("time of date:", ymd.AsTime().String())
}
5 changes: 5 additions & 0 deletions examples/pflag-start-end/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `pflag-start-end` example

A common idiom is to have start and end dates to a program.

This example shows how to use [`YMDFlag`](../../README.md) and [`pflag`](https://pkg.go.dev/github.com/spf13/pflag) to handle this.
38 changes: 38 additions & 0 deletions examples/pflag-start-end/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023 Neomantra BV

package main

import (
"fmt"
"os"
"time"

"github.com/neomantra/ymdflag"
"github.com/spf13/pflag"
)

func main() {
var startDate ymdflag.YMDFlag
var endDate ymdflag.YMDFlag

pflag.VarP(&startDate, "start", "s", "YYYYMMDD start date; defaults to end date")
pflag.VarP(&endDate, "end", "e", "YYYYMMDD end date; defaults to today (local time)")
pflag.Parse()

// set up start/end times
if startDate.IsZero() {
// if startDate is not set, default to endDate
startDate = endDate
}

var startTime, endTime time.Time
if st, et := startDate.AsTime(), endDate.AsTime(); et.Equal(st) || et.After(st) {
startTime = time.Date(st.Year(), st.Month(), st.Day(), 0, 0, 0, 0, time.UTC)
endTime = time.Date(et.Year(), et.Month(), et.Day(), 23, 59, 59, 0, time.UTC)
} else {
fmt.Fprint(os.Stderr, "--start must be before --end\n")
os.Exit(1)
}

fmt.Fprintf(os.Stdout, "startTime: %s endTime: %s\n", startTime.String(), endTime.String())
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/neomantra/ymdflag

go 1.20

require github.com/stretchr/testify v1.8.4
require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
Loading

0 comments on commit 1f21db8

Please sign in to comment.