cmd

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package cmd provides command file specs and structures used by an rce.Server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCommandNotFound  = errors.New("command not found")
	ErrDuplicateName    = errors.New("duplicate command name found")
	ErrDuplicateCommand = errors.New("duplicate command in repo")
	ErrRelativePath     = errors.New("command uses relative path")
	ErrNoCommands       = errors.New("no commands parsed")
)

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	Id   string
	Name string
	Cmd  *gocmd.Cmd
	Args []string
}

Cmd represents a running command.

func NewCmd

func NewCmd(s Spec, args []string) *Cmd

NewCmd makes a new Cmd with the given Spec and args, and assigns it an ID.

type Repo

type Repo interface {
	// Add new command to repo, identified by Cmd.Id.
	Add(*Cmd) error

	// Remove command from repo.
	Remove(id string) error

	// Return command from repo, or nil if doesn't exist.
	Get(id string) *Cmd

	// Return list of all command IDs in repos. The list is a copy and can change
	// between calls.
	All() []string
}

Repo is a thread-safe collection of Cmd used by an rce.Server.

func NewRepo

func NewRepo() Repo

NewRepo makes a new empty Repo.

type Runnable

type Runnable []Spec

func LoadCommands

func LoadCommands(file string) (Runnable, error)

LoadCommands loads all command Spec from a YAML config file. The file structure is:

  ---
  commands:
    - name: exit.zero
      exec: [/usr/bin/true]
	- name: exit.one
	  exec:
        - /bin/false
        - some-arg

Name must be unique. The first exec value must be an absolute command path. Additional exec values are optional and always included in the order listed.

func (Runnable) FindByName

func (r Runnable) FindByName(name string) (Spec, error)

FindByName returns a Spec matching the given name.

func (Runnable) Validate

func (r Runnable) Validate() error

Validate validates a list of Spec and returns an error if any invalid.

func (Runnable) ValidateNoDuplicates

func (r Runnable) ValidateNoDuplicates() error

ValidateNoDuplicates returns an ErrDuplicateName if the list of Spec contains duplicate names.

type Spec

type Spec struct {
	// Short, unique name of the command. Example: "lxc-ls". This is only an alias.
	Name string `yaml:"name"`

	// Exec args, first being the absolute cmd path. Example: ["/usr/bin/lxc-ls", "--active"].
	Exec []string `yaml:"exec"`
}

Spec represents one command in a YAML config file. See LoadCommands for the file structure.

func (Spec) Args

func (c Spec) Args() []string

Args returns the args part of a Spec.

func (Spec) Path

func (c Spec) Path() string

Path returns the path part of a Spec.

func (Spec) ValidateAbsPath

func (c Spec) ValidateAbsPath() error

ValidateAbsPath returns ErrRelativePath if the Spec's path is not an absolute path.

Jump to

Keyboard shortcuts

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