helpers

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BBGLobalFolderName = "Bumblebee"
	BBConfigFileName   = "config.yaml"
)
View Source
const (
	KeyPairNameForKeyStoreReads  = "keystore_read"
	KeyPairNameForKeyStoreWrites = "keystore_write"
)
View Source
const (
	ExitCodeSuccess = iota
	ExitCodePanicInExecute
	ExitCodeErrorReturnedToExecute
	ExitCodeInvalidInput
	ExitCodeInputError
	ExitCodeCipherError
	ExitCodeStartupFailure
	ExitCodeOutputError
	ExitCodeRequestFailed
)

A list of ExitCode value mappings to specific errors. This is useful when running this utility from a shell call or some other app.

View Source
const MAX_TRY_RECOUNTS = 5

Variables

View Source
var CmdHelpers = &HelpersInfo{}

ExitCode is used as the final ExitCode return by this runtime

View Source
var GlobalUseProfile string
View Source
var LineBreak string

Functions

func AcquireKey

func AcquireKey(ProfileName string) ([]byte, error)

AcquireKey will check to see if the key env var is available. If not, it will prompt the user for the key

func BuildProfilePath

func BuildProfilePath(name string) (string, error)

func CheckIsPiped

func CheckIsPiped() bool

func CompareStrings

func CompareStrings(a, b string) int

CompareStrings is a case-insensitive comparison. Returns:

  • 0 if strings are the same
  • -1 if a is less than b
  • 1 if a is greater than b

While strings.Equalfold is available from the standard lib, it does not tell us which value is greater than the other. This CompareStrings provides that where needed. When we only need a true/false comparison, we'll use EqualFold.

func DirExists

func DirExists(dirPath string) bool

func FileExists

func FileExists(filePath string) bool

func FileExistsInfo

func FileExistsInfo(filePath string) (found, isDir bool)

func FileExistsWithDetails

func FileExistsWithDetails(filePath string) (isFound, isDir bool, err error)

func ForcePath

func ForcePath(p string) error

func FormatDuration

func FormatDuration(totalTime time.Duration) string

func GetConfigPath

func GetConfigPath() (string, error)

func GetConsoleInputChar

func GetConsoleInputChar(allowedChars string) (inputChar string, err error)

func GetConsoleInputLine

func GetConsoleInputLine(promptText string) (inputLine string, err error)

func GetConsoleMultipleInputLines

func GetConsoleMultipleInputLines(labelText string) (inputLines []string, err error)

func GetConsoleRequiredInputLine

func GetConsoleRequiredInputLine(promptText, valueName string) (inputLine string, err error)

func GetEnvSafeName

func GetEnvSafeName(inputName string) (outputName string)

func GetFileSafeName

func GetFileSafeName(inputName string) (outputName string)

GetFileSafeName will replace characters in the inputName that are not safe for naming directories or files. Due to cross-platform concerns, this will convert or remove things that are not within the POSIX Portable File Name character set.

func GetPassword

func GetPassword() ([]byte, error)

func GetPasswordWithConfirm

func GetPasswordWithConfirm(label string) ([]byte, error)

func GetRandomBytes

func GetRandomBytes(requestedByteCount int) (secretBytes []byte, err error)

func InitializeClipboard

func InitializeClipboard() (err error)

func MatchesFilter

func MatchesFilter(value, pattern string) bool

MatchesFilter tests for match scenarios, where value can contain pattern, value can equal pattern, value can start with pattern, or pattern can be a regex expression applied to value

General guidelines...

  • pattern starts with a !!, then the rest of pattern is a regex expression
  • pattern starts AND ends with an *, then the text between the *'s should be contained in value
  • pattern only ends in an *, leading portion of value must match the corresponding leading portion of pattern prior to the *
  • pattern only starts with an *, then value must end with the portion of pattern up to the *, but does not have to exactly MATCH pattern
  • pattern does NOT start with !! and has no * at end or beginning, then value must case-insensitive MATCH pattern

This does NOT support...

  • abstract matches (*) in the middle of the match pattern
  • Placeholder wildcards using ?, but might add that in the future

If the pattern start with a single "!", this is considered a NOT indicator, so that match logic is reserved post match of the remainder of the pattern text.

func ReadFromClipboard

func ReadFromClipboard() ([]byte, error)

func ReplaceFileExt

func ReplaceFileExt(filePath, newExt string) string

ReplaceFileExt will change the file extension if the filePath contains one. If it does not, it will add the extension. newExt is allowed to have a period or not, both scenarios will work correctly

func WriteToClipboard

func WriteToClipboard(data []byte) error

Types

type ConfigHelper

type ConfigHelper struct {
	Config *ConfigInfo
}
var GlobalConfig *ConfigHelper

func NewConfigHelper

func NewConfigHelper() *ConfigHelper

func NewConfigHelperFromConfig

func NewConfigHelperFromConfig(config *ConfigInfo) *ConfigHelper

func (*ConfigHelper) GetConfigInfo

func (ch *ConfigHelper) GetConfigInfo() *ConfigInfo

func (*ConfigHelper) GetConfigYAMLLines

func (ch *ConfigHelper) GetConfigYAMLLines() ([]string, error)

func (*ConfigHelper) GetCurrentProfile

func (ch *ConfigHelper) GetCurrentProfile() *Profile

func (*ConfigHelper) GetProfile

func (ch *ConfigHelper) GetProfile(name string) *Profile

func (*ConfigHelper) GetProfileYAMLLines

func (ch *ConfigHelper) GetProfileYAMLLines(profileName string) ([]string, string, error)

func (*ConfigHelper) ListProfiles

func (ch *ConfigHelper) ListProfiles() []Profile

func (*ConfigHelper) LoadConfig

func (ch *ConfigHelper) LoadConfig() error

func (*ConfigHelper) NewProfile

func (ch *ConfigHelper) NewProfile(profile *Profile) error

func (*ConfigHelper) RemoveProfile

func (ch *ConfigHelper) RemoveProfile(name string) (found bool, err error)

func (*ConfigHelper) RenameProfile

func (ch *ConfigHelper) RenameProfile(currentName, newName string) (found bool, err error)

func (*ConfigHelper) SelectProfile

func (ch *ConfigHelper) SelectProfile(name string) error

func (*ConfigHelper) WriteConfig

func (ch *ConfigHelper) WriteConfig() error

type ConfigInfo

type ConfigInfo struct {
	Profiles       []*Profile `yaml:"profiles"`
	CurrentProfile string     `yaml:"currentProfile"`
}

func (*ConfigInfo) Clone

func (configIn *ConfigInfo) Clone() (configOut *ConfigInfo)

type ExportOutputEncoding

type ExportOutputEncoding int
const (
	ExportOutputEncodingRaw ExportOutputEncoding = iota
	ExportOutputEncodingText
	ExportOutputEncodingUnknown
)

func TextToExportOutputEncoding

func TextToExportOutputEncoding(text string) ExportOutputEncoding

type ExportOutputTarget

type ExportOutputTarget int
const (
	ExportOutputTargetConsole ExportOutputTarget = iota
	ExportOutputTargetClipboard
	ExportOutputTargetFile
	ExportOutputTargetUnknown
)

func TextToExportOutputTarget

func TextToExportOutputTarget(text string) ExportOutputTarget

type HelpersInfo

type HelpersInfo struct {
	OutputValueOnly bool
	PipeMode        bool
	UseProfile      string
}

HelpersInfo is used to store the command line parameters, as well as transformed values. These are set by the cmd package/cobra, and are used or transformed in the primary logic in the encode package.

type ImportInputSource

type ImportInputSource int
const (
	ImportInputSourcePiped ImportInputSource = iota
	ImportInputSourceClipboard
	ImportInputSourceFile
	ImportInputSourceUnknown
)

func TextToImportInputSource

func TextToImportInputSource(text string) ImportInputSource

type InputResponseVal

type InputResponseVal int
const (
	InputResponseValNull InputResponseVal = iota
	InputResponseValYes
	InputResponseValNo
)

func GetYesNoInput

func GetYesNoInput(inputMessage string, nullVal InputResponseVal) (InputResponseVal, error)

GetYesNoInput will display an optional message and append "Yes/No", then wait for user input. If input is not valid, it will check again

type Profile

type Profile struct {
	Name                  string `yaml:"name"`
	Path                  string `yaml:"path"`
	KeyStorePath          string `yaml:"keyStore"`
	KeyPairStorePath      string `yaml:"keyPairStore"`
	KeyPairStoreEncrypted bool   `yaml:"keyPairStoreEncrypted"`

	// DefaultKeypairName is optional and is the name to use as the sender when using the default key for this profile
	DefaultKeypairName string `yaml:"defaultKeypairName"`
}

func (*Profile) Clone

func (p *Profile) Clone() *Profile

type TextScanner

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

TextScanner will scan text input and parse bumblebee encrypted data. It will provide a reader interface for that parsed data.

  • if no start/end tokens are encountered, it will assume the input is a single, combined data blob and serve accordingly from the read interface.

- This is NOT intended to handle vary large datasets.

func NewTextScanner

func NewTextScanner(data []byte) (*TextScanner, error)

NewTextScanner will return an initialized text scanner. Data is optional. If provided, it will be passed to the Parse method.

func (*TextScanner) Parse

func (ts *TextScanner) Parse(data []byte) error

Parse will try to determine the nature of the data and parse accordingly. It uses this logic:

  • Does it contain the hex encoding marker ":start"? If so, parse as hexencoded
  • Does it break into lines that are only valid hex characters, ignoring marker lines? If so, parse as one hex encoded combined blob
  • Otherwise, parse as a binary blob

func (*TextScanner) Read

func (ts *TextScanner) Read(p []byte) (n int, err error)

type TextWriter

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

func NewTextWriter

func NewTextWriter(
	outputTarget TextWriterTarget,
	lineWidth int,
	mode TextWriterMode,
	headerText,
	footerText string,
	onStartFunc, afterFlushFunc TextWriterEventFunc,
) *TextWriter

func (*TextWriter) Flush

func (tw *TextWriter) Flush() (n int, err error)

func (*TextWriter) PostFlushOutputBuffer

func (tw *TextWriter) PostFlushOutputBuffer() []byte

OutputBuffer can only be called once, after which it will return empty bytes. This is because of how the bytes buffer works relating to calling "Bytes()" on the buffer. This includes when using the clipboard for output. After flush, the buffer is drained to the clipboard. This would then return nothing. So, only use this for buffered scenarios that are NOT related to the clipboard. And then ONLY after calling flush.

func (*TextWriter) PrintFooter

func (tw *TextWriter) PrintFooter() error

func (*TextWriter) PrintTextLine

func (tw *TextWriter) PrintTextLine(textLine string) error

func (*TextWriter) Reset

func (tw *TextWriter) Reset(headerText, footerText string)

func (*TextWriter) Write

func (tw *TextWriter) Write(p []byte) (n int, err error)

type TextWriterEventFunc

type TextWriterEventFunc func()
var NilTextWriterEventFunc TextWriterEventFunc = nil

type TextWriterMode

type TextWriterMode int
const (
	TextWriterModeBinary TextWriterMode = iota
	TextWriterModeText
)

type TextWriterTarget

type TextWriterTarget int
const (
	TextWriterTargetConsole TextWriterTarget = iota
	TextWriterTargetClipboard
	TextWriterTargetBuffered
)

type YAMLConfig

type YAMLConfig struct {
	Config *ConfigInfo `yaml:"BumblebeeSettings"`
}

Jump to

Keyboard shortcuts

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