util

package
v0.0.0-...-fd8c754 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SemVersion = "0.0.0"

SemVersion the version in semver.org format, MUST be overwritten during the linking stage of the build process

Functions

func BaseURL

func BaseURL(u *url.URL) (string, error)

BaseURL returns the base URL for a given URL.

func BoolMatcher

func BoolMatcher(dst *bool) func(interface{}) error

BoolMatcher stores the matched result in a bool.

func CleanFileName

func CleanFileName(s string) string

CleanFileName replaces invalid runes with an underscore and afterwards collapses multiple underscores into one. If the filename does not end with '.json' it will be appended. The filename is converted to lower case. https://docs.oasis-open.org/csaf/csaf/v2.0/csd02/csaf-v2.0-csd02.html#51-filename specifies valid runes as 'a' to 'z', '0' to '9' and '+', '-', '_'.

func ConfirmingFileName

func ConfirmingFileName(fname string) bool

ConfirmingFileName checks if the given filename is confirming the standard.

func DeepCopy

func DeepCopy(dst, src string) error

DeepCopy copy a directory tree src to tree dst. Files are hard linked.

func HashFromFile

func HashFromFile(fname string) ([]byte, error)

HashFromFile reads a base 16 coded hash sum from a file.

func HashFromReader

func HashFromReader(r io.Reader) ([]byte, error)

HashFromReader reads a base 16 coded hash sum from a reader.

func MakeUniqDir

func MakeUniqDir(prefix string) (string, error)

MakeUniqDir creates a unique named directory with the given prefix. In case of name collisions the current date plus a random number is appended.

func MakeUniqFile

func MakeUniqFile(prefix string) (string, *os.File, error)

MakeUniqFile creates a unique named file with the given prefix opened in write only mode. In case of name collisions the current date plus a random number is appended.

func PathExists

func PathExists(path string) (bool, error)

PathExists returns true if path exits.

func ReMarshalJSON

func ReMarshalJSON(dst, src interface{}) error

ReMarshalJSON transforms data from src to dst via JSON marshalling.

func ReMarshalMatcher

func ReMarshalMatcher(dst interface{}) func(interface{}) error

ReMarshalMatcher is an action to re-marshal the result to another type.

func StringMatcher

func StringMatcher(dst *string) func(interface{}) error

StringMatcher stores the matched result in a string.

func TimeMatcher

func TimeMatcher(dst *time.Time, format string) func(interface{}) error

TimeMatcher stores a time with a given format.

func WriteHashSumToFile

func WriteHashSumToFile(fname, name string, sum []byte) error

WriteHashSumToFile writes a hash sum to file fname.

func WriteHashToFile

func WriteHashToFile(fname, name string, h hash.Hash, data []byte) error

WriteHashToFile writes a hash of data to file fname.

func WriteToFile

func WriteToFile(fname string, wt io.WriterTo) error

WriteToFile saves the content of wt into a file names fname.

Types

type Client

type Client interface {
	Do(req *http.Request) (*http.Response, error)
	Get(url string) (*http.Response, error)
	Head(url string) (*http.Response, error)
	Post(url, contentType string, body io.Reader) (*http.Response, error)
	PostForm(url string, data url.Values) (*http.Response, error)
}

Client is an interface to abstract http.Client.

type LimitingClient

type LimitingClient struct {
	Client
	Limiter *rate.Limiter
}

LimitingClient is a Client implementing rate throttling.

func (*LimitingClient) Do

func (lc *LimitingClient) Do(req *http.Request) (*http.Response, error)

Do implements the respective method of the Client interface.

func (*LimitingClient) Get

func (lc *LimitingClient) Get(url string) (*http.Response, error)

Get implements the respective method of the Client interface.

func (*LimitingClient) Head

func (lc *LimitingClient) Head(url string) (*http.Response, error)

Head implements the respective method of the Client interface.

func (*LimitingClient) Post

func (lc *LimitingClient) Post(url, contentType string, body io.Reader) (*http.Response, error)

Post implements the respective method of the Client interface.

func (*LimitingClient) PostForm

func (lc *LimitingClient) PostForm(url string, data url.Values) (*http.Response, error)

PostForm implements the respective method of the Client interface.

type LoggingClient

type LoggingClient struct {
	Client
}

LoggingClient is a client that logs called URLs.

func (*LoggingClient) Do

func (lc *LoggingClient) Do(req *http.Request) (*http.Response, error)

Do implements the respective method of the Client interface.

func (*LoggingClient) Get

func (lc *LoggingClient) Get(url string) (*http.Response, error)

Get implements the respective method of the Client interface.

func (*LoggingClient) Head

func (lc *LoggingClient) Head(url string) (*http.Response, error)

Head implements the respective method of the Client interface.

func (*LoggingClient) Post

func (lc *LoggingClient) Post(url, contentType string, body io.Reader) (*http.Response, error)

Post implements the respective method of the Client interface.

func (*LoggingClient) PostForm

func (lc *LoggingClient) PostForm(url string, data url.Values) (*http.Response, error)

PostForm implements the respective method of the Client interface.

type NWriter

type NWriter struct {
	io.Writer
	N int64
}

NWriter is an io.Writer counting the bytes copied through it.

func (*NWriter) Write

func (nw *NWriter) Write(p []byte) (int, error)

Write implements the Write method of io.Writer.

type PathEval

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

PathEval is a helper to evaluate JSON paths on documents.

func NewPathEval

func NewPathEval() *PathEval

NewPathEval creates a new PathEval.

func (*PathEval) Eval

func (pe *PathEval) Eval(expr string, doc interface{}) (interface{}, error)

Eval evalutes expression expr on document doc. Returns the result of the expression.

func (*PathEval) Extract

func (pe *PathEval) Extract(
	expr string,
	action func(interface{}) error,
	optional bool,
	doc interface{},
) error

Extract extracts a value from a given document with a given expression/action.

func (*PathEval) Match

func (pe *PathEval) Match(matcher []PathEvalMatcher, doc interface{}) error

Match matches a list of PathEvalMatcher pairs against a document.

type PathEvalMatcher

type PathEvalMatcher struct {
	// Expr is the expression to evaluate
	Expr string
	// Action is executed with the result of the match.
	Action func(interface{}) error
	// Optional expresses if the expression is optional.
	Optional bool
}

PathEvalMatcher is a pair of an expression and an action when doing extractions via PathEval.Match.

Jump to

Keyboard shortcuts

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