forked from konstructio/kubefirst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (113 loc) · 3.22 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
/*
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"
"golang.org/x/exp/slices"
zeroLog "github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/kubefirst/kubefirst/cmd"
"github.com/kubefirst/kubefirst/internal/progress"
"github.com/kubefirst/runtime/configs"
"github.com/kubefirst/runtime/pkg"
"github.com/spf13/viper"
)
func main() {
argsWithProg := os.Args
bubbleTeaBlacklist := []string{"completion", "help", "--help", "-h", "quota", "logs"}
canRunBubbleTea := true
if argsWithProg != nil {
for _, arg := range argsWithProg {
isBlackListed := slices.Contains(bubbleTeaBlacklist, arg)
if isBlackListed {
canRunBubbleTea = false
}
}
}
config := configs.ReadConfig()
if err := pkg.SetupViper(config, true); err != nil {
stdLog.Panic(err)
}
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.Info().Msg(err.Error())
}
k1Dir := fmt.Sprintf("%s/.k1", homePath)
//* create k1Dir if it doesn't exist
if _, err := os.Stat(k1Dir); os.IsNotExist(err) {
err := os.MkdirAll(k1Dir, os.ModePerm)
if err != nil {
log.Info().Msgf("%s directory already exists, continuing", k1Dir)
}
}
//* create log directory
logsFolder := fmt.Sprintf("%s/logs", k1Dir)
_ = os.Mkdir(logsFolder, 0700)
if err != nil {
log.Fatal().Msgf("error creating logs directory: %s", err)
}
//* create session log file
logfile := fmt.Sprintf("%s/%s", logsFolder, logfileName)
logFileObj, err := pkg.OpenLogFile(logfile)
if err != nil {
stdLog.Panicf("unable to store log location, error is: %s - please verify the current user has write access to this directory", err)
}
// handle file close request
defer func(logFileObj *os.File) {
err = logFileObj.Close()
if err != nil {
log.Print(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)
err = viper.WriteConfig()
if err != nil {
stdLog.Panicf("unable to set log-file-location, error is: %s", err)
}
if canRunBubbleTea {
progress.InitializeProgressTerminal()
go func() {
cmd.Execute()
}()
progress.Progress.Run()
} else {
cmd.Execute()
}
}