Skip to content

Commit

Permalink
Fix issue #24.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Feb 5, 2021
1 parent 5a8549a commit 1524474
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 37 deletions.
6 changes: 5 additions & 1 deletion cmd/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cmd

import (
"github.com/VirusTotal/vt-cli/utils"
"regexp"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,7 +50,10 @@ func NewAnalysisCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("analyses/%s", args, re)
return p.GetAndPrintObjects(
"analyses/%s",
utils.StringReaderFromCmdArgs(args),
re)
},
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cmd

import (
"github.com/VirusTotal/vt-cli/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -46,8 +47,10 @@ func NewDomainCmd() *cobra.Command {
if err != nil {
return err
}
p.GetAndPrintObjects("domains/%s", args, nil)
return nil
return p.GetAndPrintObjects(
"domains/%s",
utils.StringReaderFromCmdArgs(args),
nil)
},
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cmd

import (
"github.com/VirusTotal/vt-cli/utils"
"regexp"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,7 +50,10 @@ func NewFileCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("files/%s", args, re)
return p.GetAndPrintObjects(
"files/%s",
utils.StringReaderFromCmdArgs(args),
re)
},
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cmd

import (
"github.com/VirusTotal/vt-cli/utils"
"github.com/spf13/cobra"
)

Expand All @@ -35,7 +36,10 @@ func NewGroupCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("groups/%s", args, nil)
return p.GetAndPrintObjects(
"groups/%s",
utils.StringReaderFromCmdArgs(args),
nil)
},
}

Expand Down
11 changes: 9 additions & 2 deletions cmd/hunting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bufio"
"errors"
"fmt"
"github.com/VirusTotal/vt-cli/utils"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -124,7 +125,10 @@ func NewHuntingNotificationCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("intelligence/hunting_notifications/%s", args, re)
return p.GetAndPrintObjects(
"intelligence/hunting_notifications/%s",
utils.StringReaderFromCmdArgs(args),
re)
},
}

Expand Down Expand Up @@ -352,7 +356,10 @@ func NewHuntingRulesetCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("intelligence/hunting_rulesets/%s?relationships=owner,editors", args, re)
return p.GetAndPrintObjects(
"intelligence/hunting_rulesets/%s?relationships=owner,editors",
utils.StringReaderFromCmdArgs(args),
re)
},
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cmd

import (
"github.com/VirusTotal/vt-cli/utils"
"regexp"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -47,7 +48,10 @@ func NewIPCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("ip_addresses/%s", args, re)
return p.GetAndPrintObjects(
"ip_addresses/%s",
utils.StringReaderFromCmdArgs(args),
re)
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ func NewMonitorCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("monitor/items/%s", args, re)
return p.GetAndPrintObjects("monitor/items/%s",
utils.StringReaderFromCmdArgs(args),
re)
},
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/retrohunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package cmd

import (
"fmt"
"github.com/VirusTotal/vt-cli/utils"
"io/ioutil"
"os"
"regexp"
Expand Down Expand Up @@ -324,7 +325,10 @@ func NewRetrohuntCmd() *cobra.Command {
if err != nil {
return err
}
return p.GetAndPrintObjects("intelligence/retrohunt_jobs/%s", args, re)
return p.GetAndPrintObjects(
"intelligence/retrohunt_jobs/%s",
utils.StringReaderFromCmdArgs(args),
re)
},
}

Expand Down
13 changes: 7 additions & 6 deletions cmd/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package cmd

import (
"encoding/base64"

"github.com/VirusTotal/vt-cli/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -48,11 +48,12 @@ func NewURLCmd() *cobra.Command {
if err != nil {
return err
}
ids := make([]string, len(args))
for i, arg := range args {
ids[i] = base64.RawURLEncoding.EncodeToString([]byte(arg))
}
return p.GetAndPrintObjects("urls/%s", ids, nil)
r := utils.NewMappedStringReader(
utils.StringReaderFromCmdArgs(args),
func (url string) string {
return base64.RawURLEncoding.EncodeToString([]byte(url))
})
return p.GetAndPrintObjects("urls/%s", r, nil)
},
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd
import (
"strings"

"github.com/VirusTotal/vt-cli/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,7 +46,9 @@ func NewUserCmd() *cobra.Command {
"api_quota_group",
"intelligence_quota_group",
"monitor_quota_group",
}, ","), args, nil)
}, ","),
utils.StringReaderFromCmdArgs(args),
nil)

},
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mattn/go-runewidth v0.0.5 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/gox v1.0.1 // indirect
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/plusvic/go-ansi v0.0.0-20180516115420-9879244c4340
github.com/spf13/afero v1.2.2 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ github.com/gosuri/uitable v0.0.3/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/go-version v1.0.0 h1:21MVWPKDphxa7ineQQTrCU5brh7OuVVAzGOCnnCPtE8=
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down Expand Up @@ -88,10 +86,6 @@ github.com/mattn/go-runewidth v0.0.5/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
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/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4=
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
11 changes: 1 addition & 10 deletions utils/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,7 @@ func (p *Printer) PrintObject(obj *vt.Object) error {
// items from the args slice. If args contains a single "-" string, the args are
// read from stdin one per line. If argRe is non-nil, only args that match the
// regular expression are used and the rest are discarded.
func (p *Printer) GetAndPrintObjects(endpoint string, args []string, argRe *regexp.Regexp) error {

var r StringReader

if len(args) == 1 && args[0] == "-" {
r = NewStringIOReader(os.Stdin)
} else {
r = NewStringArrayReader(args)
}

func (p *Printer) GetAndPrintObjects(endpoint string, r StringReader, argRe *regexp.Regexp) error {
if argRe != nil {
r = NewFilteredStringReader(r, argRe)
}
Expand Down
42 changes: 39 additions & 3 deletions utils/string_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package utils
import (
"bufio"
"io"
"os"
"regexp"
"strings"
)
Expand Down Expand Up @@ -86,9 +87,9 @@ func NewFilteredStringReader(r StringReader, re *regexp.Regexp) *FilteredStringR
return &FilteredStringReader{r: r, re: re}
}

// ReadString reads strings from the the underlying StringReader and returns
// the first one that matches the regular expression specified while creating
// the FilteredStringReader. If no more strings can be read err is io.EOF.
// ReadString reads strings from the underlying StringReader and returns the
// first one that matches the regular expression specified while creating the
// FilteredStringReader. If no more strings can be read err is io.EOF.
func (f *FilteredStringReader) ReadString() (s string, err error) {
for s, err = f.r.ReadString(); s != "" || err == nil; s, err = f.r.ReadString() {
if f.re.MatchString(s) {
Expand All @@ -97,3 +98,38 @@ func (f *FilteredStringReader) ReadString() (s string, err error) {
}
return s, err
}


// MappedStringReader reads strings from a StringReader and call a map function
// that transforms the strings in some other string.
type MappedStringReader struct {
r StringReader
mapFn func(string) string
}

// NewMappedStringReader creates a new MappedStringReader that reads strings from
// r and can call mapFn for transforming the string before returning it.
func NewMappedStringReader(r StringReader, mapFn func(string) string) *MappedStringReader {
return &MappedStringReader{r:r, mapFn: mapFn}
}

// ReadString reads strings from the underlying StringReader and can call the
// map function associated to the MappedStringReader with that string, then
// returns the result produced by the map function.
func (m *MappedStringReader) ReadString() (s string, err error) {
if s, err = m.r.ReadString(); err == nil {
return m.mapFn(s), nil
}
return s, err
}

// StringReaderFromCmdArgs returns a string reader for reading the arguments
// passed in the command line. If the arguments consists in single hypen "-",
// they are read from stdin.
func StringReaderFromCmdArgs(args []string) StringReader {
if len(args) == 1 && args[0] == "-" {
return NewStringIOReader(os.Stdin)
} else {
return NewStringArrayReader(args)
}
}

0 comments on commit 1524474

Please sign in to comment.