Skip to content

Commit 0cf353c

Browse files
authored
create default config if it does not already exist (#1304)
1 parent 5dc6297 commit 0cf353c

File tree

7 files changed

+64
-88
lines changed

7 files changed

+64
-88
lines changed

.goreleaser.debug.yaml

+4-6
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,22 @@ builds:
113113
goarm:
114114
- "7"
115115
archives:
116-
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-v{{ .Version }}"
116+
- name_template: >-
117+
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-v{{ .Version }}
117118
id: casaos
118119
builds:
119120
- casaos-amd64
120121
- casaos-arm64
121122
- casaos-arm-7
122-
replacements:
123-
arm: arm-7
124123
files:
125124
- build/**/*
126-
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}"
125+
- name_template: >-
126+
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}
127127
id: casaos-migration-tool
128128
builds:
129129
- casaos-migration-tool-amd64
130130
- casaos-migration-tool-arm64
131131
- casaos-migration-tool-arm-7
132-
replacements:
133-
arm: arm-7
134132
files:
135133
- build/sysroot/etc/**/*
136134
checksum:

.goreleaser.yaml

+4-6
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,22 @@ builds:
143143
goarm:
144144
- "7"
145145
archives:
146-
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-v{{ .Version }}"
146+
- name_template: >-
147+
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-v{{ .Version }}
147148
id: casaos
148149
builds:
149150
- casaos-amd64
150151
- casaos-arm64
151152
- casaos-arm-7
152-
replacements:
153-
arm: arm-7
154153
files:
155154
- build/**/*
156-
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}"
155+
- name_template: >-
156+
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}
157157
id: casaos-migration-tool
158158
builds:
159159
- casaos-migration-tool-amd64
160160
- casaos-migration-tool-arm64
161161
- casaos-migration-tool-arm-7
162-
replacements:
163-
arm: arm-7
164162
files:
165163
- build/sysroot/etc/**/*
166164
checksum:

build/sysroot/usr/lib/systemd/system/casaos.service

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[Unit]
22
After=casaos-message-bus.service
33
After=rclone.service
4-
ConditionFileNotEmpty=/etc/casaos/casaos.conf
54
Description=CasaOS Main Service
65

76
[Service]

cmd/migration-tool/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func init() {
7777
}
7878
}
7979

80-
config.InitSetup(configFlag)
80+
config.InitSetup(configFlag, "")
8181

8282
if len(dbFlag) == 0 {
8383
dbFlag = config.AppInfo.DBPath + "/db"

main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ var (
4848
//go:embed api/casaos/openapi.yaml
4949
_docYAML string
5050

51+
//go:embed build/sysroot/etc/casaos/casaos.conf.sample
52+
_confSample string
53+
5154
configFlag = flag.String("c", "", "config address")
5255
dbFlag = flag.String("db", "", "db path")
5356
versionFlag = flag.Bool("v", false, "version")
@@ -63,7 +66,7 @@ func init() {
6366
println("git commit:", commit)
6467
println("build date:", date)
6568

66-
config.InitSetup(*configFlag)
69+
config.InitSetup(*configFlag, _confSample)
6770

6871
logger.LogInit(config.AppInfo.LogPath, config.AppInfo.LogSaveName, config.AppInfo.LogFileExt)
6972
if len(*dbFlag) == 0 {

pkg/config/config.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
package config
1212

13-
const (
14-
USERCONFIGURL = "/etc/casaos/casaos.conf"
13+
import (
14+
"path/filepath"
15+
16+
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
1517
)
18+
19+
var CasaOSConfigFilePath = filepath.Join(constants.DefaultConfigPath, "casaos.conf")

pkg/config/init.go

+45-71
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,72 @@ import (
1414
"fmt"
1515
"log"
1616
"os"
17-
"path"
1817
"path/filepath"
19-
"runtime"
20-
"strings"
2118

19+
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
20+
"github.com/IceWhaleTech/CasaOS/common"
2221
"github.com/IceWhaleTech/CasaOS/model"
2322
"github.com/go-ini/ini"
2423
)
2524

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{}
4241

43-
var Cfg *ini.File
42+
Cfg *ini.File
43+
ConfigFilePath string
44+
)
4445

4546
// 初始化设置,获取系统的部分信息。
46-
func InitSetup(config string) {
47-
configDir := USERCONFIGURL
47+
func InitSetup(config string, sample string) {
48+
ConfigFilePath = CasaOSConfigFilePath
4849
if len(config) > 0 {
49-
configDir = config
50+
ConfigFilePath = config
5051
}
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+
}
5368
}
69+
5470
var err error
71+
5572
// 读取文件
56-
Cfg, err = ini.Load(configDir)
73+
Cfg, err = ini.Load(ConfigFilePath)
5774
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)
6676
}
77+
6778
mapTo("app", AppInfo)
68-
// mapTo("redis", RedisInfo)
6979
mapTo("server", ServerInfo)
7080
mapTo("system", SystemConfigInfo)
7181
mapTo("file", FileSettingInfo)
7282
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()
9183
}
9284

9385
// 映射
@@ -97,21 +89,3 @@ func mapTo(section string, v interface{}) {
9789
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
9890
}
9991
}
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

Comments
 (0)