From 40a355be9c8f444a664e3ca4ddbee294dc4f8734 Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Fri, 30 Apr 2021 20:14:58 -0300 Subject: [PATCH] Handle interrupts [CLI-151] (#273) * Handle interrupts * Fix typo * Use context --- internal/cli/root.go | 18 +++++++++++++++++- internal/cli/template.go | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/cli/root.go b/internal/cli/root.go index edd5d073c..c8ba573ee 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "os/signal" "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/buildinfo" @@ -119,7 +120,7 @@ func Execute() { // for most of the architectures there's no requirements: ansi.InitConsole() - if err := rootCmd.ExecuteContext(context.TODO()); err != nil { + if err := rootCmd.ExecuteContext(contextWithSignal(os.Interrupt)); err != nil { cli.renderer.Heading("error") cli.renderer.Errorf(err.Error()) @@ -127,3 +128,18 @@ func Execute() { os.Exit(1) } } + +func contextWithSignal(sigs ...os.Signal) context.Context { + ctx, cancel := context.WithCancel(context.Background()) + + ch := make(chan os.Signal, 1) + signal.Notify(ch, sigs...) + + go func() { + <-ch + defer cancel() + os.Exit(0) + }() + + return ctx +} diff --git a/internal/cli/template.go b/internal/cli/template.go index d7175637b..9b8eeabb3 100644 --- a/internal/cli/template.go +++ b/internal/cli/template.go @@ -183,7 +183,7 @@ func (cli *cli) obtainCustomTemplateData(ctx context.Context) (*branding.Templat g.Go(func() error { var err error - clients, err = cli.api.Client.List(management.ExcludeFields(clientExcludedList...)) + clients, err = cli.api.Client.List(management.Context(ctx), management.ExcludeFields(clientExcludedList...)) return err })