()
| 115 | } |
| 116 | |
| 117 | func createContext() context.Context { |
| 118 | ctx, cancel := context.WithCancel(context.Background()) |
| 119 | sigc := make(chan os.Signal, 1) |
| 120 | signal.Notify(sigc, getStopSignals()...) |
| 121 | go func() { |
| 122 | <-sigc |
| 123 | cancel() |
| 124 | }() |
| 125 | go func() { |
| 126 | var buf string |
| 127 | |
| 128 | n, err := fmt.Scan(&buf) |
| 129 | if err != nil { |
| 130 | if goerror.Is(err, io.EOF) { |
| 131 | return |
| 132 | } |
| 133 | panic(err) |
| 134 | } else if n == 1 && buf == "c" { |
| 135 | cancel() |
| 136 | } else { |
| 137 | println("unknown key press, code: ", buf) |
| 138 | println("press `c` and enter to send cancel signal") |
| 139 | } |
| 140 | }() |
| 141 | println("press `c` and enter to send cancel signal") |
| 142 | return ctx |
| 143 | } |
| 144 | |
| 145 | func getStopSignals() []os.Signal { |
| 146 | if runtime.GOOS == "windows" { |
no test coverage detected