forked from katexochen/ghh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/katexochen/ghh/internal/cmd"
"github.com/spf13/cobra"
)
func main() {
if err := run(); err != nil {
fmt.Printf("Error: %s", err)
os.Exit(1)
}
}
func run() error {
cobra.EnableCommandSorting = false
rootCmd := newRootCmd()
ctx, cancel := signalContext(context.Background(), os.Interrupt)
defer cancel()
return rootCmd.ExecuteContext(ctx)
}
func newRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "ghh",
Short: "GitHub Helper CLI",
}
rootCmd.SetOut(os.Stdout)
rootCmd.AddCommand(
cmd.NewDeleteAllRunsCmd(),
cmd.NewCreateProjectIssueCmd(),
cmd.NewSetAuthCmd(),
cmd.NewListBranchesCmd(),
)
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output")
return rootCmd
}
func signalContext(ctx context.Context, sig os.Signal) (context.Context, context.CancelFunc) {
sigCtx, stop := signal.NotifyContext(ctx, sig)
done := make(chan struct{}, 1)
stopDone := make(chan struct{}, 1)
go func() {
defer func() { stopDone <- struct{}{} }()
defer stop()
select {
case <-sigCtx.Done():
fmt.Println(" Signal caught. Press ctrl+c again to terminate the program immediately.")
case <-done:
}
}()
cancelFunc := func() {
done <- struct{}{}
<-stopDone
}
return sigCtx, cancelFunc
}