-
Notifications
You must be signed in to change notification settings - Fork 28
/
klog.go
99 lines (87 loc) · 2.32 KB
/
klog.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
package main
import (
_ "embed"
"fmt"
"github.com/jotaen/klog/klog/app"
"github.com/jotaen/klog/klog/app/cli/util"
"github.com/jotaen/klog/klog/app/main"
"os"
"runtime"
)
//go:embed Specification.md
var specification string
//go:embed LICENSE.txt
var license string
var BinaryVersion string // Set via build flag
var BinaryBuildHash string // Set via build flag
func main() {
if len(BinaryBuildHash) > 7 {
BinaryBuildHash = BinaryBuildHash[:7]
}
klogFolder := func() app.File {
f, err := determineKlogConfigFolder()
if err != nil {
fail(util.PrettifyAppError(err, false), app.CONFIG_ERROR.ToInt())
}
return f
}()
configFile := func() string {
c, err := readConfigFile(app.Join(klogFolder, app.CONFIG_FILE_NAME))
if err != nil {
fail(util.PrettifyAppError(err, false), app.CONFIG_ERROR.ToInt())
}
return c
}()
config := func() app.Config {
c, err := app.NewConfig(
app.FromDeterminedValues{NumCpus: runtime.NumCPU()},
app.FromEnvVars{GetVar: os.Getenv},
app.FromConfigFile{FileContents: configFile},
)
if err != nil {
fail(util.PrettifyAppError(err, false), app.CONFIG_ERROR.ToInt())
}
return c
}()
code, err := klog.Run(klogFolder, app.Meta{
Specification: specification,
License: license,
Version: BinaryVersion,
SrcHash: BinaryBuildHash,
}, config, os.Args[1:])
if err != nil {
fail(err, code)
}
}
// fail terminates the process with an error.
func fail(err error, exitCode int) {
fmt.Println(err)
os.Exit(exitCode)
}
// readConfigFile reads the config file from disk, if present.
// If not present, it returns empty string.
func readConfigFile(location app.File) (string, app.Error) {
contents, rErr := app.ReadFile(location)
if rErr != nil {
if rErr.Code() == app.NO_SUCH_FILE {
return "", nil
}
return "", rErr
}
return contents, nil
}
// determineKlogConfigFolder returns the location where the klog config folder
// is (or should be) located.
func determineKlogConfigFolder() (app.File, app.Error) {
for _, kf := range app.KLOG_CONFIG_FOLDER {
basePath := os.Getenv(kf.BasePathEnvVar)
if basePath != "" {
return app.NewFile(basePath, kf.Location)
}
}
return nil, app.NewError(
"Cannot determine klog config folder",
"Please set the `KLOG_CONFIG_HOME` environment variable, and make it point to a valid, empty folder.",
nil,
)
}