Skip to content

Commit

Permalink
cmd: pass context through args
Browse files Browse the repository at this point in the history
as suggested in [#360], we should be closer to idiomatic go where rather
than passing context via structs, we make use of 1st-arg for ctx.

see https://pkg.go.dev/context for more details

[#360]: #360 (comment)

Signed-off-by: Ciro S. Costa <ciroscosta@vmware.com>
  • Loading branch information
Ciro S. Costa committed Nov 19, 2021
1 parent c23028e commit 0f2b0b0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
4 changes: 1 addition & 3 deletions cmd/cartographer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func init() {

func main() {
ctx, cancel := context.WithCancel(context.Background())

defer cancel()

loggerOpt, err := logger.SetLogLevel(verbosity)
Expand All @@ -51,11 +50,10 @@ func main() {
cmd := root.Command{
Port: port,
CertDir: certDir,
Context: ctx,
Logger: zap.New(zap.UseDevMode(devMode), loggerOpt),
}

if err = cmd.Execute(); err != nil {
if err = cmd.Execute(ctx); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion pkg/registrar/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func registerRunnableController(mgr manager.Manager) error {
return nil
}

func IndexResources(mgr manager.Manager, ctx context.Context) error {
func IndexResources(ctx context.Context, mgr manager.Manager) error {
fieldIndexer := mgr.GetFieldIndexer()

if err := indexSupplyChains(ctx, fieldIndexer); err != nil {
Expand Down
7 changes: 3 additions & 4 deletions pkg/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ import (
type Command struct {
Port int
CertDir string
Context context.Context
Logger logr.Logger
}

func (cmd *Command) Execute() error {
func (cmd *Command) Execute(ctx context.Context) error {
log.SetLogger(cmd.Logger)
l := log.Log.WithName("cartographer")

Expand Down Expand Up @@ -65,7 +64,7 @@ func (cmd *Command) Execute() error {
return fmt.Errorf("register controllers: %w", err)
}

if err := registrar.IndexResources(mgr, cmd.Context); err != nil {
if err := registrar.IndexResources(ctx, mgr); err != nil {
return fmt.Errorf("index resources: %w", err)
}

Expand Down Expand Up @@ -109,7 +108,7 @@ func (cmd *Command) Execute() error {
}
}

if err := mgr.Start(cmd.Context); err != nil {
if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("manager start: %w", err)
}

Expand Down

0 comments on commit 0f2b0b0

Please sign in to comment.