sourcerer

package
v0.0.0-...-b47e1f0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2024 License: MIT Imports: 18 Imported by: 17

Documentation

Overview

package sourcerer sources CLI commands in a shell environment. See the `main` function in github.com/leep-frog/command/examples/source.go for an example of how to define a source file that uses this.

Index

Constants

View Source
const (
	AutocompleteBranchName              = "autocomplete"
	GenerateAutocompleteSetupBranchName = "generate-autocomplete-setup"
	ExecuteBranchName                   = "execute"
	ListBranchName                      = "listCLIs"
	SourceBranchName                    = "source"
	UsageBranchName                     = "usage"

	BuiltInCommandParameter = "builtin"
)
View Source
const (
	ExecutableFileGetProcessorName = "GO_EXECUTABLE_FILE"
)
View Source
const (
	// RootDirectoryEnvVar is the directory in which all artifact files needed will be created and stored.
	RootDirectoryEnvVar = "COMMAND_CLI_OUTPUT_DIR"
)

Variables

View Source
var (
	CurrentOS = func() OS {
		curOS, ok := os.LookupEnv("COMMAND_CLI_OS_OVERRIDE")
		if !ok {
			curOS = runtime.GOOS
		}

		for _, os := range oses {
			if os.Name() == curOS {
				return os
			}
		}
		panic(fmt.Sprintf("Unsupported leep-frog/command os: %q", curOS))
	}()
)
View Source
var (
	// NosortString returns the complete option to ignore sorting.
	// It returns nothing if the IGNORE_NOSORT environment variable is set.
	NosortString = func() string {
		if _, ignore := os.LookupEnv("IGNORE_NOSORT"); ignore {
			return ""
		}
		return "-o nosort"
	}
)
View Source
var (
	RelevantPackages = []string{
		"cd",
		"command",

		"gocli",
		"grep",

		"qmkwrapper",
		"replace",

		"sourcecontrol",
	}
)

Functions

func AliasSourcery

func AliasSourcery(goExecutable string, as ...*Aliaser) []string

AliasSourcery outputs all alias source commands to the provided `command.Output`.

func ExecutableFileGetProcessor

func ExecutableFileGetProcessor() *commander.GetProcessor[string]

ExecutableFileGetProcessor returns a `commander.GetProcessor` that sets and gets the full go executable file path.

func FileStringFromCLI

func FileStringFromCLI(cli string) string

FileStringFromCLI returns a bash command that retrieves the binary file that is actually executed for a leep-frog-generated CLI.

func FileStringFromCLIZSH

func FileStringFromCLIZSH(cli string) string

func RunCLI

func RunCLI(cli CLI) int

RunCLI runs an individual CLI, thus making the go executable file the only setup needed.

func Source

func Source(targetName string, clis []CLI, opts ...Option) int

Source generates the bash source file for a list of CLIs.

func StubExecutableFile

func StubExecutableFile(t *testing.T, filepath string, err error)

StubExecutableFile stubs the executable file returned by ExecutableFileGetProcessor()

func ValueByOS

func ValueByOS[T any](values map[string]T) T

ValueByOS will return the value that is associated with the current OS. If there is no match, then the function will panic.

NOTE: Ideally, this should not be used and any instances of this should be added into the OS interface above.

Types

type Aliaser

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

func NewAliaser

func NewAliaser(alias string, cli string, values ...string) *Aliaser

type AliaserCommand

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

AliaserCommand creates an alias for another arg

func (*AliaserCommand) Changed

func (*AliaserCommand) Changed() bool

func (*AliaserCommand) Name

func (*AliaserCommand) Name() string

func (*AliaserCommand) Node

func (ac *AliaserCommand) Node() command.Node

func (*AliaserCommand) Setup

func (*AliaserCommand) Setup() []string

type CLI

type CLI interface {

	// Name is the name of the alias command to use for this CLI.
	Name() string
	// Node returns the command node for the CLI. This is where the CLI's logic lives.
	Node() command.Node
	// Changed indicates whether or not the CLI has changed after execution.
	// If true, the CLI's value will be save to the cache.
	Changed() bool
	// Setup describes a set of commands that will be run in bash prior to the CLI.
	// The output from the commands will be stored in a file whose name will be
	// stored in the `Data` object. See the following methods:
	// `Data.SetupOutputFile()` returns the file name.
	// `Data.SetupOutputString()` returns the file contents as a string.
	// `Data.SetupOutputContents()` returns the file contents as a string slice.
	Setup() []string
}

CLI provides a way to construct CLIs in go, with tab-completion. Note, this has to be an interface (as opposed to a struct) because of the Load function.

func ToCLI

func ToCLI(name string, root command.Node) CLI

ToCLI converts a node to a CLI.

type Debugger

type Debugger struct{}

func (*Debugger) Changed

func (*Debugger) Changed() bool

func (*Debugger) Name

func (*Debugger) Name() string

func (*Debugger) Node

func (*Debugger) Node() command.Node

func (*Debugger) Setup

func (*Debugger) Setup() []string

type GoLeep

type GoLeep struct{}

GoLeep is a CLI that runs command nodes that are defined in "main" packages.

func (*GoLeep) Aliasers

func (gl *GoLeep) Aliasers() Option

func (*GoLeep) Changed

func (gl *GoLeep) Changed() bool

func (*GoLeep) Name

func (gl *GoLeep) Name() string

func (*GoLeep) Node

func (gl *GoLeep) Node() command.Node

func (*GoLeep) Setup

func (gl *GoLeep) Setup() []string

type OS

type OS interface {
	command.OS

	// Name is the operating system as specified by runtime.GOOS
	Name() string

	// ExecutableFileSuffix is the suffix to add for executable files (e.g. `.exe` in Windows)
	ExecutableFileSuffix() string

	// SourceableFile is the file basename to use for sourceable files (e.g. `myCLI_loader.sh` in linux)
	SourceableFile(target string) string

	// SourceSetup returns the code that should be run and/or added to a user's terminal profile
	// after successfully running `go run . source ...`
	SourceSetup(sourceableFile, targetName, goRunSourceCommand, userDir string) []string

	// FunctionWrap wraps the provided commands in another function.
	FunctionWrap(name, fn string) string

	// HandleAutocompleteSuccess should output the suggestions for autocomplete consumption
	HandleAutocompleteSuccess(command.Output, *command.Autocompletion)
	// HandleAutocompleteError should output error info on `Autocomplete` failure
	HandleAutocompleteError(output command.Output, compType int, err error)

	// SourceFileCommand should return the command to source the provided file.
	SourceFileCommand(sourcerersDir, targetName string) string

	// RecursiveCopyDir should return the command to recusrively copy contents from src to dst.
	RecursiveCopyDir(src, dst string) string

	// RegisterCLIs generates the code for
	RegisterCLIs(builtin bool, goExecutable, targetName string, cli []CLI) []string

	// RegisterRunCLIAutocomplete generates the shell code to source to set up
	// autocomplete for the provided alias.
	RegisterRunCLIAutocomplete(goExecutable, alias string) []string

	// RegisterAliasers
	GlobalAliaserFunc(goExecutable string) []string
	VerifyAliaser(*Aliaser) []string
	RegisterAliaser(goExecutable string, a *Aliaser) []string
}

func Linux

func Linux() OS

func Windows

func Windows() OS

type Option

type Option interface {
	// contains filtered or unexported methods
}

func Aliasers

func Aliasers(m map[string][]string) Option

type UpdateLeepPackageCommand

type UpdateLeepPackageCommand struct{}

UpdateLeepPackageCommand is a CLI for updating github.com/leep-frog packages

func (*UpdateLeepPackageCommand) Changed

func (*UpdateLeepPackageCommand) Changed() bool

func (*UpdateLeepPackageCommand) Name

func (*UpdateLeepPackageCommand) Node

func (*UpdateLeepPackageCommand) Setup

func (*UpdateLeepPackageCommand) Setup() []string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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