@@ -14,80 +14,72 @@ import (
14
14
"fmt"
15
15
"log"
16
16
"os"
17
- "path"
18
17
"path/filepath"
19
- "runtime"
20
- "strings"
21
18
19
+ "github.com/IceWhaleTech/CasaOS-Common/utils/constants"
20
+ "github.com/IceWhaleTech/CasaOS/common"
22
21
"github.com/IceWhaleTech/CasaOS/model"
23
22
"github.com/go-ini/ini"
24
23
)
25
24
26
- // 系统配置
27
- var SysInfo = & model.SysInfoModel {}
28
-
29
- // 用户相关
30
- var AppInfo = & model. APPModel {}
31
-
32
- var CommonInfo = & model. CommonModel {}
33
-
34
- // var RedisInfo = &model.RedisModel{}
35
-
36
- // server相关
37
- var ServerInfo = & model. ServerModel {}
38
-
39
- var SystemConfigInfo = & model.SystemConfig {}
40
-
41
- var FileSettingInfo = & model.FileSetting {}
25
+ var (
26
+ SysInfo = & model.SysInfoModel {}
27
+ AppInfo = & model. APPModel {
28
+ DBPath : constants . DefaultDataPath ,
29
+ LogPath : constants . DefaultLogPath ,
30
+ LogSaveName : common . SERVICENAME ,
31
+ LogFileExt : "log" ,
32
+ ShellPath : "/usr/share/casaos/shell" ,
33
+ UserDataPath : filepath . Join ( constants . DefaultDataPath , "conf" ),
34
+ }
35
+ CommonInfo = & model. CommonModel {
36
+ RuntimePath : constants . DefaultRuntimePath ,
37
+ }
38
+ ServerInfo = & model.ServerModel {}
39
+ SystemConfigInfo = & model. SystemConfig {}
40
+ FileSettingInfo = & model.FileSetting {}
42
41
43
- var Cfg * ini.File
42
+ Cfg * ini.File
43
+ ConfigFilePath string
44
+ )
44
45
45
46
// 初始化设置,获取系统的部分信息。
46
- func InitSetup (config string ) {
47
- configDir := USERCONFIGURL
47
+ func InitSetup (config string , sample string ) {
48
+ ConfigFilePath = CasaOSConfigFilePath
48
49
if len (config ) > 0 {
49
- configDir = config
50
+ ConfigFilePath = config
50
51
}
51
- if runtime .GOOS == "darwin" {
52
- configDir = "./conf/conf.conf"
52
+
53
+ // create default config file if not exist
54
+ if _ , err := os .Stat (ConfigFilePath ); os .IsNotExist (err ) {
55
+ fmt .Println ("config file not exist, create it" )
56
+ // create config file
57
+ file , err := os .Create (ConfigFilePath )
58
+ if err != nil {
59
+ panic (err )
60
+ }
61
+ defer file .Close ()
62
+
63
+ // write default config
64
+ _ , err = file .WriteString (sample )
65
+ if err != nil {
66
+ panic (err )
67
+ }
53
68
}
69
+
54
70
var err error
71
+
55
72
// 读取文件
56
- Cfg , err = ini .Load (configDir )
73
+ Cfg , err = ini .Load (ConfigFilePath )
57
74
if err != nil {
58
- Cfg , err = ini .Load ("/etc/casaos.conf" )
59
- if err != nil {
60
- Cfg , err = ini .Load ("/casaOS/server/conf/conf.ini" )
61
- if err != nil {
62
- fmt .Printf ("Fail to read file: %v" , err )
63
- os .Exit (1 )
64
- }
65
- }
75
+ panic (err )
66
76
}
77
+
67
78
mapTo ("app" , AppInfo )
68
- // mapTo("redis", RedisInfo)
69
79
mapTo ("server" , ServerInfo )
70
80
mapTo ("system" , SystemConfigInfo )
71
81
mapTo ("file" , FileSettingInfo )
72
82
mapTo ("common" , CommonInfo )
73
- SystemConfigInfo .ConfigPath = configDir
74
- if len (AppInfo .DBPath ) == 0 {
75
- AppInfo .DBPath = "/var/lib/casaos"
76
- }
77
- if len (AppInfo .LogPath ) == 0 {
78
- AppInfo .LogPath = "/var/log/casaos/"
79
- }
80
- if len (AppInfo .ShellPath ) == 0 {
81
- AppInfo .ShellPath = "/usr/share/casaos/shell"
82
- }
83
- if len (AppInfo .UserDataPath ) == 0 {
84
- AppInfo .UserDataPath = "/var/lib/casaos/conf"
85
- }
86
- if len (CommonInfo .RuntimePath ) == 0 {
87
- CommonInfo .RuntimePath = "/var/run/casaos"
88
- }
89
- Cfg .SaveTo (configDir )
90
- // AppInfo.ProjectPath = getCurrentDirectory() //os.Getwd()
91
83
}
92
84
93
85
// 映射
@@ -97,21 +89,3 @@ func mapTo(section string, v interface{}) {
97
89
log .Fatalf ("Cfg.MapTo %s err: %v" , section , err )
98
90
}
99
91
}
100
-
101
- // 获取当前执行文件绝对路径(go run)
102
- func getCurrentAbPathByCaller () string {
103
- var abPath string
104
- _ , filename , _ , ok := runtime .Caller (0 )
105
- if ok {
106
- abPath = path .Dir (filename )
107
- }
108
- return abPath
109
- }
110
-
111
- func getCurrentDirectory () string {
112
- dir , err := filepath .Abs (filepath .Dir (os .Args [0 ]))
113
- if err != nil {
114
- log .Fatal (err )
115
- }
116
- return strings .Replace (dir , "\\ " , "/" , - 1 )
117
- }
0 commit comments