Skip to content

Commit

Permalink
use strftime format specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
bbkane committed Dec 17, 2021
1 parent cafa141 commit 483afba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ GOOGLE_APPLICATION_CREDENTIALS=/path/to/keys.json starghaze gsheets upload \

## TODO

- change `--date-format` to use strftime conventions.
- `readmes` command to download READMEs for starred repos,
- zinc command to push to [zinc](https://github.com/prabhatsharma/zinc) and see if I like zinc
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ require (
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/googleapis/gax-go/v2 v2.1.1 // indirect
github.com/lestrrat-go/strftime v1.0.5 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,15 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/strftime v1.0.5 h1:A7H3tT8DhTz8u65w+JRpiBxM4dINQhUXAZnhBa2xeOE=
github.com/lestrrat-go/strftime v1.0.5/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.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 @@ -180,6 +185,7 @@ github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a h1:KikTa6HtAK8cS1
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
27 changes: 18 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"

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

// This will be overriden by goreleaser
Expand Down Expand Up @@ -162,13 +164,14 @@ type formattedDate struct {
t time.Time
// Format cotnrols what is sent out. time.Times being unmarshalled should be
// in RFC3339 format (the default)
Format string
Format *strftime.Strftime
}

func (d formattedDate) MarshalJSON() ([]byte, error) {
// https://www.programming-books.io/essential/go/custom-json-marshaling-468765d144a34e87b913c7674e66c3a4
// NOTE: if you forget the enclosing quotes, MarshalJSON doesn't emit anything and doesn't error out
s := "\"" + d.t.Format(d.Format) + "\""
// s := "\"" + d.t.Format(d.Format) + "\""
s := `"` + d.Format.FormatString(d.t) + `"`
return []byte(s), nil
}

Expand All @@ -178,7 +181,8 @@ func (d *formattedDate) UnmarshalJSON(b []byte) error {
}

func (d formattedDate) String() string {
return d.t.Format(d.Format)
// return d.t.Format(d.Format)
return d.Format.FormatString(d.t)
}

type starredRepositoryEdge struct {
Expand Down Expand Up @@ -209,7 +213,12 @@ func stats(pf flag.PassedFlags) error {
output, outputExists := pf["--output"].(string)
format := pf["--format"].(string)
timeout := pf["--timeout"].(time.Duration)
dateFormat := pf["--date-format"].(string)
dateFormatStr := pf["--date-format"].(string)

dateFormat, err := strftime.New(dateFormatStr)
if err != nil {
return fmt.Errorf("--date-format error: %w", err)
}

fp := os.Stdout
if outputExists {
Expand All @@ -234,13 +243,13 @@ func stats(pf flag.PassedFlags) error {
return fmt.Errorf("unknown output format: %s", format)
}

err := p.Header()
defer p.Flush()

err = p.Header()
if err != nil {
return err
}

defer p.Flush()

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
src := oauth2.StaticTokenSource(
Expand Down Expand Up @@ -406,9 +415,9 @@ func main() {
),
command.Flag(
"--date-format",
"Format for outputted dates. See https://pkg.go.dev/time#Time.Format for details",
"Format for outputted dates. See https://github.com/lestrrat-go/strftime#supported-conversion-specifications for details",
value.String,
flag.Default("Jan 2, 2006"),
flag.Default("%b %d, %Y"),
flag.Required(),
),
),
Expand Down

0 comments on commit 483afba

Please sign in to comment.