-
Notifications
You must be signed in to change notification settings - Fork 0
/
appcontext.go
58 lines (51 loc) · 1.44 KB
/
appcontext.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
// SPDX-FileCopyrightText: 2023 RH America LLC <info@rhamerica.com>
// SPDX-License-Identifier: GPL-3.0-only
package model
import (
"fmt"
"os"
"path/filepath"
"github.com/bazurto/bz/lib/utils"
)
type AppContext struct {
AppName string
LockFileName string
HomeDir string
UserDir string
UserConfigFileName string
UserCacheDirName string
ConfigFileNames []string
UserConfig UserConfig
}
func NewDefaultAppContext() *AppContext {
appName := "bz"
homeDir, _ := os.UserHomeDir()
userDir := filepath.Join(homeDir, fmt.Sprintf(".%s", appName))
userConfigFileName := filepath.Join(userDir, "config")
// load user config if it exists
var userConfig *UserConfig
if utils.FileExists(userConfigFileName) {
var err error
userConfig, err = NewUserConfigFromFile(userConfigFileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading user config (%s): %s\n", userConfigFileName, err)
os.Exit(1)
}
} else {
userConfig = &UserConfig{}
}
return &AppContext{
AppName: appName,
LockFileName: fmt.Sprintf(".%s.lock", appName),
HomeDir: homeDir,
UserDir: userDir,
UserConfigFileName: filepath.Join(userDir, "config"),
UserCacheDirName: filepath.Join(userDir, "cache"),
ConfigFileNames: []string{
fmt.Sprintf(".%s.hcl", appName),
fmt.Sprintf(".%s.json", appName),
fmt.Sprintf(".%s", appName),
},
UserConfig: *userConfig,
}
}