-
Notifications
You must be signed in to change notification settings - Fork 142
/
main.go
147 lines (119 loc) · 3.54 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package main
import (
"fmt"
stdLog "log"
"os"
"time"
"github.com/konstructio/kubefirst-api/pkg/configs"
utils "github.com/konstructio/kubefirst-api/pkg/utils"
"github.com/konstructio/kubefirst/cmd"
"github.com/konstructio/kubefirst/internal/progress"
zeroLog "github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
)
func main() {
argsWithProg := os.Args
bubbleTeaBlacklist := []string{"completion", "help", "--help", "-h", "quota", "logs"}
canRunBubbleTea := true
for _, arg := range argsWithProg {
isBlackListed := slices.Contains(bubbleTeaBlacklist, arg)
if isBlackListed {
canRunBubbleTea = false
}
}
config, err := configs.ReadConfig()
if err != nil {
log.Error().Msgf("failed to read config: %v", err)
return
}
if err := utils.SetupViper(config, true); err != nil {
log.Error().Msgf("failed to setup Viper: %v", err)
return
}
now := time.Now()
epoch := now.Unix()
logfileName := fmt.Sprintf("log_%d.log", epoch)
isProvision := slices.Contains(argsWithProg, "create")
isLogs := slices.Contains(argsWithProg, "logs")
// don't create a new log file for logs, using the previous one
if isLogs {
logfileName = viper.GetString("k1-paths.log-file-name")
}
// use cluster name as filename
if isProvision {
clusterName := fmt.Sprint(epoch)
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
// Check if the argument is "--cluster-name"
if arg == "--cluster-name" && i+1 < len(os.Args) {
// Get the value of the cluster name
clusterName = os.Args[i+1]
break
}
}
logfileName = fmt.Sprintf("log_%s.log", clusterName)
}
homePath, err := os.UserHomeDir()
if err != nil {
log.Error().Msgf("failed to get user home directory: %v", err)
return
}
k1Dir := fmt.Sprintf("%s/.k1", homePath)
// * create k1Dir if it doesn't exist
if _, err := os.Stat(k1Dir); os.IsNotExist(err) {
if err := os.MkdirAll(k1Dir, os.ModePerm); err != nil {
log.Error().Msgf("error creating directory %q: %v", k1Dir, err)
return
}
}
// * create log directory if it doesn't exist
logsFolder := fmt.Sprintf("%s/logs", k1Dir)
if _, err := os.Stat(logsFolder); os.IsNotExist(err) {
if err := os.Mkdir(logsFolder, 0o700); err != nil {
log.Error().Msgf("error creating logs directory: %v", err)
return
}
}
// * create session log file
logfile := fmt.Sprintf("%s/%s", logsFolder, logfileName)
logFileObj, err := utils.OpenLogFile(logfile)
if err != nil {
log.Error().Msgf("unable to store log location, error is: %v - please verify the current user has write access to this directory", err)
return
}
// handle file close request
defer func(logFileObj *os.File) {
if err := logFileObj.Close(); err != nil {
log.Error().Msgf("error closing log file: %v", err)
}
}(logFileObj)
// setup default logging
// this Go standard log is active to keep compatibility with current code base
stdLog.SetOutput(logFileObj)
stdLog.SetPrefix("LOG: ")
stdLog.SetFlags(stdLog.Ldate)
log.Logger = zeroLog.New(logFileObj).With().Timestamp().Logger()
viper.Set("k1-paths.logs-dir", logsFolder)
viper.Set("k1-paths.log-file", logfile)
viper.Set("k1-paths.log-file-name", logfileName)
if err := viper.WriteConfig(); err != nil {
log.Error().Msgf("failed to write config: %v", err)
return
}
if canRunBubbleTea {
progress.InitializeProgressTerminal()
go func() {
cmd.Execute()
}()
progress.Progress.Run()
} else {
cmd.Execute()
}
}