-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
65 lines (54 loc) · 1.54 KB
/
config.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
package main
import (
"errors"
"log"
"os"
"path"
"github.com/adrg/xdg"
)
var configFile = "config.json"
var packageName = "browser-profile-launcher"
var packageNameOld = "chrome-profile-selector"
func Load() (string, error) {
configFilePath, err := xdg.SearchConfigFile(path.Join(packageName, configFile))
if err != nil {
log.Println("コンフィグファイルが見つかりません:", configFilePath)
}
ret, err := loadFile(configFilePath)
if err == nil {
return ret, nil
}
oldFilePath, err := xdg.SearchConfigFile(path.Join(packageNameOld, configFile))
if err != nil {
log.Println("コンフィグファイルが見つかりません:", oldFilePath)
}
ret, err = loadFile(oldFilePath)
if err == nil {
return ret, nil
}
log.Println("コンフィグファイルの読み込み失敗")
return "", errors.New("Failed to load config file")
}
func loadFile(filePath string) (string, error) {
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
log.Println("コンフィグファイルが存在しません:", err)
return "", err
}
text, err := os.ReadFile(filePath)
if err != nil {
log.Println("コンフィグファイルを読み込めません:", err)
return "", err
}
return string(text), nil
}
func Save(text string) error {
configFilePath, err := xdg.ConfigFile(path.Join(packageName, configFile))
if err != nil {
log.Println("コンフィグファイルが見つかりません:", configFilePath)
}
if err := os.WriteFile(configFilePath, []byte(text), 0666); err != nil {
log.Fatal(err)
}
return nil
}