executable

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const AnyVersionConstraint = ">= 0.0.0-0"

Variables

View Source
var (
	ErrCheckingForExecutable = fmt.Errorf("error checking for executable")
	ErrExecutableNotFound    = fmt.Errorf("executable not found")
)
View Source
var DefaultDiscoverContainerOptions = DiscoverContainerOptions{
	Packages: []ContainerPackage{},
}
View Source
var DefaultDiscoverNixOptions = DiscoverNixStoreOptions{
	Packages: []NixStorePackage{
		{
			Name:       "openjdk",
			Expression: `([a-z0-9]{32})-(openjdk)-(\d+\.\d+\.\d+.+)`,
			Env: map[string]string{
				"JAVA_HOME": "{{EXECUTABLE_PKG_DIR}}/lib/openjdk",
			},
		},
		{
			Name:       "go",
			Expression: `([a-z0-9]{32})-(go)-(\d+\.\d+\.\d+)`,
			Env: map[string]string{
				"GOPATH":     "$HOME/go",
				"GOMODCACHE": "$HOME/go/pkg/mod",
				"GOCACHE":    "$HOME/.cache/go-build",
			},
		},
		{
			Name:       "zig",
			Expression: `([a-z0-9]{32})-(zig)-(\d+\.\d+\.\d+)`,
		},
		{
			Name:       "helm",
			Expression: `([a-z0-9]{32})-(kubernetes-helm)-(\d+\.\d+\.\d+)`,
		},
		{
			Name:       "helmfile",
			Expression: `([a-z0-9]{32})-(helmfile)-(\d+\.\d+\.\d+)`,
		},
		{
			Name:       "kubectl",
			Expression: `([a-z0-9]{32})-(kubectl)-(\d+\.\d+\.\d+)`,
		},
		{
			Name:             "openshift",
			ProgramsProvided: []string{"oc"},
			Expression:       `([a-z0-9]{32})-(openshift)-(\d+\.\d+\.\d+)`,
		},
		{
			Name:       "ansible",
			Expression: `([a-z0-9]{32})-(ansible)-(\d+\.\d+\.\d+)`,
		},
		{
			Name:             "minio-client",
			ProgramsProvided: []string{"mc"},
			Expression:       `([a-z0-9]{32})-(minio-client)-(\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z)`,
		},
	},
	VersionLookupCommand: true,
}
View Source
var DefaultDiscoverNixShellOptions = DiscoverNixShellOptions{
	Packages: []NixShellPackage{
		{
			Name:             "openjdk",
			ProgramsProvided: []string{"java", "javac", "javadoc", "javap"},
			Env: map[string]string{
				"JAVA_HOME": "{{EXECUTABLE_PKG_DIR}}/lib/openjdk",
			},
		},
		{
			Name:             "go",
			ProgramsProvided: []string{"go"},
			Env: map[string]string{
				"GOPATH":     "$HOME/go",
				"GOMODCACHE": "$HOME/go/pkg/mod",
				"GOCACHE":    "$HOME/.cache/go-build",
			},
		},
		{
			Name:             "zig",
			ProgramsProvided: []string{"zig"},
		},
		{
			Name:             "kubernetes-helm",
			ProgramsProvided: []string{"helm"},
		},
		{
			Name:             "helmfile",
			ProgramsProvided: []string{"helmfile"},
		},
		{
			Name:             "kubectl",
			ProgramsProvided: []string{"kubectl"},
		},
		{
			Name:             "openshift",
			ProgramsProvided: []string{"oc"},
		},
		{
			Name:             "ansible",
			ProgramsProvided: []string{"ansible"},
		},
		{
			Name:             "minio-client",
			ProgramsProvided: []string{"mc"},
		},
	},
	VersionLookupCommand: true,
}
View Source
var DefaultDiscoverPathOptions = DiscoverPathOptions{
	LookupRules: []PathDiscoveryRule{
		{
			Binary: []string{"podman"},
			Lookup: []PathDiscoveryRuleLookup{
				{
					Key:            "PATH",
					Version:        "0.0.0",
					VersionCommand: "-v",
					VersionRegex:   `(?m)podman version (\d+\.\d+\.\d+)`,
				},
			},
		},
		{
			Binary: []string{"java"},
			Lookup: []PathDiscoveryRuleLookup{
				{
					Key:            "PATH",
					Version:        "0.0.0",
					VersionCommand: "--version",
					VersionRegex:   `(?m)openjdk (\d+\.\d+\.\d+)`,
				},
				{
					Key:            "JAVA_HOME_8",
					KeyAliases:     []string{"JAVA_HOME_8_X64", "JAVA_HOME_8_X86"},
					Directory:      "bin",
					Version:        "8.0.0",
					VersionCommand: "--version",
					VersionRegex:   `(?m)openjdk (\d+\.\d+\.\d+)`,
				},
				{
					Key:            "JAVA_HOME_11",
					KeyAliases:     []string{"JAVA_HOME_11_X64", "JAVA_HOME_11_X86"},
					Directory:      "bin",
					Version:        "11.0.0",
					VersionCommand: "--version",
					VersionRegex:   `(?m)openjdk (\d+\.\d+\.\d+)`,
				},
				{
					Key:            "JAVA_HOME_17",
					KeyAliases:     []string{"JAVA_HOME_17_X64", "JAVA_HOME_17_X86"},
					Directory:      "bin",
					Version:        "17.0.0",
					VersionCommand: "--version",
					VersionRegex:   `(?m)openjdk (\d+\.\d+\.\d+)`,
				},
				{
					Key:            "JAVA_HOME_21",
					KeyAliases:     []string{"JAVA_HOME_21_X64", "JAVA_HOME_21_X86"},
					Directory:      "bin",
					Version:        "21.0.0",
					VersionCommand: "--version",
					VersionRegex:   `(?m)openjdk (\d+\.\d+\.\d+)`,
				},
			},
			Env: map[string]string{
				"JAVA_HOME": "{{EXECUTABLE_PKG_DIR}}",
			},
		},
		{
			Binary: []string{"go"},
			Lookup: []PathDiscoveryRuleLookup{
				{
					Key:            "PATH",
					Version:        "0.0.0",
					VersionCommand: "version",
					VersionRegex:   `(?m)go(\d+\.\d+\.\d+)`,
				},
				{
					Key:            "GOROOT_1_21",
					KeyAliases:     []string{"GOROOT_1_21_X64"},
					Version:        "1.21.0",
					VersionCommand: "version",
					VersionRegex:   `(?m)go(\d+\.\d+\.\d+)`,
				},
				{
					Key:            "GOROOT_1_22",
					KeyAliases:     []string{"GOROOT_1_22_X64"},
					Version:        "1.22.0",
					VersionCommand: "version",
					VersionRegex:   `(?m)go(\d+\.\d+\.\d+)`,
				},
				{
					Key:            "GOROOT_1_23",
					KeyAliases:     []string{"GOROOT_1_23_X64"},
					Version:        "1.23.0",
					VersionCommand: "version",
					VersionRegex:   `(?m)go(\d+\.\d+\.\d+)`,
				},
			},
			Env: map[string]string{
				"GOPATH":     "$HOME/go",
				"GOMODCACHE": "$HOME/go/pkg/mod",
				"GOCACHE":    "$HOME/.cache/go-build",
			},
		},
	},
	VersionLookupCommand: true,
}

Functions

func ReplaceCommandPlaceholders

func ReplaceCommandPlaceholders(input string, env map[string]string) string

ReplaceCommandPlaceholders replaces env placeholders in a command

func ResetExecutableCache

func ResetExecutableCache()

ResetExecutableCache clears the candidate cache

func UpdateExecutableCache

func UpdateExecutableCache(candidates []Executable) error

UpdateExecutableCache persists the candidates into a file

Types

type BaseCandidate

type BaseCandidate struct {
	Name    string        `yaml:"name,required"`
	Version string        `yaml:"version,required"`
	Type    CandidateType `yaml:"type,required"`
}

func (BaseCandidate) GetName

func (c BaseCandidate) GetName() string

func (BaseCandidate) GetType

func (c BaseCandidate) GetType() CandidateType

func (BaseCandidate) GetUri

func (c BaseCandidate) GetUri() string

func (BaseCandidate) GetVersion

func (c BaseCandidate) GetVersion() string

func (BaseCandidate) Run

func (c BaseCandidate) Run(opts RunParameters) (string, string, error)

type CandidateFilter

type CandidateFilter struct {
	Types             []CandidateType
	Executable        string
	VersionPreference PreferVersion
	VersionConstraint string
}

type CandidateType

type CandidateType string
const (
	ExecutionExec      CandidateType = "exec"
	ExecutionContainer CandidateType = "container"
	ExecutionNixStore  CandidateType = "nix-store"
	ExecutionNixShell  CandidateType = "nix-shell"
)

func ToCandidateTypes

func ToCandidateTypes(types []string) []CandidateType

type ContainerCache

type ContainerCache struct {
	ID            string `yaml:"id" json:"id"`
	ContainerPath string `yaml:"dir" json:"dir"`
	MountType     string `yaml:"type,omitempty" json:"type,omitempty"`
}

type ContainerCandidate

type ContainerCandidate struct {
	BaseCandidate
	Image      string `yaml:"package,omitempty"`
	ImageCache []ContainerCache
	Mounts     []ContainerMount
	Security   ContainerSecurity
	Entrypoint *string
	Certs      []ContainerCerts `yaml:"certs,omitempty"`
}

ContainerCandidate is used for the execution using container images

func (ContainerCandidate) GetUri

func (c ContainerCandidate) GetUri() string

func (ContainerCandidate) Run

type ContainerCerts

type ContainerCerts struct {
	Type          string `yaml:"type"`
	ContainerPath string `yaml:"dir"`
}

type ContainerMount

type ContainerMount struct {
	Src  string `yaml:"src"`
	Dest string `yaml:"dest"`
}

type ContainerPackage

type ContainerPackage struct {
	Binary []string
	Image  string
	Cache  []ContainerCache
}

type ContainerSecurity

type ContainerSecurity struct {
	Capabilities []string `yaml:"capabilities,omitempty"`
	Privileged   bool     `yaml:"privileged,omitempty"`
}

type DiscoverContainerOptions

type DiscoverContainerOptions struct {
	Packages []ContainerPackage
}

type DiscoverNixShellOptions added in v0.6.0

type DiscoverNixShellOptions struct {
	Packages             []NixShellPackage
	VersionLookupCommand bool
}

type DiscoverNixStoreOptions added in v0.6.0

type DiscoverNixStoreOptions struct {
	Packages             []NixStorePackage
	VersionLookupCommand bool
}

type DiscoverPathOptions

type DiscoverPathOptions struct {
	LookupRules          []PathDiscoveryRule
	VersionLookupCommand bool
}

type ExecCandidate

type ExecCandidate struct {
	BaseCandidate
	AbsolutePath string            `json:"absolute-path,omitempty"`
	Env          map[string]string `json:"env,omitempty"`
}

ExecCandidate is used for the execution using locally installed binaries

func (ExecCandidate) Run

func (c ExecCandidate) Run(opts RunParameters) (string, string, error)

type Executable added in v0.6.0

type Executable interface {
	GetName() string
	GetVersion() string
	GetType() CandidateType
	GetUri() string // GetUri returns the URI of the candidate, for auditing purposes
	Run(opts RunParameters) (string, string, error)
}

func DiscoverContainerCandidates

func DiscoverContainerCandidates(opts *DiscoverContainerOptions) []Executable

func DiscoverExecutables

func DiscoverExecutables() ([]Executable, error)

func DiscoverNixShellExecutables added in v0.6.0

func DiscoverNixShellExecutables(opts *DiscoverNixShellOptions) []Executable

func DiscoverNixStoreExecutables added in v0.6.0

func DiscoverNixStoreExecutables(opts *DiscoverNixStoreOptions) []Executable

func DiscoverPathCandidates

func DiscoverPathCandidates(opts *DiscoverPathOptions) []Executable

func FromTypedCandidate added in v0.6.0

func FromTypedCandidate(typed TypedCandidate) (Executable, error)

func LoadCachedExecutables

func LoadCachedExecutables() ([]Executable, error)

LoadCachedExecutables loads the candidates from the cache

func LoadExecutables

func LoadExecutables() ([]Executable, error)

func SelectCandidate

func SelectCandidate(candidates []Executable, options CandidateFilter) *Executable

SelectCandidate selects the first candidate that matches the given requirements

type NixShellCandidate

type NixShellCandidate struct {
	BaseCandidate
	Package            string            `yaml:"package,omitempty"`
	PackageVersion     string            `yaml:"package-version,omitempty"`
	AdditionalPackages []string          `yaml:"additional-packages,omitempty"`
	Channel            string            `yaml:"channel,omitempty"`
	Env                map[string]string `json:"env,omitempty"`
}

NixShellCandidate is used for the execution using nix-shell

func (NixShellCandidate) GetUri added in v0.6.0

func (c NixShellCandidate) GetUri() string

func (NixShellCandidate) Run

type NixShellPackage added in v0.6.0

type NixShellPackage struct {
	Name             string
	ProgramsProvided []string
	Env              map[string]string
}

type NixStoreCandidate

type NixStoreCandidate struct {
	BaseCandidate
	AbsolutePath   string            `json:"absolute-path,omitempty"`
	Package        string            `json:"package,omitempty"`
	PackageVersion string            `json:"package-version,omitempty"`
	Env            map[string]string `json:"env,omitempty"`
}

NixStoreCandidate is used for the execution using binaries in the nix store

func (NixStoreCandidate) GetUri

func (c NixStoreCandidate) GetUri() string

func (NixStoreCandidate) Run

type NixStorePackage added in v0.6.0

type NixStorePackage struct {
	Name             string
	ProgramsProvided []string
	Expression       string
	Env              map[string]string
}

type PathDiscoveryRule

type PathDiscoveryRule struct {
	Binary []string
	Lookup []PathDiscoveryRuleLookup
	Env    map[string]string
}

type PathDiscoveryRuleLookup

type PathDiscoveryRuleLookup struct {
	Key            string   // env name
	KeyAliases     []string `yaml:"key-aliases"`
	Directory      string   // directory
	Version        string   // version
	VersionCommand string   `yaml:"version-command"`
	VersionRegex   string   `yaml:"version-regex"`
}

type PreferVersion

type PreferVersion string
const (
	PreferHighest PreferVersion = "highest"
	PreferLowest  PreferVersion = "lowest"
)

type RunParameters

type RunParameters struct {
	Executable    string
	Args          []string
	Env           map[string]string
	RootDir       string
	WorkDir       string
	TempDir       string
	Ports         []int
	Stdin         io.Reader
	Stdout        io.Writer
	Stderr        io.Writer
	CaptureOutput bool
	HideStdOut    bool
	HideStdErr    bool
}

type TypedCandidate added in v0.6.0

type TypedCandidate struct {
	Type      string          `json:"type"`      // Type required for unmarshalling
	Candidate json.RawMessage `json:"candidate"` // Candidate contains the actual candidate data
}

func ToTypedCandidate added in v0.6.0

func ToTypedCandidate(candidate Executable) (TypedCandidate, error)

Jump to

Keyboard shortcuts

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