Skip to content

Commit

Permalink
create default config if it does not already exist (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus committed Jul 31, 2023
1 parent 8991400 commit 0f82ecf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
10 changes: 4 additions & 6 deletions .goreleaser.debug.yaml
Expand Up @@ -112,24 +112,22 @@ builds:
goarm:
- "7"
archives:
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-v{{ .Version }}"
- name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-v{{ .Version }}
id: casaos-message-bus
builds:
- casaos-message-bus-amd64
- casaos-message-bus-arm64
- casaos-message-bus-arm-7
replacements:
arm: arm-7
files:
- build/**/*
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}"
- name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}
id: casaos-message-bus-migration-tool
builds:
- casaos-message-bus-migration-tool-amd64
- casaos-message-bus-migration-tool-arm64
- casaos-message-bus-migration-tool-arm-7
replacements:
arm: arm-7
files:
- build/sysroot/etc/**/*
checksum:
Expand Down
10 changes: 4 additions & 6 deletions .goreleaser.yaml
Expand Up @@ -142,24 +142,22 @@ builds:
goarm:
- "7"
archives:
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-v{{ .Version }}"
- name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-v{{ .Version }}
id: casaos-message-bus
builds:
- casaos-message-bus-amd64
- casaos-message-bus-arm64
- casaos-message-bus-arm-7
replacements:
arm: arm-7
files:
- build/**/*
- name_template: "{{ .Os }}-{{ .Arch }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}"
- name_template: >-
{{ .Os }}-{{- if eq .Arch "arm" }}arm-7{{- else }}{{ .Arch }}{{- end }}-{{ .ProjectName }}-migration-tool-v{{ .Version }}
id: casaos-message-bus-migration-tool
builds:
- casaos-message-bus-migration-tool-amd64
- casaos-message-bus-migration-tool-arm64
- casaos-message-bus-migration-tool-arm-7
replacements:
arm: arm-7
files:
- build/sysroot/etc/**/*
checksum:
Expand Down
@@ -1,6 +1,5 @@
[Unit]
After=casaos-gateway.service
ConditionFileNotEmpty=/etc/casaos/message-bus.conf
Description=CasaOS Message Bus Service

[Service]
Expand Down
26 changes: 23 additions & 3 deletions config/init.go
@@ -1,20 +1,23 @@
package config

import (
"fmt"
"log"
"os"

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

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

AppInfo = &model.APPModel{
LogPath: "/var/log/casaos",
LogPath: constants.DefaultLogPath,
LogSaveName: common.MessageBusServiceName,
LogFileExt: "log",
}
Expand All @@ -23,12 +26,29 @@ var (
ConfigFilePath string
)

func InitSetup(config string) {
func InitSetup(config string, sample string) {
ConfigFilePath = MessageBusConfigFilePath
if len(config) > 0 {
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)
Expand Down
5 changes: 4 additions & 1 deletion main.go
Expand Up @@ -41,6 +41,9 @@ var (

//go:embed api/message_bus/openapi.yaml
_docYAML string

//go:embed build/sysroot/etc/casaos/message-bus.conf.sample
_confSample string
)

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

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

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

Expand Down

0 comments on commit 0f82ecf

Please sign in to comment.