/
path.go
144 lines (126 loc) · 3.13 KB
/
path.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"fmt"
"github.com/DoG-peer/gobou/notify"
"github.com/DoG-peer/gobou/utils"
"io/ioutil"
"os"
"path/filepath"
)
// AppPath stores paths about this application
type AppPath struct {
ConfigDir string
DataDir string
CacheDir string
PluginDir string
PluginConfigDir string
ConfigFile string
Config AppConfig
}
// AppConfig stores config about this application
type AppConfig struct {
Plugins []PluginInfo
Jtalk notify.Jtalk
}
// PrepareDirs make directories and so on
func (app *AppPath) PrepareDirs() error {
dirs := []string{
app.ConfigDir,
app.DataDir,
app.CacheDir,
app.PluginDir,
app.PluginConfigDir,
}
for _, dir := range dirs {
if finfo, e := os.Stat(dir); os.IsNotExist(e) {
err := os.MkdirAll(dir, 0777)
if err != nil {
return err
}
} else if !finfo.IsDir() {
return fmt.Errorf("%s is not directory", dir)
}
}
// app.configFile
if finfo, e := os.Stat(app.ConfigFile); !os.IsNotExist(e) && finfo.IsDir() {
return fmt.Errorf("%s is directory", app.ConfigFile)
}
return nil
}
// GetHome returns absolute path of home directory
func GetHome() string {
home := os.Getenv("HOME")
if home == "" {
home = filepath.Join(os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH"))
}
return home
}
// GetAppPath initialize application
func GetAppPath(name string) AppPath {
home := GetHome()
xdgConfigDir := os.Getenv("XDG_CONFIG_DIR")
xdgDataDir := os.Getenv("XDG_DATA_DIR")
xdgCacheHome := os.Getenv("XDG_CACHE_HOME")
if xdgConfigDir == "" {
xdgConfigDir = filepath.Join(home, ".config")
}
if xdgDataDir == "" {
xdgDataDir = filepath.Join(home, ".local/share")
}
if xdgCacheHome == "" {
xdgCacheHome = filepath.Join(home, ".cache")
}
configDir := filepath.Join(xdgConfigDir, name)
return AppPath{
ConfigDir: configDir,
DataDir: filepath.Join(xdgDataDir, name),
CacheDir: filepath.Join(xdgCacheHome, name),
ConfigFile: filepath.Join(configDir, "config.json"),
PluginDir: filepath.Join(configDir, "plugin"),
PluginConfigDir: filepath.Join(configDir, "plugin_config"),
}
}
// Configure loads config file
func (app *AppPath) Configure() error {
file, err := ioutil.ReadFile(app.ConfigFile)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
err = json.Unmarshal(file, &app.Config)
if err != nil {
return err
}
return nil
}
// Add plugin to config
func (c *AppConfig) Add(plug PluginInfo) {
newConfig := []PluginInfo{plug}
for _, p := range c.Plugins {
if p.Name != plug.Name {
newConfig = append(newConfig, p)
}
}
c.Plugins = newConfig
}
func (c *AppConfig) String() string {
s, _ := json.MarshalIndent(c, "", " ")
return string(s)
}
// SaveConfig saves config
func (app *AppPath) SaveConfig() {
ioutil.WriteFile(app.ConfigFile, []byte(app.Config.String()), os.ModePerm)
}
// ShowMessage shows messages from plugins
func (app *AppPath) ShowMessage(m gobou.Message) {
if !m.NotifyMessage.IsNone() {
notify.Notify(m.NotifyMessage.Text)
}
app.Config.Jtalk.Play(m.JtalkMessage)
if !m.StdoutMessage.IsNone() {
fmt.Println(m.StdoutMessage.Text)
}
}