Skip to content

Commit

Permalink
create default config if it does not already exist (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Jul 31, 2023
1 parent f626b02 commit 9a986a6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[Unit]
After=casaos-message-bus.service
ConditionFileNotEmpty=/etc/casaos/user-service.conf
Description=CasaOS User Service

[Service]
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ var (

//go:embed api/user-service/openapi.yaml
_docYAML string

//go:embed build/sysroot/etc/casaos/user-service.conf.sample
_confSample string
)

func init() {
Expand All @@ -59,7 +62,7 @@ func init() {
println("git commit:", commit)
println("build date:", date)

config.InitSetup(*configFlag)
config.InitSetup(*configFlag, _confSample)

logger.LogInit(config.AppInfo.LogPath, config.AppInfo.LogSaveName, config.AppInfo.LogFileExt)

Expand Down
51 changes: 36 additions & 15 deletions pkg/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,56 @@ import (
"log"
"os"

"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
"github.com/IceWhaleTech/CasaOS-UserService/model"
"gopkg.in/ini.v1"
)

// models with default values

var CommonInfo = &model.CommonModel{
RuntimePath: "/var/run/casaos",
}
var (
CommonInfo = &model.CommonModel{
RuntimePath: constants.DefaultRuntimePath,
}

var AppInfo = &model.APPModel{
DBPath: "/var/lib/casaos",
UserDataPath: "/var/lib/casaos",
LogPath: "/var/log/casaos",
LogSaveName: "user",
LogFileExt: "log",
}
AppInfo = &model.APPModel{
DBPath: constants.DefaultDataPath,
UserDataPath: constants.DefaultDataPath,
LogPath: constants.DefaultLogPath,
LogSaveName: "user",
LogFileExt: "log",
}

var Cfg *ini.File
Cfg *ini.File
ConfigFilePath string
)

func InitSetup(config string) {
configFilePath := UserServiceConfigFilePath
func InitSetup(config string, sample string) {
ConfigFilePath = UserServiceConfigFilePath
if len(config) > 0 {
configFilePath = config
ConfigFilePath = config
}

// create default config file if not exist
if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) {
fmt.Println("config file not exist, create it")
// create config file
file, err := os.Create(ConfigFilePath)
if err != nil {
panic(err)
}
defer file.Close()

// write default config
_, err = file.WriteString(sample)
if err != nil {
panic(err)
}
}

var err error

Cfg, err = ini.Load(configFilePath)
Cfg, err = ini.Load(ConfigFilePath)
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
Expand Down

0 comments on commit 9a986a6

Please sign in to comment.