utils

package
v0.0.0-...-4ece259 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterMap

func FilterMap(m map[string]interface{}, include, exclude []string) map[string]interface{}

FilterMap receives a map with string keys and arbitrary values (possibly other maps) and return a new map which is a subset of the original one containing only the keys matching any of the patterns in "include" and excluding keys matching any of the patterns in "exclude". The logic for determining if a key matches the pattern goes as follow:

  • The path for the key is computed. If the key is in the top-level map its path is the key itself, if the key is contained within a nested map its path is the concatenation of the parent's path and the key, using a dot (.) as a separator. The path for "key" in {a:{b:{key:val}}} is a.b.key.
  • The path is matched against the pattern, which can contain asterisks (*) as a placeholder for any character different from a dot (.) and ** as a placeholder for any character including a dot. For more information go to: https://godoc.org/github.com/gobwas/glob#Compile
  • If the path matches any pattern in "include" the key is included in the resulting map, as long as it doesn't match a pattern in "exclude".

func IsDir

func IsDir(f string) bool

IsDir function returns whether a file is a directory or not

func ObjectToMap

func ObjectToMap(obj *vt.Object) map[string]interface{}

ObjectToMap function that returns the attributes for an object as a map. Keys are attribute names and values are the attribute's value. Two special keys _id and _type are also included in the map with object's identifier and type respectively. The map is filtered according to the filters specified in the --include and --exclude command-line arguments.

Types

type APIClient

type APIClient struct {
	*vt.Client
}

APIClient represents a VirusTotal API client.

func NewAPIClient

func NewAPIClient(agent string) (*APIClient, error)

NewAPIClient returns a new VirusTotal API client using the API key configured either using the program configuration file or the --apikey command-line flag.

func (*APIClient) RetrieveObjects

func (c *APIClient) RetrieveObjects(endpoint string, args []string, outCh chan *vt.Object, errCh chan error) error

RetrieveObjects retrieves objects from the specified endpoint. The endpoint must contain a %s placeholder that will be replaced with items from the args slice. The objects are put into the outCh as they are retrieved.

type Coordinator

type Coordinator struct {
	Threads int
	Spinner *spinner.Spinner
	// contains filtered or unexported fields
}

Coordinator coordinates the work of multiple instances of a Doer that run in parallel.

func NewCoordinator

func NewCoordinator(threads int) *Coordinator

NewCoordinator creates a new instance of Coordinator.

func (*Coordinator) DoWithItemsFromChannel

func (c *Coordinator) DoWithItemsFromChannel(doer Doer, ch <-chan interface{})

DoWithItemsFromChannel calls the Do method of a type implementing the Doer interface with items read from a channel. This function doesn't exit until the channel is closed.

func (*Coordinator) DoWithObjectsFromIterator

func (c *Coordinator) DoWithObjectsFromIterator(doer Doer, it *vt.Iterator, bufferSize int)

DoWithObjectsFromIterator calls the Do of a type implementing the Doer interface with the objects returned by a vt.Iterator. Objects returned by the iterator are put in a channel with a buffer size of bufferSize.

func (*Coordinator) DoWithStringsFromReader

func (c *Coordinator) DoWithStringsFromReader(doer Doer, reader StringReader)

DoWithStringsFromReader calls the Do of a type implementing the Doer interface with strings read from a StringReader. The doer's Do method is called once for each string, and this function doesn't exit until the StringReader returns an empty string.

func (*Coordinator) EnableSpinner

func (c *Coordinator) EnableSpinner()

EnableSpinner activates an animation while the coordinator is waiting.

type Doer

type Doer interface {
	Do(interface{}, *DoerState) string
}

Doer is the interface that must be implemented for any type to be used with DoWithStringsFromReader and DoWithStringsFromChannel.

type DoerState

type DoerState struct {
	Progress string
}

DoerState represents the current state of a Doer.

type FilteredStringReader

type FilteredStringReader struct {
	// contains filtered or unexported fields
}

FilteredStringReader filters a StringReader returning only the strings that match a given regular expression.

func NewFilteredStringReader

func NewFilteredStringReader(r StringReader, re *regexp.Regexp) *FilteredStringReader

NewFilteredStringReader creates a new FilteredStringReader that reads strings from r and return only those that match re.

func (*FilteredStringReader) ReadString

func (f *FilteredStringReader) ReadString() (s string, err error)

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.

type MappedStringReader

type MappedStringReader struct {
	// contains filtered or unexported fields
}

MappedStringReader reads strings from a StringReader and call a map function that transforms the strings in some other string.

func NewMappedStringReader

func NewMappedStringReader(r StringReader, mapFn func(string) string) *MappedStringReader

NewMappedStringReader creates a new MappedStringReader that reads strings from r and can call mapFn for transforming the string before returning it.

func (*MappedStringReader) ReadString

func (m *MappedStringReader) ReadString() (s string, err error)

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.

type PQueue

type PQueue []PQueueNode

PQueue is a type implementing a priority queue

func (PQueue) Len

func (pq PQueue) Len() int

func (PQueue) Less

func (pq PQueue) Less(i, j int) bool

func (*PQueue) Pop

func (pq *PQueue) Pop() interface{}

Pop removes a value from the head of the priority queue

func (*PQueue) Push

func (pq *PQueue) Push(x interface{})

Push add a value to the tail of the priority queue

func (PQueue) Swap

func (pq PQueue) Swap(i, j int)

type PQueueNode

type PQueueNode struct {
	Priority int
	Data     interface{}
}

PQueueNode is a node in a priority queues

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer prints objects to stdout.

func NewPrinter

func NewPrinter(client *APIClient, cmd *cobra.Command, colors *yaml.Colors) (*Printer, error)

NewPrinter creates a new object printer.

func (*Printer) GetAndPrintObjects

func (p *Printer) GetAndPrintObjects(endpoint string, r StringReader, argRe *regexp.Regexp) error

GetAndPrintObjects retrieves objects from the specified endpoint and prints them. The endpoint must contain a %s placeholder that will be replaced with 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 (*Printer) Print

func (p *Printer) Print(data interface{}) error

Print prints the provided data to stdout.

func (*Printer) PrintCollection

func (p *Printer) PrintCollection(collection *url.URL) error

PrintCollection prints a collection of objects retrieved from the collection specified by the collection URL.

func (*Printer) PrintCommandLineWithCursor

func (p *Printer) PrintCommandLineWithCursor(it *vt.Iterator)

PrintCommandLineWithCursor prints the same command-line that was used for executing the program but adding or replacing the --cursor flag with the current cursor for the given iterator.

func (*Printer) PrintIterator

func (p *Printer) PrintIterator(it *vt.Iterator) error

PrintIterator prints the objects returned by an object iterator.

func (*Printer) PrintObject

func (p *Printer) PrintObject(obj *vt.Object) error

PrintObject prints the specified object to stdout.

func (*Printer) PrintObjects

func (p *Printer) PrintObjects(objs []*vt.Object) error

PrintObjects prints all the specified objects to stdout.

func (*Printer) PrintSyncMap

func (p *Printer) PrintSyncMap(sm *sync.Map) error

PrintSyncMap prints a sync.Map.

type StringArrayReader

type StringArrayReader struct {
	// contains filtered or unexported fields
}

StringArrayReader is a wrapper around a slice of strings that implements the StringReader interface. Each time the ReadString method is called a string from the array is returned and the position is advanced by one. When all strings have been returned ReadString returns an io.EOF error.

func NewFileDirReader

func NewFileDirReader(fileDir string) (*StringArrayReader, error)

FileDirReader returns all files inside a given directory as a StringArrayReader

func NewStringArrayReader

func NewStringArrayReader(strings []string) *StringArrayReader

NewStringArrayReader creates a new StringArrayReader.

func (*StringArrayReader) ReadString

func (sar *StringArrayReader) ReadString() (string, error)

ReadString reads one string from StringArrayReader. When all strings have been returned ReadString returns an io.EOF error.

type StringIOReader

type StringIOReader struct {
	// contains filtered or unexported fields
}

StringIOReader is a wrapper around a bufio.Scanner that implements the StringReader interface.

func NewStringIOReader

func NewStringIOReader(r io.Reader) *StringIOReader

NewStringIOReader creates a new StringIOReader.

func (*StringIOReader) ReadString

func (sir *StringIOReader) ReadString() (string, error)

ReadString reads one string from StringIOReader. When all strings have been returned ReadString returns an io.EOF error.

type StringReader

type StringReader interface {
	ReadString() (string, error)
}

StringReader is the interface that wraps the ReadString method.

func StringReaderFromCmdArgs

func StringReaderFromCmdArgs(args []string) StringReader

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL