util

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 12 Imported by: 30

Documentation

Index

Constants

View Source
const (
	// MaxTimeout is the maximum timeout value used in scrapligo.
	MaxTimeout = 86_400
	// All used for test flags for platform/transport.
	All = "all"
	// Admin used for functional test user/pass.
	Admin = "admin"
)

Variables

View Source
var (
	// ErrIgnoredOption is the error returned when attempting to apply an option to a struct that
	// is not of the expected type. This error should not be exposed to end users.
	ErrIgnoredOption = errors.New("errIgnoredOption")
	// ErrConnectionError is the error returned for non auth related connection failures typically
	// encountered during *in channel ssh authentication* -- things like host key verification
	// failures and other openssh errors.
	ErrConnectionError = errors.New("errConnectionError")
	// ErrBadOption is returned when a bad value is passed to an option function.
	ErrBadOption = errors.New("errBadOption")
	// ErrTimeoutError is returned for any scrapligo timeout issues, meaning socket, transport or
	// channel (ops) timeouts.
	ErrTimeoutError = errors.New("errTimeoutError")
	// ErrAuthError is returned if any authentication errors are returned.
	ErrAuthError = errors.New("errAuthError")
	// ErrPrivilegeError is returned if there are any issues acquiring a privilege level or a user
	// requests an invalid privilege level etc..
	ErrPrivilegeError = errors.New("errPrivilegeError")
	// ErrFileNotFoundError is returned if a file cannot be found.
	ErrFileNotFoundError = errors.New("errFileNotFoundError")
	// ErrParseError is returned when parsing contents/files fails.
	ErrParseError = errors.New("errParseError")
	// ErrPlatformError is returned for any "platform" issues.
	ErrPlatformError = errors.New("errPlatformError")
	// ErrNetconfError is a blanket error for NETCONF issues that don't fall into another more
	// explicit error.
	ErrNetconfError = errors.New("errNetconfError")
	// ErrOperationError is returned for any "operation" issues -- mostly meaning ops timeouts.
	ErrOperationError = errors.New("errOperationError")
	// ErrNoOp is an error returned when a "no op" event happens -- that is a SendCommands or
	// SendConfigs method is called with an empty command/config slice provided.
	ErrNoOp = errors.New("errNoOp")
)

Functions

func ByteContainsAny added in v1.0.0

func ByteContainsAny(b []byte, l [][]byte) bool

ByteContainsAny checks if byte slice b is contained in any byte slice in the slice of byte slices l.

func ByteIsAny added in v1.0.0

func ByteIsAny(b byte, l []byte) bool

ByteIsAny checks if byte b is contained in byte slice l.

func BytesRoughlyContains added in v1.2.0

func BytesRoughlyContains(input, output []byte) bool

BytesRoughlyContains returns true if all bytes from the given byte slice `input` exist in the given `output` byte slice -- the elements must be found in order. This is basically the same as what you can see in @lithammer's(1) fuzzysearch `Match` function (thank you to them!) but converted to work for bytes and to not use a continuation block. Some examples:

input 'aa', output 'b' = false input 'aa', output 'bba' = false input 'aa', output 'bbaa' = true input 'aba', output 'bba' = false

In the context of scrapligo this is basically used for "fuzzy" matching our inputs. This lets us cope with devices that do things like the following srlinux banner entry output:

	```
 --{ !* candidate shared default }--[  ]--
 A:srl# system banner login-banner "
 ...my banner
 ...has
 ...some lines
 ...that are neat
 ..."
 --{ !* candidate shared default }--[  ]--

The "..." at the beginning of each line would historically be problematic for scrapli because in a very brute force/ham-fisted way we would demand to read back exactly what we sent to the device in the output -- so the "..." broke that. Not cool! This can be used to ensure that doesn't happen!

Note: @lithammer's fuzzy search `Match` function here: https://github.com/lithammer/fuzzysearch/blob/ \ b1f37a8c2080703d9fbd3e8989b2855c149a09e4/fuzzy/fuzzy.go#L60-L83.

func GetCleanFunc added in v1.0.0

func GetCleanFunc(platform string) func(r string) string

GetCleanFunc is only used for testing -- it returns a function that "cleans" output (usually a "show run" type of output) of any data that may change over time -- things like timestamps and password hashes, this allows for comparing the stored "golden" test data against "new" output gleaned from test clab devices.

func GetEnvIntOrDefault added in v1.0.0

func GetEnvIntOrDefault(k string, d int) int

GetEnvIntOrDefault returns the value of the environment variable k as an int *or* the default d if casting fails or the environment variable is not set.

func GetEnvStrOrDefault added in v1.0.0

func GetEnvStrOrDefault(k, d string) string

GetEnvStrOrDefault returns the value of the environment variable k as a string *or* the default d if casting fails or the environment variable is not set.

func LoadFileLines

func LoadFileLines(f string) ([]string, error)

LoadFileLines convenience function to load a file and return slice of strings of lines in that file.

func PlatformOK added in v1.0.0

func PlatformOK(platforms *string, platformName string) bool

PlatformOK is only used for testing and checks if the cli flag platforms is either "all" or contains the requested platform name platformName.

func ResolveAtFileOrURL added in v1.0.0

func ResolveAtFileOrURL(path string) ([]byte, error)

ResolveAtFileOrURL returns the bytes from `path` where path is either a filepath or URL.

func ResolveFilePath added in v0.1.2

func ResolveFilePath(f string) (string, error)

ResolveFilePath resolves the qualified path to file string f.

func StringContainsAny added in v1.0.0

func StringContainsAny(s string, l []string) bool

StringContainsAny checks if string s is in the string slice l, this function returns a bool indicating inclusion or not.

func StringContainsAnySubStrs added in v1.0.0

func StringContainsAnySubStrs(s string, l []string) string

StringContainsAnySubStrs checks if a string s is in the string slice l and returns the first encountered substring. If no match is encountered an empty string is returned.

func StringSliceContains added in v1.0.0

func StringSliceContains(ss []string, s string) bool

StringSliceContains checks if the string slice ss contains the substring s.

func StripANSI added in v1.0.0

func StripANSI(b []byte) []byte

StripANSI removes ANSI escape codes from the given byte slice b.

func TextFsmParse added in v1.0.0

func TextFsmParse(s, path string) ([]map[string]interface{}, error)

TextFsmParse parses recorded output w/ a provided textfsm template. the argument is interpreted as URL or filesystem path, for example: response.TextFsmParse("http://example.com/textfsm.template") or response.TextFsmParse("./local/textfsm.template").

func TransportOK added in v1.0.0

func TransportOK(transports *string, transportName string) bool

TransportOK is only used for testing and checks if the cli flag transports is either "all" or contains the requested transport name transportName.

Types

type Option added in v1.0.0

type Option func(interface{}) error

Option is a simple type that accepts an interface and returns an error; this should only be used in the context of applying options to an object.

type PayloadTestCase added in v1.0.0

type PayloadTestCase struct {
	Description string
	PayloadFile string
	ExpectErr   bool
}

PayloadTestCase is a simple struct used in testing only.

type Queue added in v1.0.0

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

Queue is a simple queue structure to store and queue/requeue/dequeue bytes.

func NewQueue added in v1.0.0

func NewQueue() *Queue

NewQueue returns a prepared Queue object.

func (*Queue) Dequeue added in v1.0.0

func (q *Queue) Dequeue() []byte

Dequeue returns the first bytes in the queue.

func (*Queue) DequeueAll added in v1.0.0

func (q *Queue) DequeueAll() []byte

DequeueAll returns all bytes in the queue.

func (*Queue) Enqueue added in v1.0.0

func (q *Queue) Enqueue(b []byte)

Enqueue queues some bytes at the end of the queue.

func (*Queue) GetDepth added in v1.0.0

func (q *Queue) GetDepth() int

GetDepth returns the depth of the queue.

func (*Queue) Requeue added in v1.0.0

func (q *Queue) Requeue(b []byte)

Requeue prepends some bytes to the front of the queue.

Jump to

Keyboard shortcuts

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