storj/cmd/storj-sim/cancelable.go
Egon Elbre 080ba47a06 all: fix dots
Change-Id: I6a419c62700c568254ff67ae5b73efed2fc98aa2
2020-07-16 14:58:28 +00:00

35 lines
597 B
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"os"
"os/signal"
)
// NewCLIContext creates a context that can be canceled with Ctrl-C.
func NewCLIContext(root context.Context) (context.Context, func()) {
// trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(root)
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
stop := func() {
signal.Stop(signals)
cancel()
}
go func() {
select {
case <-signals:
stop()
case <-ctx.Done():
}
}()
return ctx, stop
}