Skip to content

Commit

Permalink
Use latest vt-go and improve the handling of dates while generating Y…
Browse files Browse the repository at this point in the history
…AML content.
  • Loading branch information
plusvic committed Apr 15, 2021
1 parent a8b7555 commit 09706b4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/VirusTotal/vt-cli
go 1.14

require (
github.com/VirusTotal/vt-go v0.0.0-20210329222805-6a66bdb2ca68
github.com/VirusTotal/vt-go v0.0.0-20210415081439-66c331640a9d
github.com/briandowns/spinner v1.7.0
github.com/cavaliercoder/grab v2.0.0+incompatible
github.com/dustin/go-humanize v1.0.0
Expand All @@ -21,7 +21,6 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.4.0
github.com/thedevsaddam/gojsonq v2.3.0+incompatible // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/VirusTotal/vt-go v0.0.0-20210329222805-6a66bdb2ca68 h1:Ift2rmfYEvSS+TsnZ+8p2tHQvRldep3ZDP+f/IfbrAc=
github.com/VirusTotal/vt-go v0.0.0-20210329222805-6a66bdb2ca68/go.mod h1:JYm6xWAu6hBrmIcmcEne7lmiE9PJ73kthTmrIpvGBu8=
github.com/VirusTotal/vt-go v0.0.0-20210415081439-66c331640a9d h1:3TT/bNnjEfMysjRz822gkw2ca8LmAjgk9vc8FWhe7MU=
github.com/VirusTotal/vt-go v0.0.0-20210415081439-66c331640a9d/go.mod h1:u1+HeRyl/gQs67eDgVEWNE7+x+zCyXhdtNVrRJR5YPE=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -208,12 +208,12 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/thedevsaddam/gojsonq v2.3.0+incompatible h1:i2lFTvGY4LvoZ2VUzedsFlRiyaWcJm3Uh6cQ9+HyQA8=
github.com/thedevsaddam/gojsonq v2.3.0+incompatible/go.mod h1:RBcQaITThgJAAYKH7FNp2onYodRz8URfsuEGpAch0NA=
github.com/thedevsaddam/gojsonq/v2 v2.5.2 h1:CoMVaYyKFsVj6TjU6APqAhAvC07hTI6IQen8PHzHYY0=
github.com/thedevsaddam/gojsonq/v2 v2.5.2/go.mod h1:bv6Xa7kWy82uT0LnXPE2SzGqTj33TAEeR560MdJkiXs=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down Expand Up @@ -339,11 +339,11 @@ gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
29 changes: 18 additions & 11 deletions yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,24 @@ func (enc *Encoder) encodeMap(m reflect.Value, indent int, prefix string) (err e
if v.IsValid() {
vt := v.Type()
ks := k.String()
// If key is "date" or ends with "_date" and value is json.Number, this
// field is a date.
isDate := enc.matchDateKey(ks) &&
vt.Name() == "Number" &&
vt.PkgPath() == "encoding/json"
// If this field is a date let's add a comment with the date in a
// human-readable format.
if isDate {
ts, err := strconv.ParseInt(v.String(), 10, 64)
if err != nil {
panic(err)
// If key matches any of the patterns specified in the EncoderDateKeys
// option while creating the YAML encoder, this field should be treated
// as a date, let's add a comment with the date in a human-readable format.
if enc.matchDateKey(ks) {
var ts int64
switch {
case vt.Name() == "Number" && vt.PkgPath() == "encoding/json":
var err error
if ts, err = strconv.ParseInt(v.String(), 10, 64); err != nil {
panic(err)
}
case vt.Name() == "float64":
ts = int64(v.Float())
case vt.Name() == "int64":
panic("int")
ts = v.Int()
default:
return fmt.Errorf("unexpected type for a date field: %s", vt.Name())
}
commentPrinter(enc.w, " # %v", time.Unix(ts, 0))
}
Expand Down
20 changes: 20 additions & 0 deletions yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@ var tests = []T{
Foo_date: 10000 # %v
`, time.Unix(10000, 0))),
},
{
data: struct {
Bar_date int64
}{
Bar_date: 10000,
},
yaml: Y(fmt.Sprintf(`
Bar_date: 10000 # %v
`, time.Unix(10000, 0))),
},
{
data: struct {
Baz_date float64
}{
Baz_date: 1618312811,
},
yaml: Y(fmt.Sprintf(`
Baz_date: 1.618312811e+09 # %v
`, time.Unix(1618312811, 0))),
},
}

func TestYAML(t *testing.T) {
Expand Down

0 comments on commit 09706b4

Please sign in to comment.