Skip to content

Commit

Permalink
Updates context.TODO references
Browse files Browse the repository at this point in the history
  • Loading branch information
wbreza committed Jul 22, 2024
1 parent 3458953 commit e58e333
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 69 deletions.
12 changes: 6 additions & 6 deletions cli/azd/cmd/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func NewCobraBuilder(container *container.Container) *CobraBuilder {
}

// Builds a cobra Command for the specified action descriptor
func (cb *CobraBuilder) BuildCommand(descriptor *actions.ActionDescriptor) (*cobra.Command, error) {
func (cb *CobraBuilder) BuildCommand(ctx context.Context, descriptor *actions.ActionDescriptor) (*cobra.Command, error) {
cmd := descriptor.Options.Command
if cmd.Use == "" {
cmd.Use = descriptor.Name
}

// Build the full command tree
for _, childDescriptor := range descriptor.Children() {
childCmd, err := cb.BuildCommand(childDescriptor)
childCmd, err := cb.BuildCommand(ctx, childDescriptor)
if err != nil {
return nil, err
}
Expand All @@ -52,7 +52,7 @@ func (cb *CobraBuilder) BuildCommand(descriptor *actions.ActionDescriptor) (*cob
// Bind root command after command tree has been established
// This ensures the command path is ready and consistent across all nested commands
if descriptor.Parent() == nil {
if err := cb.bindCommand(cmd, descriptor); err != nil {
if err := cb.bindCommand(ctx, cmd, descriptor); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func (df *docsFlag) Set(value string) error {
}

// Binds the intersection of cobra command options and action descriptor options
func (cb *CobraBuilder) bindCommand(cmd *cobra.Command, descriptor *actions.ActionDescriptor) error {
func (cb *CobraBuilder) bindCommand(ctx context.Context, cmd *cobra.Command, descriptor *actions.ActionDescriptor) error {
actionName := createActionName(cmd)

// Automatically adds a consistent help flag
Expand Down Expand Up @@ -237,7 +237,7 @@ func (cb *CobraBuilder) bindCommand(cmd *cobra.Command, descriptor *actions.Acti

// The flags resolver is constructed and bound to the cobra command via dependency injection
// This allows flags to be options and support any set of required dependencies
if err := cb.container.InvokeAndRegister(context.TODO(), container.RegisterOptions{
if err := cb.container.InvokeAndRegister(ctx, container.RegisterOptions{
Resolver: descriptor.Options.FlagsResolver,
Lifetime: container.Singleton,
}); err != nil {
Expand Down Expand Up @@ -277,7 +277,7 @@ func (cb *CobraBuilder) bindCommand(cmd *cobra.Command, descriptor *actions.Acti
// Bind the child commands for the current descriptor
for _, childDescriptor := range descriptor.Children() {
childCmd := childDescriptor.Options.Command
if err := cb.bindCommand(childCmd, childDescriptor); err != nil {
if err := cb.bindCommand(ctx, childCmd, childDescriptor); err != nil {
return err
}
}
Expand Down
29 changes: 16 additions & 13 deletions cli/azd/cmd/cobra_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ func Test_BuildAndRunSimpleCommand(t *testing.T) {
},
})

ctx := context.Background()
builder := NewCobraBuilder(container)
cmd, err := builder.BuildCommand(root)
cmd, err := builder.BuildCommand(ctx, root)

require.NotNil(t, cmd)
require.NoError(t, err)

// Disable args processing from os:args
cmd.SetArgs([]string{})
err = cmd.ExecuteContext(context.Background())
err = cmd.ExecuteContext(ctx)

require.NoError(t, err)
require.True(t, ran)
Expand All @@ -58,14 +59,15 @@ func Test_BuildAndRunSimpleAction(t *testing.T) {
FlagsResolver: newTestFlags,
})

ctx := context.Background()
builder := NewCobraBuilder(rootContainer)
cmd, err := builder.BuildCommand(root)
cmd, err := builder.BuildCommand(ctx, root)

require.NotNil(t, cmd)
require.NoError(t, err)

cmd.SetArgs([]string{"-r"})
err = cmd.ExecuteContext(context.Background())
err = cmd.ExecuteContext(ctx)

require.NoError(t, err)
}
Expand All @@ -79,16 +81,16 @@ func Test_BuildAndRunSimpleActionWithMiddleware(t *testing.T) {
FlagsResolver: newTestFlags,
}).UseMiddleware("A", newTestMiddlewareA)

ctx := context.Background()
builder := NewCobraBuilder(rootContainer)
cmd, err := builder.BuildCommand(root)
cmd, err := builder.BuildCommand(ctx, root)

require.NotNil(t, cmd)
require.NoError(t, err)

actionRan := false
middlewareRan := false

ctx := context.Background()
ctx = context.WithValue(ctx, actionName, &actionRan)
ctx = context.WithValue(ctx, middlewareAName, &middlewareRan)

Expand All @@ -112,8 +114,9 @@ func Test_BuildAndRunActionWithNestedMiddleware(t *testing.T) {
FlagsResolver: newTestFlags,
}).UseMiddleware("B", newTestMiddlewareB)

ctx := context.Background()
builder := NewCobraBuilder(rootContainer)
cmd, err := builder.BuildCommand(root)
cmd, err := builder.BuildCommand(ctx, root)

require.NotNil(t, cmd)
require.NoError(t, err)
Expand All @@ -122,7 +125,6 @@ func Test_BuildAndRunActionWithNestedMiddleware(t *testing.T) {
middlewareARan := false
middlewareBRan := false

ctx := context.Background()
ctx = context.WithValue(ctx, actionName, &actionRan)
ctx = context.WithValue(ctx, middlewareAName, &middlewareARan)
ctx = context.WithValue(ctx, middlewareBName, &middlewareBRan)
Expand Down Expand Up @@ -154,8 +156,9 @@ func Test_BuildAndRunActionWithNestedAndConditionalMiddleware(t *testing.T) {
return false
})

ctx := context.Background()
builder := NewCobraBuilder(rootContainer)
cmd, err := builder.BuildCommand(root)
cmd, err := builder.BuildCommand(ctx, root)

require.NotNil(t, cmd)
require.NoError(t, err)
Expand All @@ -164,7 +167,6 @@ func Test_BuildAndRunActionWithNestedAndConditionalMiddleware(t *testing.T) {
middlewareARan := false
middlewareBRan := false

ctx := context.Background()
ctx = context.WithValue(ctx, actionName, &actionRan)
ctx = context.WithValue(ctx, middlewareAName, &middlewareARan)
ctx = context.WithValue(ctx, middlewareBName, &middlewareBRan)
Expand All @@ -191,8 +193,9 @@ func Test_BuildCommandsWithAutomaticHelpAndOutputFlags(t *testing.T) {
},
})

ctx := context.Background()
cobraBuilder := NewCobraBuilder(rootContainer)
cmd, err := cobraBuilder.BuildCommand(root)
cmd, err := cobraBuilder.BuildCommand(ctx, root)

require.NoError(t, err)
require.NotNil(t, cmd)
Expand Down Expand Up @@ -238,7 +241,7 @@ func Test_RunDocsFlow(t *testing.T) {
}

cobraBuilder := NewCobraBuilder(rootContainer)
cmd, err := cobraBuilder.BuildCommand(root)
cmd, err := cobraBuilder.BuildCommand(*testCtx.Context, root)

require.NoError(t, err)
require.NotNil(t, cmd)
Expand Down Expand Up @@ -270,7 +273,7 @@ func Test_RunDocsAndHelpFlow(t *testing.T) {
}

cobraBuilder := NewCobraBuilder(rootContainer)
cmd, err := cobraBuilder.BuildCommand(root)
cmd, err := cobraBuilder.BuildCommand(*testCtx.Context, root)

require.NoError(t, err)
require.NotNil(t, cmd)
Expand Down
63 changes: 38 additions & 25 deletions cli/azd/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ func registerCommonDependencies(rootContainer *container.Container) {

container.MustRegisterSingleton(
rootContainer,
func(serviceLocator ioc.ServiceLocator) *lazy.Lazy[environment.LocalDataStore] {
func(ctx context.Context, serviceLocator ioc.ServiceLocator) *lazy.Lazy[environment.LocalDataStore] {
return lazy.NewLazy(func() (environment.LocalDataStore, error) {
var localDataStore environment.LocalDataStore
err := serviceLocator.Resolve(context.TODO(), &localDataStore)
err := serviceLocator.Resolve(ctx, &localDataStore)
if err != nil {
return nil, err
}
Expand All @@ -271,7 +271,11 @@ func registerCommonDependencies(rootContainer *container.Container) {
// Environment manager depends on azd context
container.MustRegisterSingleton(
rootContainer,
func(serviceLocator ioc.ServiceLocator, azdContext *lazy.Lazy[*azdcontext.AzdContext]) *lazy.Lazy[environment.Manager] {
func(
ctx context.Context,
serviceLocator ioc.ServiceLocator,
azdContext *lazy.Lazy[*azdcontext.AzdContext],
) *lazy.Lazy[environment.Manager] {
return lazy.NewLazy(func() (environment.Manager, error) {
azdCtx, err := azdContext.GetValue()
if err != nil {
Expand All @@ -284,7 +288,7 @@ func registerCommonDependencies(rootContainer *container.Container) {
}

var envManager environment.Manager
err = serviceLocator.Resolve(context.TODO(), &envManager)
err = serviceLocator.Resolve(ctx, &envManager)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -507,14 +511,17 @@ func registerCommonDependencies(rootContainer *container.Container) {
container.MustRegisterSingleton(rootContainer, templates.NewTemplateManager)
container.MustRegisterSingleton(rootContainer, templates.NewSourceManager)
container.MustRegisterScoped(rootContainer, project.NewResourceManager)
container.MustRegisterScoped(rootContainer, func(serviceLocator ioc.ServiceLocator) *lazy.Lazy[project.ResourceManager] {
return lazy.NewLazy(func() (project.ResourceManager, error) {
var resourceManager project.ResourceManager
err := serviceLocator.Resolve(context.TODO(), &resourceManager)
container.MustRegisterScoped(
rootContainer,
func(ctx context.Context, serviceLocator ioc.ServiceLocator) *lazy.Lazy[project.ResourceManager] {
return lazy.NewLazy(func() (project.ResourceManager, error) {
var resourceManager project.ResourceManager
err := serviceLocator.Resolve(ctx, &resourceManager)

return resourceManager, err
})
})
return resourceManager, err
})
},
)
container.MustRegisterScoped(rootContainer, project.NewProjectManager)
// Currently caches manifest across command executions
container.MustRegisterSingleton(rootContainer, project.NewDotNetImporter)
Expand All @@ -527,14 +534,17 @@ func registerCommonDependencies(rootContainer *container.Container) {
return project.ServiceOperationCache{}
})

container.MustRegisterScoped(rootContainer, func(serviceLocator ioc.ServiceLocator) *lazy.Lazy[project.ServiceManager] {
return lazy.NewLazy(func() (project.ServiceManager, error) {
var serviceManager project.ServiceManager
err := serviceLocator.Resolve(context.TODO(), &serviceManager)
container.MustRegisterScoped(
rootContainer,
func(ctx context.Context, serviceLocator ioc.ServiceLocator) *lazy.Lazy[project.ServiceManager] {
return lazy.NewLazy(func() (project.ServiceManager, error) {
var serviceManager project.ServiceManager
err := serviceLocator.Resolve(ctx, &serviceManager)

return serviceManager, err
})
})
return serviceManager, err
})
},
)
container.MustRegisterSingleton(rootContainer, repository.NewInitializer)
container.MustRegisterSingleton(rootContainer, alpha.NewFeaturesManager)
container.MustRegisterSingleton(rootContainer, config.NewUserConfigManager)
Expand Down Expand Up @@ -771,14 +781,17 @@ func registerCommonDependencies(rootContainer *container.Container) {
container.MustRegisterNamedSingleton(rootContainer, platformName, constructor)
}

container.MustRegisterSingleton(rootContainer, func(s ioc.ServiceLocator) (workflow.AzdCommandRunner, error) {
var rootCmd *cobra.Command
if err := s.ResolveNamed(context.TODO(), "root-cmd", &rootCmd); err != nil {
return nil, err
}
return &workflowCmdAdapter{cmd: rootCmd}, nil
container.MustRegisterSingleton(
rootContainer,
func(ctx context.Context, s ioc.ServiceLocator) (workflow.AzdCommandRunner, error) {
var rootCmd *cobra.Command
if err := s.ResolveNamed(ctx, "root-cmd", &rootCmd); err != nil {
return nil, err
}
return &workflowCmdAdapter{cmd: rootCmd}, nil

})
},
)
container.MustRegisterSingleton(rootContainer, workflow.NewRunner)

// Required for nested actions called from composite actions like 'up'
Expand Down
7 changes: 4 additions & 3 deletions cli/azd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// rootContainer - The IoC container to use for registering and resolving dependencies. If nil is provided, a new
// container empty will be created.
func NewRootCmd(
ctx context.Context,
staticHelp bool,
middlewareChain []*actions.MiddlewareRegistration,
rootContainer *container.Container,
Expand Down Expand Up @@ -348,17 +349,17 @@ func NewRootCmd(
// Initialize the platform specific components for the IoC container
// Only container resolution errors will return an error
// Invalid configurations will fall back to default platform
if _, err := platform.Initialize(rootContainer, azd.PlatformKindDefault); err != nil {
if _, err := platform.Initialize(ctx, rootContainer, azd.PlatformKindDefault); err != nil {
panic(err)
}

// Compose the hierarchy of action descriptions into cobra commands
var cobraBuilder *CobraBuilder
if err := rootContainer.Resolve(context.TODO(), &cobraBuilder); err != nil {
if err := rootContainer.Resolve(ctx, &cobraBuilder); err != nil {
panic(err)
}

cmd, err := cobraBuilder.BuildCommand(root)
cmd, err := cobraBuilder.BuildCommand(ctx, root)

if err != nil {
// If their is a container registration issue or similar we'll get an error at this point
Expand Down
3 changes: 2 additions & 1 deletion cli/azd/cmd/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"context"
"html/template"
"strings"
"testing"
Expand All @@ -21,7 +22,7 @@ import (
func TestUsage(t *testing.T) {
// disable rich formatting output
t.Setenv("TERM", "dumb")
root := NewRootCmd(false, nil, nil)
root := NewRootCmd(context.Background(), false, nil, nil)

usageSnapshot(t, root)
}
Expand Down
3 changes: 2 additions & 1 deletion cli/azd/docs/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bytes"
"context"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -53,7 +54,7 @@ func main() {

// staticHelp is true to inform commands to use generate help text instead
// of generating help text that includes execution-specific state.
cmd := azd.NewRootCmd(true, nil, nil)
cmd := azd.NewRootCmd(context.Background(), true, nil, nil)

basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
filename := filepath.Join("./md", basename)
Expand Down
4 changes: 3 additions & 1 deletion cli/azd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func main() {
go fetchLatestVersion(latest)

rootContainer := container.New()
cmdErr := cmd.NewRootCmd(false, nil, rootContainer).ExecuteContext(ctx)
cmdErr := cmd.
NewRootCmd(ctx, false, nil, rootContainer).
ExecuteContext(ctx)

oneauth.Shutdown()

Expand Down
3 changes: 2 additions & 1 deletion cli/azd/pkg/environment/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type manager struct {

// NewManager creates a new Manager instance
func NewManager(
ctx context.Context,
serviceLocator ioc.ServiceLocator,
azdContext *azdcontext.AzdContext,
console input.Console,
Expand All @@ -98,7 +99,7 @@ func NewManager(
// via the container but we can't do that because the remote data store is optional and the IoC
// container doesn't support optional interface based dependencies.
if remoteConfig != nil {
err := serviceLocator.ResolveNamed(context.TODO(), remoteConfig.Backend, &remote)
err := serviceLocator.ResolveNamed(ctx, remoteConfig.Backend, &remote)
if err != nil {
if errors.Is(err, container.ErrResolutionFailed) {
return nil, fmt.Errorf(
Expand Down
Loading

0 comments on commit e58e333

Please sign in to comment.