-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Location as a field of YMDFlag (#5)
* Add Location as a field of YMDFlag, refactor with public helpers Signed-off-by: Kevin Miller <[email protected]> * Add examples and Taskfile Signed-off-by: Kevin Miller <[email protected]> * More interface refinement Signed-off-by: Kevin Miller <[email protected]> * Remove Location field and simplify interface, limiting where auto-date happens Signed-off-by: Kevin Miller <[email protected]> * add yyyymmdd validation tests Signed-off-by: Kevin Miller <[email protected]> * moar tests Signed-off-by: Kevin Miller <[email protected]> * Trying things out Signed-off-by: Kevin Miller <[email protected]> * Move to newer test action Signed-off-by: Kevin Miller <[email protected]> * add clarifying comment Signed-off-by: Kevin Miller <[email protected]> * fix linter issue Signed-off-by: Kevin Miller <[email protected]> * More action cleanup * task update also updates tests --------- Signed-off-by: Kevin Miller <[email protected]> Co-authored-by: Kevin Miller <[email protected]>
- Loading branch information
Showing
12 changed files
with
409 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bin/ | ||
.task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.