-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.go
57 lines (48 loc) · 1.3 KB
/
init.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/ahui2016/temp-files/util"
"github.com/pelletier/go-toml/v2"
"github.com/samber/lo"
)
const (
MB = 1024 * 1024
)
const (
AppConfigTOML = "app_config.toml"
AppConfigDefaultTOML = "app_config_default.toml"
PublicFolderName = "public"
FilesFolderName = "files"
OldTextFilesFolderName = "old"
)
var (
Separator = string(filepath.Separator)
app_root = filepath.Dir(executable())
app_config *AppConfig
app_config_path = filepath.Join(app_root, AppConfigTOML)
app_cfg_default_path = filepath.Join(app_root, AppConfigDefaultTOML)
public_folder = filepath.Join(app_root, PublicFolderName)
files_folder = filepath.Join(app_root, FilesFolderName)
old_text_files_folder = filepath.Join(app_root, OldTextFilesFolderName)
)
func init() {
initAppConfig()
readAppConfig()
fmt.Println(app_config)
}
func initAppConfig() {
if util.PathNotExists(app_config_path) {
err := util.CopyFile(app_config_path, app_cfg_default_path)
lo.Must0(err)
}
}
// executable returns lo.Must1(os.Executable())
func executable() string {
return lo.Must1(os.Executable())
}
func readAppConfig() {
data := lo.Must(os.ReadFile(app_config_path))
lo.Must0(toml.Unmarshal(data, &app_config))
}