shell

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SessionTokenPrefix = ".session."
)

Constants

Variables

This section is empty.

Functions

func ResetSessionToken added in v0.5.6

func ResetSessionToken()

Reset the session token - used primarily for testing

Types

type DefaultShell

type DefaultShell struct {
	Shell
	// contains filtered or unexported fields
}

DefaultShell is the default implementation of the Shell interface

func NewDefaultShell

func NewDefaultShell(injector di.Injector) *DefaultShell

NewDefaultShell creates a new instance of DefaultShell

func (*DefaultShell) AddCurrentDirToTrustedFile added in v0.3.0

func (s *DefaultShell) AddCurrentDirToTrustedFile() error

Adds the current directory to a trusted list stored in a file. Creates necessary directories if they don't exist. Checks if the directory is already trusted before adding.

func (*DefaultShell) CheckResetFlags added in v0.5.6

func (s *DefaultShell) CheckResetFlags() (bool, error)

CheckResetFlags checks if a reset signal file exists for the current session token. It returns true if the specific session token file exists and always removes all .session.* files.

func (*DefaultShell) CheckTrustedDirectory added in v0.3.0

func (s *DefaultShell) CheckTrustedDirectory() error

Check if the current directory is in the trusted file list.

func (*DefaultShell) Exec

func (s *DefaultShell) Exec(command string, args ...string) (string, error)

Exec runs a command with args, capturing stdout and stderr. It prints output and returns stdout as a string. If the command is "sudo", it connects stdin to the terminal for password input.

func (*DefaultShell) ExecProgress

func (s *DefaultShell) ExecProgress(message string, command string, args ...string) (string, error)

ExecProgress is a method of the DefaultShell struct that executes a command with a progress indicator. It takes a message, a command, and arguments, using the Exec method if verbose mode is enabled. Otherwise, it captures stdout and stderr with pipes and uses a spinner to show progress. The method returns the command's stdout as a string and any error encountered.

func (*DefaultShell) ExecSilent

func (s *DefaultShell) ExecSilent(command string, args ...string) (string, error)

ExecSilent is a method that runs a command quietly, capturing its output. It returns the command's stdout as a string and any error encountered.

func (*DefaultShell) ExecSudo

func (s *DefaultShell) ExecSudo(message string, command string, args ...string) (string, error)

ExecSudo runs a command with 'sudo', ensuring elevated privileges. It handles password prompts by connecting to the terminal and captures the command's output. If verbose mode is enabled, it prints a message to stderr. The function returns the command's stdout or an error if execution fails.

func (*DefaultShell) GetProjectRoot

func (s *DefaultShell) GetProjectRoot() (string, error)

GetProjectRoot finds the project root. It checks for a cached root first. If not found, it looks for "windsor.yaml" or "windsor.yml" in the current directory and its parents up to a maximum depth. Returns the root path or an empty string if not found.

func (*DefaultShell) GetSessionToken added in v0.5.6

func (s *DefaultShell) GetSessionToken() (string, error)

GetSessionToken retrieves or generates a session token. It first checks if a token is already stored in memory. If not, it looks for a token in the environment variable. If no token is found in the environment, it generates a new token.

func (*DefaultShell) Initialize

func (s *DefaultShell) Initialize() error

Initialize initializes the shell

func (*DefaultShell) InstallHook

func (s *DefaultShell) InstallHook(shellName string) error

InstallHook sets up a shell hook for a specified shell using a template with the Windsor path. It returns an error if the shell is unsupported. For PowerShell, it formats the script into a single line.

func (*DefaultShell) PrintAlias

func (s *DefaultShell) PrintAlias(aliases map[string]string)

PrintAlias prints the aliases for the shell.

func (*DefaultShell) PrintEnvVars

func (s *DefaultShell) PrintEnvVars(envVars map[string]string)

PrintEnvVars prints the provided environment variables in a sorted order. If the value of an environment variable is an empty string, it will print an unset command.

func (*DefaultShell) Reset added in v0.5.6

func (s *DefaultShell) Reset()

Reset removes all managed environment variables and aliases. It uses the environment variables "WINDSOR_MANAGED_ENV" and "WINDSOR_MANAGED_ALIAS" to retrieve the previous set of managed environment variables and aliases, respectively. These environment variables represent the previous set of managed values that need to be reset.

func (*DefaultShell) SetVerbosity added in v0.2.1

func (s *DefaultShell) SetVerbosity(verbose bool)

SetVerbosity sets the verbosity flag

func (*DefaultShell) UnsetAlias added in v0.5.6

func (s *DefaultShell) UnsetAlias(aliases []string)

UnsetAlias generates commands to unset multiple aliases. For Unix shells, this produces a separate 'unalias' command for each alias.

func (*DefaultShell) UnsetEnvs added in v0.5.6

func (s *DefaultShell) UnsetEnvs(envVars []string)

UnsetEnvs generates a command to unset multiple environment variables. For Unix shells, this produces a single 'unset' command with all variables in one line.

func (*DefaultShell) WriteResetToken added in v0.5.6

func (s *DefaultShell) WriteResetToken() (string, error)

WriteResetToken writes a reset token file based on the WINDSOR_SESSION_TOKEN environment variable. If the environment variable doesn't exist, no file is written. Returns the path to the written file or an empty string if no file was written.

type HookContext

type HookContext struct {
	// SelfPath is the unescaped absolute path to direnv
	SelfPath string
}

HookContext are the variables available during hook template evaluation

type MockShell

type MockShell struct {
	DefaultShell
	InitializeFunc                 func() error
	PrintEnvVarsFunc               func(envVars map[string]string)
	PrintAliasFunc                 func(envVars map[string]string)
	GetProjectRootFunc             func() (string, error)
	ExecFunc                       func(command string, args ...string) (string, error)
	ExecSilentFunc                 func(command string, args ...string) (string, error)
	ExecProgressFunc               func(message string, command string, args ...string) (string, error)
	ExecSudoFunc                   func(message string, command string, args ...string) (string, error)
	InstallHookFunc                func(shellName string) error
	SetVerbosityFunc               func(verbose bool)
	AddCurrentDirToTrustedFileFunc func() error
	CheckTrustedDirectoryFunc      func() error
	UnsetEnvsFunc                  func(envVars []string)
	UnsetAliasFunc                 func(aliases []string)
	WriteResetTokenFunc            func() (string, error)
	GetSessionTokenFunc            func() (string, error)
	CheckResetFlagsFunc            func() (bool, error)
	ResetFunc                      func()
}

MockShell is a struct that simulates a shell environment for testing purposes.

func NewMockShell

func NewMockShell(injectors ...di.Injector) *MockShell

NewMockShell creates a new instance of MockShell. If injector is provided, it sets the injector on MockShell.

func (*MockShell) AddCurrentDirToTrustedFile added in v0.3.0

func (s *MockShell) AddCurrentDirToTrustedFile() error

AddCurrentDirToTrustedFile calls the custom AddCurrentDirToTrustedFileFunc if provided.

func (*MockShell) CheckResetFlags added in v0.5.6

func (s *MockShell) CheckResetFlags() (bool, error)

CheckResetFlags checks if a reset signal file exists for the current session

func (*MockShell) CheckTrustedDirectory added in v0.3.0

func (s *MockShell) CheckTrustedDirectory() error

CheckTrustedDirectory calls the custom CheckTrustedDirectoryFunc if provided.

func (*MockShell) Exec

func (s *MockShell) Exec(command string, args ...string) (string, error)

Exec calls the custom ExecFunc if provided.

func (*MockShell) ExecProgress

func (s *MockShell) ExecProgress(message string, command string, args ...string) (string, error)

ExecProgress calls the custom ExecProgressFunc if provided.

func (*MockShell) ExecSilent

func (s *MockShell) ExecSilent(command string, args ...string) (string, error)

ExecSilent calls the custom ExecSilentFunc if provided.

func (*MockShell) ExecSudo

func (s *MockShell) ExecSudo(message string, command string, args ...string) (string, error)

ExecSudo calls the custom ExecSudoFunc if provided.

func (*MockShell) GetProjectRoot

func (s *MockShell) GetProjectRoot() (string, error)

GetProjectRoot calls the custom GetProjectRootFunc if provided.

func (*MockShell) GetSessionToken added in v0.5.6

func (s *MockShell) GetSessionToken() (string, error)

GetSessionToken retrieves or generates a session token

func (*MockShell) Initialize

func (s *MockShell) Initialize() error

Initialize calls the custom InitializeFunc if provided.

func (*MockShell) InstallHook

func (s *MockShell) InstallHook(shellName string) error

InstallHook calls the custom InstallHook if provided.

func (*MockShell) PrintAlias

func (s *MockShell) PrintAlias(envVars map[string]string)

PrintAlias calls the custom PrintAliasFunc if provided.

func (*MockShell) PrintEnvVars

func (s *MockShell) PrintEnvVars(envVars map[string]string)

PrintEnvVars calls the custom PrintEnvVarsFunc if provided.

func (*MockShell) Reset added in v0.5.6

func (s *MockShell) Reset()

Reset calls the custom ResetFunc if provided.

func (*MockShell) SetVerbosity added in v0.2.1

func (s *MockShell) SetVerbosity(verbose bool)

SetVerbosity calls the custom SetVerbosityFunc if provided.

func (*MockShell) UnsetAlias added in v0.5.6

func (s *MockShell) UnsetAlias(aliases []string)

UnsetAlias calls the custom UnsetAliasFunc if provided.

func (*MockShell) UnsetEnvs added in v0.5.6

func (s *MockShell) UnsetEnvs(envVars []string)

UnsetEnvs calls the custom UnsetEnvsFunc if provided.

func (*MockShell) WriteResetToken added in v0.5.6

func (s *MockShell) WriteResetToken() (string, error)

WriteResetToken writes a reset token file

type SecureShell

type SecureShell struct {
	DefaultShell
	// contains filtered or unexported fields
}

SecureShell implements the Shell interface using SSH.

func NewSecureShell

func NewSecureShell(injector di.Injector) *SecureShell

NewSecureShell creates a new instance of SecureShell.

func (*SecureShell) Exec

func (s *SecureShell) Exec(command string, args ...string) (string, error)

Exec executes a command on the remote host via SSH and returns its output as a string.

func (*SecureShell) ExecProgress

func (s *SecureShell) ExecProgress(message string, command string, args ...string) (string, error)

ExecProgress executes a command and returns its output as a string

func (*SecureShell) ExecSilent

func (s *SecureShell) ExecSilent(command string, args ...string) (string, error)

ExecSilent executes a command and returns its output as a string without printing to stdout or stderr

func (*SecureShell) Initialize

func (s *SecureShell) Initialize() error

Initialize initializes the SecureShell instance.

type Shell

type Shell interface {
	// Initialize initializes the shell environment
	Initialize() error
	// SetVerbosity sets the verbosity flag
	SetVerbosity(verbose bool)
	// PrintEnvVars prints the provided environment variables
	PrintEnvVars(envVars map[string]string)
	// PrintAlias retrieves the shell alias
	PrintAlias(envVars map[string]string)
	// GetProjectRoot retrieves the project root directory
	GetProjectRoot() (string, error)
	// Exec executes a command with optional privilege elevation
	Exec(command string, args ...string) (string, error)
	// ExecSilent executes a command and returns its output as a string without printing to stdout or stderr
	ExecSilent(command string, args ...string) (string, error)
	// ExecSudo executes a command with sudo if not already present and returns its output as a string while suppressing it from being printed
	ExecSudo(message string, command string, args ...string) (string, error)
	// ExecProgress executes a command and returns its output as a string while displaying progress status
	ExecProgress(message string, command string, args ...string) (string, error)
	// InstallHook installs a shell hook for the specified shell name
	InstallHook(shellName string) error
	// AddCurrentDirToTrustedFile adds the current directory to a trusted list stored in a file.
	AddCurrentDirToTrustedFile() error
	// CheckTrustedDirectory verifies if the current directory is in the trusted file list.
	CheckTrustedDirectory() error
	// UnsetEnvs generates a command to unset multiple environment variables
	UnsetEnvs(envVars []string)
	// UnsetAlias generates commands to unset multiple aliases
	UnsetAlias(aliases []string)
	// WriteResetToken writes a reset token file based on the session token
	WriteResetToken() (string, error)
	// GetSessionToken retrieves or generates a session token
	GetSessionToken() (string, error)
	// CheckResetFlags checks if a reset signal file exists for the current session
	CheckResetFlags() (bool, error)
	// Reset removes all managed environment variables and aliases
	Reset()
}

Shell interface defines methods for shell operations

Jump to

Keyboard shortcuts

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