context

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 8 Imported by: 5

Documentation

Index

Constants

View Source
const (
	// Name of the env var pointing to the kubectl binary
	ENV_VAR_KUBECTL_PATH = "KUBESWITCHER_KUBECTL_PATH"

	// Name of the env var pointing to the kubeconfig file
	ENV_VAR_KUBECONFIG_PATH = "KUBESWITCHER_KUBECONFIG_PATH"

	// Name of the env var containing the name of the currently executed plugin
	ENV_VAR_CURRENT_PLUGIN_NAME = "KUBESWITCHER_CURRENT_PLUGIN_NAME"

	// Name of the env var containing the path to the generic state file
	ENV_VAR_GENERIC_STATE_PATH = "KUBESWITCHER_GENERIC_STATE_PATH"

	// Name of the env var containing the path to the plugin state file
	ENV_VAR_PLUGIN_STATE_PATH = "KUBESWITCHER_PLUGIN_STATE_PATH"

	// Name of the env var containing the path to the notification message file
	ENV_VAR_NOTIFICATION_MESSAGE_PATH = "KUBESWITCHER_NOTIFICATION_MESSAGE_PATH"

	// Name of the env var containing the path to the file with the id string.
	ENV_VAR_ID_PATH = "KUBESWITCHER_ID_PATH"

	// Name of the env var containing the path to the internal call file.
	ENV_VAR_INTERNAL_CALL_PATH = "KUBESWITCHER_INTERNAL_CALL_PATH"

	// Name of the env var containing the path to the internal callback file.
	ENV_VAR_INTERNAL_CALLBACK_PATH = "KUBESWITCHER_INTERNAL_CALLBACK_PATH"

	// Name of the env var containing the statically defined plugin configuration.
	ENV_VAR_PLUGIN_CONFIG = "KUBESWITCHER_PLUGIN_CONFIG"

	// Name of the env var containing the current session id.
	ENV_VAR_SESSION_ID = "KUBESWITCHER_SESSION_ID"

	// Name of the env var containing the current session directory.
	ENV_VAR_SESSION_CONFIG_DIR = "KUBESWITCHER_SESSION_CONFIG_DIR"

	// Name of the env var containing the path to the kubeswitcher config directory.
	ENV_VAR_CONFIG_DIR = "KUBESWITCHER_CONFIG_DIR"

	// Name of the env var containing the debug flag
	ENV_VAR_DEBUG = "KUBESWITCHER_FLAG_DEBUG"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// path to the kubectl binary
	// defaults to "kubectl" if not set
	KubectlBinary string `json:"kubectlBinary"`
	// path to the kubeconfig
	KubeconfigPath string `json:"kubeconfigPath"`
	// name of the currently executed plugin
	CurrentPluginName string `json:"currentPluginName"`
	// path to the generic state file
	GenericStatePath string `json:"genericStatePath"`
	// path to the plugin state file
	PluginStatePath string `json:"pluginStatePath"`
	// path to the notification message file
	NotificationMessagePath string `json:"notificationMessagePath"`
	// path to the id file
	IdPath string `json:"idPath"`
	// path to the internal call file
	InternalCallPath string `json:"internalCallPath"`
	// path to the internal callback file
	InternalCallbackPath string `json:"internalCallbackPath"`
	// statically defined plugin configuration (or empty)
	PluginConfig string `json:"pluginConfig"`
	// current session id
	SessionID string `json:"sessionID"`
	// current session directory
	SessionConfigDir string `json:"sessionConfigDir"`
	// path to the kubeswitcher config directory
	ConfigDir string `json:"configDir"`
}

func GetContext

func GetContext() *Context

GetContext returns the current context. Returns nil if no context has been created yet.

func NewContext

func NewContext(kubectlBinary, kubeconfigPath, currentPluginName, genericStatePath, pluginStatePath, notificationMessagePath, idPath, internalCallPath, internalCallbackPath, pluginConfig, sessionID, sessionConfigDir, configDir string) *Context

NewContext creates a new context object from the given parameters. Plugins will more likely want to call NewContextFromEnv() instead. Note that this overwrites the current context and should only be called if GetContext() returns nil.

func NewContextFromEnv

func NewContextFromEnv() (*Context, error)

NewContextFromEnv creates a new context object from the environment variables. Returns an error if any of the required environment variables are not set. Note that this overwrites the current context and should only be called if GetContext() returns nil. Also sets debug.PrintDebugStatements to true if the environment variable DEBUG is set to "true".

func (*Context) EnvFromContext

func (con *Context) EnvFromContext(pluginName string, pluginConfig []byte, internalCallbackPath string) map[string]string

EnvFromContext generates a map of environment variables from the context. Plugin name and config are injected, since they are usually not set when this is called.

func (*Context) SetPluginName

func (con *Context) SetPluginName(name string)

SetPluginName sets the name of the currently executed plugin, if it is empty. No-op if the name is already set.

func (*Context) String

func (con *Context) String() string

func (*Context) WriteId

func (con *Context) WriteId(id string, args ...any) error

WriteId formats the given string and writes it to the id file. All newlines are removed from the string.

func (*Context) WriteInternalCall

func (con *Context) WriteInternalCall(call string, callback []byte) error

WriteInternalCall requests an internal call by writing the command to the internal call file. Note that the name of the kubeswitcher binary is omitted, so to call 'kw foo --bar', the call parameter should be 'foo --bar'. If callback is not nil, the calling command will receive a callback after the internal call has been executed. During the callback, the information written here can be read and used to react to the internal call.

func (*Context) WriteKubeconfig

func (con *Context) WriteKubeconfig(kcfg []byte, message string, args ...any) error

Writes the kubeconfig and a notification message to the respective files. This assumes that the kubeconfig has actually changed. The additional args are passed into fmt.Sprintf with the message for formatting.

func (con *Context) WriteKubeconfigSymlink(kcfgPath, message string, args ...any) error

func (*Context) WriteNotificationMessage

func (con *Context) WriteNotificationMessage(message string, args ...any) error

WriteNotificationMessage writes the given message to the notification message file. The additional args are passed into fmt.Sprintf with the message for formatting. Note that this function is usually called by WriteKubeconfig (or WriteKubeconfigSymlink) and does not need to be called manually.

func (*Context) WritePluginState

func (con *Context) WritePluginState(ps any) error

WritePluginState writes the given state to the plugin state file. If the parameter has the type []byte, it is written as is. Otherwise, it is marshaled into JSON before being written. If this function has not been called on the context when Close() is called, the plugin state will be deleted to ensure that no leftover state from a previous command is kept.

Jump to

Keyboard shortcuts

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