Documentation
¶
Index ¶
- Constants
- Variables
- func HookNameAttribute(name string) attribute.KeyValue
- func HooksConfigSignature(hooks map[string][]*HookConfig) string
- func IsKnownHookName(name string) bool
- type Event
- type EventDispatcher
- func (ed *EventDispatcher[T]) AddHandler(ctx context.Context, name Event, handler EventHandlerFn[T]) error
- func (ed *EventDispatcher[T]) Invoke(ctx context.Context, name Event, eventArgs T, action InvokeFn) error
- func (ed *EventDispatcher[T]) RaiseEvent(ctx context.Context, name Event, eventArgs T) error
- type EventHandlerFn
- type HookConfig
- type HookFilterPredicateFn
- type HookPlatformType
- type HookType
- type HookValidationResult
- type HookWarning
- type HooksConfig
- type HooksManager
- func (h *HooksManager) GetAll(hooks map[string][]*HookConfig) ([]*HookConfig, error)
- func (h *HooksManager) GetByParams(hooks map[string][]*HookConfig, prefix HookType, commands ...string) ([]*HookConfig, error)
- func (h *HooksManager) ValidateHooks(ctx context.Context, allHooks map[string][]*HookConfig) *HookValidationResult
- func (h *HooksManager) ValidateRuntimesErr(ctx context.Context, allHooks map[string][]*HookConfig) error
- type HooksManagerOptions
- type HooksRunner
- type InvokeFn
Constants ¶
const ( // Executes pre hooks HookTypePre HookType = "pre" // Execute post hooks HookTypePost HookType = "post" HookTypeNone HookType = "" HookPlatformWindows HookPlatformType = "windows" HookPlatformPosix HookPlatformType = "posix" )
Variables ¶
var ( // ErrScriptTypeUnknown indicates the hook kind could not be inferred // from the script path and was not explicitly configured. ErrScriptTypeUnknown error = errors.New( "unable to determine hook kind. " + "Ensure 'kind' or 'shell' is set in your hook configuration, " + "or use a file with a recognized extension " + "(.sh, .ps1, .py, .js, .ts, .cs)", ) // ErrRunRequired indicates the hook configuration is missing the mandatory 'run' field. ErrRunRequired error = errors.New( "'run' is required for every hook configuration. " + "Set 'run' to an inline script or a relative file path", ) // ErrUnsupportedScriptType indicates the script file has an extension // that is not recognized and no explicit kind or shell was set. ErrUnsupportedScriptType error = errors.New( "script type is not valid. " + "Supported extensions: .sh, .ps1, .py, .js, .ts, .cs. " + "Alternatively, set 'kind' (e.g. kind: python) " + "or 'shell' (e.g. shell: sh)", ) )
var (
ErrInvalidEvent = errors.New("invalid event name for the current type")
)
Functions ¶
func HookNameAttribute ¶ added in v1.25.6
HookNameAttribute returns the telemetry attribute for the `hooks.name` field. Built-in lifecycle hook names are emitted raw; any other (user- or extension-defined) name is hashed via fields.StringHashed so identifiers are not leaked. The allowlist check happens before hashing so the common path does no unnecessary SHA-256 work.
func HooksConfigSignature ¶ added in v1.23.15
func HooksConfigSignature(hooks map[string][]*HookConfig) string
HooksConfigSignature returns a stable signature for a set of hook configurations. The signature ignores runtime-only state and changes whenever the effective hook config changes.
func IsKnownHookName ¶ added in v1.25.6
IsKnownHookName reports whether name is one of the built-in azd lifecycle hook names. Extension- or project-author-defined names return false and are hashed before emission in telemetry.
Types ¶
type EventDispatcher ¶
type EventDispatcher[T any] struct { // contains filtered or unexported fields }
func NewEventDispatcher ¶
func NewEventDispatcher[T any](validEventNames ...Event) *EventDispatcher[T]
func (*EventDispatcher[T]) AddHandler ¶
func (ed *EventDispatcher[T]) AddHandler(ctx context.Context, name Event, handler EventHandlerFn[T]) error
AddHandler adds an event handler for the specified event name. The handler is automatically removed when ctx is cancelled.
func (*EventDispatcher[T]) Invoke ¶
func (ed *EventDispatcher[T]) Invoke(ctx context.Context, name Event, eventArgs T, action InvokeFn) error
Invokes an action and raises an event before and after the action
func (*EventDispatcher[T]) RaiseEvent ¶
func (ed *EventDispatcher[T]) RaiseEvent(ctx context.Context, name Event, eventArgs T) error
Raises the specified event and calls any registered event handlers
type HookConfig ¶
type HookConfig struct {
// Internal name of the hook running for a given command
Name string `yaml:",omitempty"`
// Kind specifies the executor type of the hook script.
// Allowed values: "sh", "pwsh", "js", "ts", "python", "dotnet".
// When empty, the kind is auto-detected from the file extension
// of the run path (e.g. .py → python, .ps1 → pwsh). If Kind
// and Shell are both empty and run references a file,
// the extension is used. For inline scripts, Kind or Shell
// must be set explicitly.
Kind language.HookKind `yaml:"kind,omitempty" json:"kind,omitempty"`
// Shell is a deprecated alias for Kind. When set in YAML
// (shell: sh) it is mapped to Kind during validate().
// Retained for backwards compatibility with legacy configs.
Shell string `yaml:"shell,omitempty" json:"-"`
// Dir specifies the working directory for hook execution,
// used as the project context for dependency installation and builds.
// When empty, defaults to the directory containing the script
// referenced by the run field. Only set this when the project root
// differs from the script's directory.
Dir string `yaml:"dir,omitempty" json:"dir,omitempty"`
// The inline script to execute or path to existing file
Run string `yaml:"run,omitempty"`
// When set to true will not halt command execution even when a script error occurs.
ContinueOnError bool `yaml:"continueOnError,omitempty"`
// When set to true will bind the stdin, stdout & stderr to the running console
Interactive bool `yaml:"interactive,omitempty"`
// When running on windows use this override config
Windows *HookConfig `yaml:"windows,omitempty"`
// When running on linux/macos use this override config
Posix *HookConfig `yaml:"posix,omitempty"`
// Environment variables in this list are added to the hook script and if the value is a akvs:// reference
// it will be resolved to the secret value
Secrets map[string]string `yaml:"secrets,omitempty"`
// Config is an optional property bag of executor-specific settings,
// discriminated by Kind. Each executor may unmarshal this into a
// strongly-typed struct for its own configuration needs.
Config map[string]any `yaml:"config,omitempty"`
// contains filtered or unexported fields
}
HookConfig defines the configuration for a single hook in azure.yaml. Hooks are lifecycle scripts that run before or after azd commands. Every hook is executed through a tools.HookExecutor resolved via IoC based on the hook's Kind — including Bash, PowerShell, Python, and future executor types (JS, TS, DotNet).
func (*HookConfig) IsPowerShellHook ¶
func (hc *HookConfig) IsPowerShellHook() bool
IsPowerShellHook determines if a hook configuration uses PowerShell. It checks the resolved Kind first, then falls back to the raw Shell field and file extension for hooks that have not yet been validated.
func (*HookConfig) IsUsingDefaultShell ¶
func (hc *HookConfig) IsUsingDefaultShell() bool
IsUsingDefaultShell returns true if the hook is using the OS default shell because no shell was explicitly configured
type HookFilterPredicateFn ¶
type HookFilterPredicateFn func(scriptName string, hookConfig *HookConfig) bool
type HookPlatformType ¶
type HookPlatformType string
HookPlatformType identifies the operating system platform for platform-specific hook overrides.
type HookType ¶
type HookType string
HookType represents the execution timing of a hook relative to the associated command. Supported values are 'pre' and 'post'.
func InferHookType ¶
InferHookType extracts the hook timing prefix ("pre" or "post") from a hook name and returns the remaining command name. For example, "preprovision" → (HookTypePre, "provision").
type HookValidationResult ¶
type HookValidationResult struct {
Warnings []HookWarning
}
HookValidationResult contains warnings found during hook validation
type HookWarning ¶
HookWarning represents a validation warning for hooks
type HooksConfig ¶ added in v1.23.14
type HooksConfig map[string][]*HookConfig
HooksConfig is an alias for map of hook names to slices of hook configurations. It supports unmarshalling both legacy single-hook and newer multi-hook formats.
func (HooksConfig) MarshalYAML ¶ added in v1.23.14
func (ch HooksConfig) MarshalYAML() (any, error)
MarshalYAML marshals hook configuration to YAML, supporting both single-hook configuration and multiple-hooks configuration.
func (*HooksConfig) UnmarshalYAML ¶ added in v1.23.14
func (ch *HooksConfig) UnmarshalYAML(unmarshal func(any) error) error
UnmarshalYAML converts hook configuration from YAML, supporting both single-hook configuration and multiple-hooks configuration.
Each hook entry is independently parsed as either a single HookConfig (mapping node) or a list of HookConfigs (sequence node), allowing mixed formats within the same hooks: block.
type HooksManager ¶
type HooksManager struct {
// contains filtered or unexported fields
}
HooksManager enables support to invoke lifecycle hooks before & after commands. Hooks can be invoked at the project or service level.
func NewHooksManager ¶
func NewHooksManager( options HooksManagerOptions, commandRunner exec.CommandRunner, ) *HooksManager
NewHooksManager creates a new HooksManager instance. When HooksManagerOptions.Cwd is empty defaults to current shell working directory. HooksManagerOptions.ProjectDir is the project root directory (where azure.yaml lives), used as the security boundary for path containment. When empty, Cwd is used as the boundary.
func (*HooksManager) GetAll ¶
func (h *HooksManager) GetAll(hooks map[string][]*HookConfig) ([]*HookConfig, error)
Gets an array of all hook configurations Will return an error if any configuration errors are found
func (*HooksManager) GetByParams ¶
func (h *HooksManager) GetByParams( hooks map[string][]*HookConfig, prefix HookType, commands ...string, ) ([]*HookConfig, error)
Gets an array of hook configurations matching the specified hook type and commands Will return an error if any configuration errors are found
func (*HooksManager) ValidateHooks ¶
func (h *HooksManager) ValidateHooks(ctx context.Context, allHooks map[string][]*HookConfig) *HookValidationResult
ValidateHooks validates hook configurations and returns any warnings
func (*HooksManager) ValidateRuntimesErr ¶ added in v1.23.15
func (h *HooksManager) ValidateRuntimesErr( ctx context.Context, allHooks map[string][]*HookConfig, ) error
ValidateRuntimesErr is a convenience wrapper around ValidateHooks that returns an errorhandler.ErrorWithSuggestion when any required runtime is missing. Callers that need a hard early failure (e.g. before starting a long deployment) should use this instead of inspecting warnings manually.
type HooksManagerOptions ¶ added in v1.24.0
type HooksManagerOptions struct {
Cwd string // Working directory for resolving relative hook paths
ProjectDir string // Project root (azure.yaml location) for security boundary
}
HooksManagerOptions configures a new HooksManager.
type HooksRunner ¶
type HooksRunner struct {
// contains filtered or unexported fields
}
HooksRunner enables support to invoke lifecycle hooks before & after commands. Hooks can be invoked at the project or service level.
func NewHooksRunner ¶
func NewHooksRunner( hooksManager *HooksManager, commandRunner exec.CommandRunner, envManager environment.Manager, console input.Console, cwd string, hooks map[string][]*HookConfig, env *environment.Environment, serviceLocator ioc.ServiceLocator, ) *HooksRunner
NewHooksRunner creates a new instance of HooksRunner. When `cwd` is empty defaults to current shell working directory.
func (*HooksRunner) Invoke ¶
func (h *HooksRunner) Invoke( ctx context.Context, commands []string, hookType string, actionFn InvokeFn, ) error
Invoke runs any registered pre and post script hooks around the specified action. The hookType parameter identifies the hook scope for telemetry (project, layer, or service).
func (*HooksRunner) RunHooks ¶
func (h *HooksRunner) RunHooks( ctx context.Context, ht HookType, hookType string, options *tools.ExecutionContext, commands ...string, ) error
RunHooks invokes any registered script hooks for the specified hook type and command. The hookType parameter identifies the hook scope for telemetry (project, layer, or service).