-
Notifications
You must be signed in to change notification settings - Fork 280
hooks: require environment and project always #4011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,99 +10,76 @@ import ( | |
| "github.com/azure/azure-dev/cli/azd/pkg/exec" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/ext" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/input" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/lazy" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/project" | ||
| ) | ||
|
|
||
| type HooksMiddleware struct { | ||
| lazyEnvManager *lazy.Lazy[environment.Manager] | ||
| lazyEnv *lazy.Lazy[*environment.Environment] | ||
| lazyProjectConfig *lazy.Lazy[*project.ProjectConfig] | ||
| importManager *project.ImportManager | ||
| commandRunner exec.CommandRunner | ||
| console input.Console | ||
| options *Options | ||
| envManager environment.Manager | ||
| env *environment.Environment | ||
| projectConfig *project.ProjectConfig | ||
| importManager *project.ImportManager | ||
| commandRunner exec.CommandRunner | ||
| console input.Console | ||
| options *Options | ||
| } | ||
|
|
||
| // Creates a new instance of the Hooks middleware | ||
| func NewHooksMiddleware( | ||
| lazyEnvManager *lazy.Lazy[environment.Manager], | ||
| lazyEnv *lazy.Lazy[*environment.Environment], | ||
| lazyProjectConfig *lazy.Lazy[*project.ProjectConfig], | ||
| envManager environment.Manager, | ||
| env *environment.Environment, | ||
| projectConfig *project.ProjectConfig, | ||
| importManager *project.ImportManager, | ||
| commandRunner exec.CommandRunner, | ||
| console input.Console, | ||
| options *Options, | ||
| ) Middleware { | ||
| return &HooksMiddleware{ | ||
| lazyEnvManager: lazyEnvManager, | ||
| lazyEnv: lazyEnv, | ||
| lazyProjectConfig: lazyProjectConfig, | ||
| importManager: importManager, | ||
| commandRunner: commandRunner, | ||
| console: console, | ||
| options: options, | ||
| envManager: envManager, | ||
| env: env, | ||
| projectConfig: projectConfig, | ||
| importManager: importManager, | ||
| commandRunner: commandRunner, | ||
| console: console, | ||
| options: options, | ||
| } | ||
| } | ||
|
|
||
| // Runs the Hooks middleware | ||
| func (m *HooksMiddleware) Run(ctx context.Context, next NextFn) (*actions.ActionResult, error) { | ||
| env, err := m.lazyEnv.GetValue() | ||
| if err != nil { | ||
| log.Println("azd environment is not available, skipping all hook registrations.") | ||
| return next(ctx) | ||
| } | ||
|
|
||
| projectConfig, err := m.lazyProjectConfig.GetValue() | ||
| if err != nil || projectConfig == nil { | ||
| log.Println("azd project is not available, skipping all hook registrations.") | ||
| return next(ctx) | ||
| } | ||
|
|
||
| if err := m.registerServiceHooks(ctx, env, projectConfig); err != nil { | ||
| if err := m.registerServiceHooks(ctx); err != nil { | ||
| return nil, fmt.Errorf("failed registering service hooks, %w", err) | ||
| } | ||
|
|
||
| return m.registerCommandHooks(ctx, env, projectConfig, next) | ||
| return m.registerCommandHooks(ctx, next) | ||
| } | ||
|
|
||
| // Register command level hooks for the executing cobra command & action | ||
| // Invokes the middleware next function | ||
| func (m *HooksMiddleware) registerCommandHooks( | ||
| ctx context.Context, | ||
| env *environment.Environment, | ||
| projectConfig *project.ProjectConfig, | ||
| next NextFn, | ||
| ) (*actions.ActionResult, error) { | ||
| if projectConfig.Hooks == nil || len(projectConfig.Hooks) == 0 { | ||
| func (m *HooksMiddleware) registerCommandHooks(ctx context.Context, next NextFn) (*actions.ActionResult, error) { | ||
| if m.projectConfig.Hooks == nil || len(m.projectConfig.Hooks) == 0 { | ||
| log.Println( | ||
| "azd project is not available or does not contain any command hooks, skipping command hook registrations.", | ||
| "azd project does not contain any command hooks, skipping command hook registrations.", | ||
| ) | ||
| return next(ctx) | ||
| } | ||
|
|
||
| envManager, err := m.lazyEnvManager.GetValue() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed getting environment manager, %w", err) | ||
| } | ||
|
|
||
| hooksManager := ext.NewHooksManager(projectConfig.Path) | ||
| hooksManager := ext.NewHooksManager(m.projectConfig.Path) | ||
| hooksRunner := ext.NewHooksRunner( | ||
| hooksManager, | ||
| m.commandRunner, | ||
| envManager, | ||
| m.envManager, | ||
| m.console, | ||
| projectConfig.Path, | ||
| projectConfig.Hooks, | ||
| env, | ||
| m.projectConfig.Path, | ||
| m.projectConfig.Hooks, | ||
| m.env, | ||
|
Comment on lines
67
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the environment isn't available and we have |
||
| ) | ||
|
|
||
| var actionResult *actions.ActionResult | ||
|
|
||
| commandNames := []string{m.options.CommandPath} | ||
| commandNames = append(commandNames, m.options.Aliases...) | ||
|
|
||
| err = hooksRunner.Invoke(ctx, commandNames, func() error { | ||
| err := hooksRunner.Invoke(ctx, commandNames, func() error { | ||
| result, err := next(ctx) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -121,17 +98,8 @@ func (m *HooksMiddleware) registerCommandHooks( | |
|
|
||
| // Registers event handlers for all services within the project configuration | ||
| // Runs hooks for each matching event handler | ||
| func (m *HooksMiddleware) registerServiceHooks( | ||
| ctx context.Context, | ||
| env *environment.Environment, | ||
| projectConfig *project.ProjectConfig, | ||
| ) error { | ||
| envManager, err := m.lazyEnvManager.GetValue() | ||
| if err != nil { | ||
| return fmt.Errorf("failed getting environment manager, %w", err) | ||
| } | ||
|
|
||
| stableServices, err := m.importManager.ServiceStable(ctx, projectConfig) | ||
| func (m *HooksMiddleware) registerServiceHooks(ctx context.Context) error { | ||
| stableServices, err := m.importManager.ServiceStable(ctx, m.projectConfig) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when there isn't a project? Won't we get a panic for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if err != nil { | ||
| return fmt.Errorf("failed getting services: %w", err) | ||
| } | ||
|
|
@@ -148,11 +116,11 @@ func (m *HooksMiddleware) registerServiceHooks( | |
| serviceHooksRunner := ext.NewHooksRunner( | ||
| serviceHooksManager, | ||
| m.commandRunner, | ||
| envManager, | ||
| m.envManager, | ||
| m.console, | ||
| service.Path(), | ||
| service.Hooks, | ||
| env, | ||
| m.env, | ||
| ) | ||
|
|
||
| for hookName := range service.Hooks { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we would get
nilreference panic here if projectConfig isnil