-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathmain.go
88 lines (71 loc) · 2.04 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
/*
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/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/kubefirst/kubefirst/cmd"
"github.com/kubefirst/runtime/configs"
"github.com/kubefirst/runtime/pkg"
"github.com/spf13/viper"
)
func main() {
now := time.Now()
epoch := now.Unix()
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/log_%d.log", logsFolder, epoch)
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 | stdLog.Lmicroseconds | stdLog.Llongfile)
// setup Zerolog
log.Logger = pkg.ZerologSetup(logFileObj, zerolog.InfoLevel)
config := configs.ReadConfig()
if err = pkg.SetupViper(config); err != nil {
stdLog.Panic(err)
}
viper.Set("k1-paths.logs-dir", logsFolder)
viper.Set("k1-paths.log-file", fmt.Sprintf("%s/log_%d.log", logsFolder, epoch))
err = viper.WriteConfig()
if err != nil {
stdLog.Panicf("unable to set log-file-location, error is: %s", err)
}
cmd.Execute()
}