Skip to content

Commit

Permalink
Merge pull request #25 from kanmu/add_next_cmd_format
Browse files Browse the repository at this point in the history
Update next command: Add --format option
  • Loading branch information
winebarrel committed Jun 18, 2023
2 parents 2f85e2a + 609048e commit 6a9ce69
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,14 @@ $ jhol next
$ jhol is-holiday 2023-07-17
true
$ jhol is-holiday # today
false
$ jhol next 5 -f '%Y/%m/%d(%a)'
2023/07/17(Mon) 海の日
2023/08/11(Fri) 山の日
2023/09/18(Mon) 敬老の日
2023/09/23(Sat) 秋分の日
2023/10/09(Mon) スポーツの日
```
4 changes: 2 additions & 2 deletions cmd/subcmd/is_holiday_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestIsHolidayCmd_True(_t *testing.T) {
}

err := cmd.Run(&subcmd.Binds{
Client: Client,
Client: TestClient,
Out: out,
})

Expand All @@ -37,7 +37,7 @@ func TestIsHolidayCmd_False(_t *testing.T) {
}

err := cmd.Run(&subcmd.Binds{
Client: Client,
Client: TestClient,
Out: out,
})

Expand Down
17 changes: 15 additions & 2 deletions cmd/subcmd/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package subcmd

import (
"fmt"

"github.com/lestrrat-go/strftime"
)

type Next struct {
N int `arg:"" default:"3" help:"Number to output."`
N int `arg:"" default:"3" help:"Number to output."`
Format string `short:"f" help:"Date format"`
}

func (cmd *Next) Run(binds *Binds) error {
Expand All @@ -17,7 +20,17 @@ func (cmd *Next) Run(binds *Binds) error {
}

for _, h := range holidays {
fmt.Fprintln(binds.Out, h)
if cmd.Format == "" {
fmt.Fprintln(binds.Out, h)
} else {
date, err := strftime.Format(cmd.Format, h.Date)

if err != nil {
return err
}

fmt.Fprintf(binds.Out, "%s\t%s\n", date, h.Name)
}
}

return nil
Expand Down
29 changes: 27 additions & 2 deletions cmd/subcmd/next_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNextCmd_3(_t *testing.T) {
}

err := cmd.Run(&subcmd.Binds{
Client: Client,
Client: TestClient,
Out: out,
Now: func() time.Time { return dateparse.MustParse("2023-07-17") },
})
Expand All @@ -43,7 +43,7 @@ func TestNextCmd_5(_t *testing.T) {
}

err := cmd.Run(&subcmd.Binds{
Client: Client,
Client: TestClient,
Out: out,
Now: func() time.Time { return dateparse.MustParse("2023-07-17") },
})
Expand All @@ -59,3 +59,28 @@ func TestNextCmd_5(_t *testing.T) {
2023-10-09 スポーツの日
`, out.String())
}

func TestNextCmd_Format(_t *testing.T) {
assert := assert.New(_t)
out := &strings.Builder{}

cmd := &subcmd.Next{
N: 3,
Format: "%Y/%m/%d(%a)",
}

err := cmd.Run(&subcmd.Binds{
Client: TestClient,
Out: out,
Now: func() time.Time { return dateparse.MustParse("2023-07-17") },
})

if !assert.NoError(err) {
return
}

assert.Equal(`2023/07/17(Mon) 海の日
2023/08/11(Fri) 山の日
2023/09/18(Mon) 敬老の日
`, out.String())
}
4 changes: 2 additions & 2 deletions cmd/subcmd/subcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
Client *jhol.ClientWithoutContext
TestClient *jhol.ClientWithoutContext
)

func TestMain(m *testing.M) {
Expand All @@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
panic("$TEST_GCAL_API_KEY is empty")
}

Client = jhol.NewClient(apiKey).WithoutContext()
TestClient = jhol.NewClient(apiKey).WithoutContext()

m.Run()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
github.com/googleapis/gax-go/v2 v2.10.0 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ github.com/googleapis/gax-go/v2 v2.10.0 h1:ebSgKfMxynOdxw8QQuFOKMgomqeLGPqNLQox2
github.com/googleapis/gax-go/v2 v2.10.0/go.mod h1:4UOEnMCrxsSqQ940WnTiD6qJ63le2ev3xfyagutxiPw=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -81,6 +86,7 @@ github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down

0 comments on commit 6a9ce69

Please sign in to comment.