Skip to content

Commit

Permalink
cmd/listen: Exit when stdin is closed (#1155)
Browse files Browse the repository at this point in the history
Co-authored-by: Ian Jabour <[email protected]>
  • Loading branch information
wojtekmach and ianjabour-stripe authored Mar 26, 2024
1 parent dcb9645 commit 531ccfb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/cmd/listen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bufio"
"context"
"fmt"
"net/url"
Expand Down Expand Up @@ -130,6 +131,8 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error {
}).Debug("Ctrl+C received, cleaning up...")
})

ctx = withStdinCloseCancel(ctx)

client := &stripe.Client{
APIKey: key,
BaseURL: apiBase,
Expand Down Expand Up @@ -198,6 +201,20 @@ func withSIGTERMCancel(ctx context.Context, onCancel func()) context.Context {
return ctx
}

func withStdinCloseCancel(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)

go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
}
if scanner.Err() == nil {
cancel()
}
}()
return ctx
}

func createVisitor(logger *log.Logger, format string, printJSON bool) *websocket.Visitor {
var s *spinner.Spinner

Expand Down

0 comments on commit 531ccfb

Please sign in to comment.