-
Notifications
You must be signed in to change notification settings - Fork 1
/
opp.go
62 lines (55 loc) · 1.33 KB
/
opp.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
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"github.com/cupcicm/opp/cmd"
"github.com/cupcicm/opp/core"
"github.com/cupcicm/opp/core/story"
"github.com/spf13/viper"
)
func CommandContext() (context.Context, context.CancelCauseFunc) {
return context.WithCancelCause(context.Background())
}
func gh(ctx context.Context) core.Gh {
return core.NewClient(ctx)
}
func sf(tool, token string) story.StoryFetcher {
return story.NewStoryFetcher(tool, token)
}
func main() {
repo := core.Current()
viper.AddConfigPath(repo.DotOpDir())
viper.AddConfigPath("$HOME/.config/opp")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.ReadInConfig()
root := cmd.MakeApp(os.Stdout, os.Stdin, repo, gh, sf)
ctx, cancel := CommandContext()
if !repo.OppEnabled() && (len(os.Args) != 2 || os.Args[1] != "init") {
fmt.Println("Please run opp init first")
os.Exit(1)
}
signalChan := make(chan os.Signal, 1)
// Get a signal when the User Ctrl-C.
signal.Notify(signalChan, os.Interrupt)
defer func() {
signal.Stop(signalChan)
cancel(nil)
}()
go func() {
select {
case <-signalChan: // first signal, cancel context
cancel(errors.New("interrupted"))
case <-ctx.Done():
}
<-signalChan // second signal, hard exit
os.Exit(2)
}()
if err := root.Run(ctx, os.Args); err != nil {
log.Fatal(err)
}
}