-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (75 loc) · 2.03 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"fmt"
"github.com/komodorio/komocli/pkg/portforward"
"github.com/spf13/cobra"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
var (
version = "0.0.0"
commit = "none"
date = "unknown"
)
const flagVerbose = "verbose"
var rootCtxCancel context.CancelFunc = func() {}
var RootCmd = &cobra.Command{
Use: filepath.Base(os.Args[0]),
Version: version,
Short: "Komodor CLI",
Long: `Allows interacting with Komodor platform for automation`,
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := os.Setenv("KOMOCLI_VERSION", version) // for anyone willing to access it
if err != nil {
fmt.Println("Failed to remember app version because of error: " + err.Error())
}
if verbose, err := cmd.Flags().GetBool(flagVerbose); err == nil {
verbose = verbose || os.Getenv("DEBUG") != ""
setupLogging(verbose)
} else {
return err
}
ctx, cancel := context.WithCancel(context.Background())
rootCtxCancel = cancel
cmd.SetContext(ctx)
osSignal := make(chan os.Signal, 1)
signal.Notify(osSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
oscall := <-osSignal
log.Warnf("Stopping on signal: %s\n", oscall)
rootCtxCancel()
}()
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
rootCtxCancel()
},
}
func init() {
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "Show verbose debug information and logging")
RootCmd.AddCommand(portforward.NewCommand())
}
func main() {
if err := RootCmd.Execute(); err != nil {
log.Fatalf("Failed running CLI: %s", err)
}
log.Infof("Done.")
}
func setupLogging(verbose bool) {
if verbose {
log.SetLevel(log.DebugLevel)
gin.SetMode(gin.DebugMode)
log.Debugf("Debug logging is enabled")
} else {
log.SetLevel(log.InfoLevel)
gin.SetMode(gin.ReleaseMode)
}
log.Infof("Komodor CLI, version %s (%s @ %s)", version, commit, date)
}