Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Location as a field of YMDFlag #5

Merged
merged 12 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading