-
Notifications
You must be signed in to change notification settings - Fork 26
/
root.go
75 lines (64 loc) · 1.67 KB
/
root.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
package cmd
import (
"fmt"
"os"
"github.com/mattn/go-colorable"
"github.com/one2nc/cloudlens/internal/config"
"github.com/one2nc/cloudlens/internal/view"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var (
profile, region, gcpCredFilePath, localStackPort string
useLocalStack bool
version = "v0.1.4"
commit = "dev"
date = "today"
rootCmd = &cobra.Command{
Use: `cloudlens`,
Short: `cli for cloud services`,
Long: `cli for cloud services[aws,gcp]`,
Run: run,
}
cloudConfig config.CloudConfig
out = colorable.NewColorableStdout()
)
func init() {
rootCmd.AddCommand(versionCmd(), updateCmd(), awsCommand(), gcpCommand())
}
func Execute() {
file := ensureLogging()
defer func() {
if file != nil {
_ = file.Close()
}
}()
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) {
initView()
}
func initView() {
app := view.NewApp()
app.Init(version, cloudConfig)
if err := app.Run(); err != nil {
panic(fmt.Sprintf("app run failed %v", err))
}
}
func ensureLogging() *os.File {
filename := "cloudlens.log"
mod := os.O_CREATE | os.O_APPEND | os.O_WRONLY
file, err := os.OpenFile(filename, mod, 0644)
if err != nil {
log.Printf("Could not open cloudlens.log. Writing logs to stdout instead.")
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}
if err == nil {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: file})
}
return file
}