parser

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommandIdx

func CommandIdx(args []string, commands map[string]bool) int

CommandIdx finds the index of the command in a slice of args. If no commands are found (all args are filters), then -1 is returned. Preconditions: args is a full command line input from os.Args, including program name `flash-cli` at index 0

func FindCommand

func FindCommand(args []string, commands map[string]bool) (string, int)

FindCommand returns the command and its index in a slice of args Returns default command and -1 if no command is found Preconditions: args is a full command line input from os.Args, including program name `flash-cli` at index 0

func IsFilter

func IsFilter(s string) bool

IsFilter produces true if the string matches the requirements of command filter. If a string contains any whitespace charcater it is not a filter Any single word string starting with '-' or '+' and any strings containing ':' are considered filters. Any string consisting of a valid uuid is considered a filter. Any string starting with a numeric digit is considered a filter.

NOTE: This does not validate filter's correctness which must be checked elsewhere. ie. a filter character without arguments ":" will pass or invalid number will pass "14a"

func ValidateFilter

func ValidateFilter(s string) error

ValidateFilter returns an error if filters passed to this function are invalid

Preconditions: s passed the IsFilter() function check Passing IsFilter() ensures one of these conditions:

  • s starts with "-" but not "--" ie. "-foo"
  • s starts with "+" ie. "+foo"
  • s contains ":" ie. "group:foo" "foo:bar" ":"
  • s starts with a digit "[0..9]" ie. "1,5,10" "14a"
  • s is a valid UUID

func ValidateFilters

func ValidateFilters(filters []string) error

ValidateFilters returns an error on the first invalid filter found Empty filters slice is considered valid

Types

type Filter

type Filter struct {
	// Type is an iota enum one of ID|RANGE|UUID|TAG|GROUP|CUSTOM that represents the filter's type
	Type FilterType
	// Key is the filter key for CUSTOM type filters. Will equal "group" for GROUP types and
	// its respective +/- tag for TAGs though other fields should be used. All others will be empty string
	Key string
	// Value is filter string processed for database query. Strips prefixes
	Value string
	// True if it was a '-' tag. Will be global mandate exclusion rather than 'or' behavior.
	IsExclude bool
	// Low end of the ID range, inclusive. Will equal High if single ID. Will equal -1 for all other types.
	Low int
	// High end of the ID range, inclusive. Will equal Low if single ID. Will equal -1 for all other types.
	High int
	// contains filtered or unexported fields
}

Filter is a single filter (no compounds) which hold the string and the filter type Values stored in Type are one of constants ID|RANGE|UUID|TAG|GROUP|CUSTOM

func ParseFilters

func ParseFilters(args ParsedArgs) []Filter

ParseFilters takes a ParsedArgs struct and extracts its filters field. It then separates all compound filters into a set of equivalent single filters with their respective filter types assigned. ID range filters (ie. "5-9") get additional parsing into a struct with their integer values

NOTE: Original order of filters is not preserved

Preconditions: Assumes all filters in args passed validation check by parser.ValidateFilter() regardless of type.

func (Filter) IsMandated

func (f Filter) IsMandated() bool

IsMandated returns true if filter is a TAG type, all other types return false by default. Mandated in this context refers to SQL queries for this filter to have implicit 'and' behavior rather than default 'or'.

func (Filter) String

func (f Filter) String() string

type FilterList

type FilterList []Filter

func (*FilterList) Map

func (l *FilterList) Map() map[string]string

Preconditions: result only defined for KVs type FilterList

func (*FilterList) String

func (l *FilterList) String() []string

type FilterType

type FilterType int
const (
	ID FilterType = iota
	RANGE
	UUID
	TAG
	GROUP
	KV
	CUSTOM
)

type ParsedArgs

type ParsedArgs struct {
	Binary  string
	Command string
	Filters []string
	Mods    []string
	// OriginalInput is the original command line input from os.Args
	// before parsing/reordering. Preserved mostly for debugging
	// since os.Args is overwritten after reordering
	OriginalInput string
}

ParsedArgs are the command line arguments separated into categories split at parsed command

func ParseArgs

func ParseArgs(args []string, commands map[string]bool) (ParsedArgs, error)

ParseArgs returns a struct for the parsed command line arguments. Returns an error if filters are malformed. Does not check if filters or mods are relevant to the individual subcommands requirements' Takes valid command line arguments in the form <program> <filters> <command> <mods> and separates them into a field in their respective types. Also returns args reordered into Cobra cli expected format <program> <command> <args>

func (ParsedArgs) Args

func (args ParsedArgs) Args(binaryName string) []string

Args outputs the cobra CLI friendly reordered arguments in order of <program> <command> <filters> <mods>

func (ParsedArgs) CobraArgs

func (args ParsedArgs) CobraArgs() []string

CobraArgs returns the args with mods and filters stripped in a cobra CLI friendly manner. The "help" command is different in which it will preserve one mod (the subcommand) if exists

type RawFilter

type RawFilter string

RawFilter is a command line filter in string based form of any type

Precondition: any value converted to RawFilter must already passed parser.ValidateFilter() check and is at least one valid filter type. Passing an invalid filter will result in incorrect behavior

func (RawFilter) String

func (rf RawFilter) String() string

type SearchFilters

type SearchFilters struct {
	IDs    FilterList
	Ranges FilterList
	UUIDs  FilterList
	Tags   FilterList
	Groups FilterList
	KVs    FilterList
}

SearchFtilers is an object to hold arrays of all filter types found in args separated into their respective buckets

func NewSearchFilters

func NewSearchFilters(v *viper.Viper, filters []Filter, usesDefaultFilters bool) SearchFilters

func ParseSearchFilters

func ParseSearchFilters(v *viper.Viper, parsedArgs ParsedArgs, usesDefaultFilters bool) SearchFilters

func (SearchFilters) CanOverrideGroups

func (sf SearchFilters) CanOverrideGroups() bool

func (SearchFilters) CanOverrideTags

func (sf SearchFilters) CanOverrideTags() bool

func (*SearchFilters) Limit

func (sf *SearchFilters) Limit() (int, error)

func (*SearchFilters) Size

func (sf *SearchFilters) Size() int

func (*SearchFilters) TryOverrideGroups

func (sf *SearchFilters) TryOverrideGroups(v *viper.Viper)

func (*SearchFilters) TryOverrideTags

func (sf *SearchFilters) TryOverrideTags(v *viper.Viper)

Jump to

Keyboard shortcuts

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