Skip to content

Commit

Permalink
Handle interrupts [CLI-151] (#273)
Browse files Browse the repository at this point in the history
* Handle interrupts

* Fix typo

* Use context
  • Loading branch information
Widcket committed Apr 30, 2021
1 parent 1bea3af commit 40a355b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"os/signal"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/buildinfo"
Expand Down Expand Up @@ -119,11 +120,26 @@ 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())

instrumentation.ReportException(err)
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
}
2 changes: 1 addition & 1 deletion internal/cli/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down

0 comments on commit 40a355b

Please sign in to comment.