Skip to content

Commit ba29e14

Browse files
committed
move SetupSignalContext to its own file
1 parent 081f195 commit ba29e14

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

internal/cmd/root.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"os"
8-
"os/signal"
98
"regexp"
109
"strings"
11-
"syscall"
1210

1311
"github.com/cli/go-gh/v2/pkg/api"
1412
"github.com/spf13/cobra"
@@ -120,7 +118,7 @@ func Run() error {
120118

121119
// runCombine is the main execution function for the combine command
122120
func runCombine(cmd *cobra.Command, args []string) error {
123-
ctx, cancel := setupSignalContext()
121+
ctx, cancel := SetupSignalContext()
124122
defer cancel()
125123

126124
Logger.Debug("starting gh-combine", "version", version.String())
@@ -151,25 +149,6 @@ func runCombine(cmd *cobra.Command, args []string) error {
151149
return nil
152150
}
153151

154-
// setupSignalContext creates a context that's cancelled on SIGINT or SIGTERM
155-
func setupSignalContext() (context.Context, context.CancelFunc) {
156-
ctx, cancel := context.WithCancel(context.Background())
157-
signalChan := make(chan os.Signal, 1)
158-
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
159-
160-
go func() {
161-
select {
162-
case <-signalChan:
163-
Logger.Debug("Received interrupt signal, cancelling operations...")
164-
cancel()
165-
case <-ctx.Done():
166-
}
167-
signal.Stop(signalChan)
168-
}()
169-
170-
return ctx, cancel
171-
}
172-
173152
// parseRepositories parses repository names from arguments or file
174153
func parseRepositories(args []string) ([]string, error) {
175154
var repos []string

internal/cmd/setup.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
)
9+
10+
// SetupSignalContext creates a context that's cancelled on SIGINT or SIGTERM
11+
func SetupSignalContext() (context.Context, context.CancelFunc) {
12+
ctx, cancel := context.WithCancel(context.Background())
13+
signalChan := make(chan os.Signal, 1)
14+
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
15+
16+
go func() {
17+
select {
18+
case <-signalChan:
19+
Logger.Debug("Received interrupt signal, cancelling operations...")
20+
cancel()
21+
case <-ctx.Done():
22+
}
23+
signal.Stop(signalChan)
24+
}()
25+
26+
return ctx, cancel
27+
}

0 commit comments

Comments
 (0)