-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (68 loc) · 1.88 KB
/
main.go
File metadata and controls
86 lines (68 loc) · 1.88 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/lets-cli/lets/cmd"
"github.com/lets-cli/lets/config"
"github.com/lets-cli/lets/env"
"github.com/lets-cli/lets/logging"
"github.com/lets-cli/lets/plugins"
"github.com/lets-cli/lets/runner"
"github.com/lets-cli/lets/workdir"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var version = "0.0.0-dev"
func main() {
ctx := getContext()
logging.InitLogging(env.IsDebug(), os.Stdout, os.Stderr)
configFile := os.Getenv("LETS_CONFIG")
configDir := os.Getenv("LETS_CONFIG_DIR")
cfg, readConfigErr := config.Load(configFile, configDir, version)
var rootCmd *cobra.Command
if cfg != nil {
rootCmd = cmd.CreateRootCommandWithConfig(os.Stdout, cfg, version)
if err := workdir.CreateDotLetsDir(cfg.WorkDir); err != nil {
log.Error(err)
os.Exit(1)
}
} else {
rootCmd = cmd.CreateRootCommand(version)
}
if readConfigErr != nil {
cmd.ConfigErrorCheck(rootCmd, readConfigErr)
}
if len(cfg.Plugins) > 0 {
if err := plugins.Load(cfg); err != nil {
log.Error(err)
os.Exit(1)
}
}
if err := rootCmd.ExecuteContext(ctx); err != nil {
log.Error(err.Error())
exitCode := 1
if e, ok := err.(*runner.RunErr); ok { //nolint:errorlint
exitCode = e.ExitCode()
}
os.Exit(exitCode)
}
}
// getContext returns context and kicks of a goroutine
// which waits for SIGINT, SIGTERM and cancels global context.
//
// Note that since we setting stdin to command we run, that command
// will receive SIGINT, SIGTERM at the same time as we here,
// so command's process can begin finishing earlier than cancel will say it to.
func getContext() context.Context {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
sig := <-ch
log.Printf("lets: signal received: %s", sig)
cancel()
}()
return ctx
}