Skip to content

Commit

Permalink
refactor: conf & vfs pack.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Mar 28, 2024
1 parent 27a2183 commit 426f2d5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 116 deletions.
4 changes: 2 additions & 2 deletions cmd/vasedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func init() {
fl := parseFlags()

// 根据命令行传入的配置文件地址,覆盖掉默认的配置
if conf.HasCustomConfig(fl.config) {
if conf.HasCustom(fl.config) {
err := conf.Load(fl.config, conf.Settings)
if err != nil {
clog.Failed(err)
Expand Down Expand Up @@ -90,7 +90,7 @@ func init() {

clog.Info("Initial logger setup successful")

err = vfs.SetupFS(conf.Settings.Path, conf.Folders...)
err = vfs.SetupFS(conf.Settings.Path, conf.Permissions)
if err != nil {
clog.Failed(err)
}
Expand Down
23 changes: 2 additions & 21 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ var (
Settings *ServerConfig = new(ServerConfig)
// DefaultConfig is the default configuration
DefaultConfig *ServerConfig = new(ServerConfig)
// Folders 标准目录结构
Folders = []string{"etc", "temp", "data", "index"}
)

func init() {
Expand All @@ -64,8 +62,8 @@ func init() {
_ = Settings.Unmarshal([]byte(DefaultConfigJSON))
}

// HasCustomConfig checked enable custom config
func HasCustomConfig(path string) bool {
// HasCustom checked enable custom config
func HasCustom(path string) bool {
return path != defaultFilePath
}

Expand All @@ -84,23 +82,6 @@ func Load(file string, opt *ServerConfig) error {
return v.Unmarshal(&opt)
}

// Reload 此方法只会在初始化完成之后生效,否则找不到相关的配置文件
func Reload(opt *ServerConfig) error {

// 恢复默认的 ${Settings.Path}/etc/config.yaml
v := viper.New()
v.SetConfigType(cfSuffix)
v.SetConfigName(defaultFileName)
v.AddConfigPath(filepath.Join(Settings.Path, Folders[0]))

err := v.ReadInConfig()
if err != nil {
return err
}

return v.Unmarshal(&opt)
}

func saved(path string, opt *ServerConfig) error {
// 将配置对象转换为 YAML 格式的字节数组
yamlData, _ := yaml.Marshal(&opt)
Expand Down
86 changes: 2 additions & 84 deletions conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,61 +50,6 @@ vasedb:
}
}

func TestReloadConfig(t *testing.T) {

// 创建一个临时目录用于测试
tmpDir := t.TempDir()

// 设置 Settings.Path 为临时目录
Settings.Path = tmpDir

// 创建一个配置文件并写入测试数据
configFile := filepath.Join(tmpDir, "etc", "config.yaml")
// 模拟文件中数据
configData := []byte(`
{
"vasedb": {
"port": 8080,
"path": "/test/path",
"debug": true
}
}
`)

// 设置文件系统权限
perm := os.FileMode(0755)

err := os.MkdirAll(filepath.Dir(configFile), perm)
if err != nil {
t.Fatalf("Error creating test directory: %v", err)
}
err = os.WriteFile(configFile, configData, perm)
if err != nil {
t.Fatalf("Error writing test config file: %v", err)
}

// 调用 ReloadConfig 函数
reloadedConfig := new(ServerConfig)
err = Reload(reloadedConfig)
if err != nil {
t.Fatalf("Error reloading config: %v", err)
}

// 检查重新加载的配置是否正确
expectedConfig := &ServerConfig{
VaseDB: VaseDB{
Port: 8080,
Path: "/test/path",
Debug: true,
},
}

// 采用深度比较是否一致
if !reflect.DeepEqual(reloadedConfig, expectedConfig) {
t.Errorf("Reloaded config is not as expected.\nGot: %+v\nExpected: %+v", reloadedConfig, expectedConfig)
}
}

func TestConfigLoad_Error(t *testing.T) {

// 创建一个临时目录用于测试
Expand All @@ -125,33 +70,6 @@ func TestConfigLoad_Error(t *testing.T) {

}

func TestReloadConfig_Error(t *testing.T) {

// 创建一个失败目录目录用于测试
tmpDir := t.TempDir() + "/aaa/bbb"

// 设置 Settings.Path 为临时目录
Settings.Path = tmpDir

// 调用 ReloadConfig 函数
reloadedConfig := new(ServerConfig)
err := Reload(reloadedConfig)

if err != nil && os.IsNotExist(err) {
t.Errorf("reload config error : %v", err)
}

}

func TestReloadConfig_UnmarshalError(t *testing.T) {

err := Reload(nil)
if err != nil {
t.Log(err)
}

}

func TestSavedAsConfig(t *testing.T) {

// 创建一个临时目录用于测试
Expand Down Expand Up @@ -198,7 +116,7 @@ func TestSavedConfig(t *testing.T) {
// 创建一个临时目录用于测试
tmpDir := t.TempDir()

os.Mkdir(filepath.Join(tmpDir, Folders[0]), Permissions)
os.Mkdir(filepath.Join(tmpDir, "etc"), Permissions)

// 创建一个 ServerConfig 实例
config := &ServerConfig{
Expand Down Expand Up @@ -262,7 +180,7 @@ func TestIsDefault(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HasCustomConfig(tt.flag); got != tt.want {
if got := HasCustom(tt.flag); got != tt.want {
t.Errorf("IsDefault() = %v, want %v", got, tt.want)
}
})
Expand Down
18 changes: 9 additions & 9 deletions vfs/filesys.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package vfs

import (
"io/fs"
"os"
"path/filepath"

"github.com/auula/vasedb/clog"
"github.com/auula/vasedb/conf"
"github.com/auula/vasedb/utils"
)

var (
// Folders 标准目录结构
folders = []string{"etc", "temp", "data", "index"}
)

// SetupFS build vasedb file system
func SetupFS(path string, folders ...string) error {
func SetupFS(path string, perm fs.FileMode) error {

// 拼接文件路径
for _, dir := range folders {
// 检查目录是否存在
if utils.IsExist(filepath.Join(path, dir)) {
clog.Infof("Initial %s checked successful", dir)
} else {
if !utils.IsExist(filepath.Join(path, dir)) {
// 不存在创建对应的目录
err := os.MkdirAll(filepath.Join(path, dir), conf.Permissions)
err := os.MkdirAll(filepath.Join(path, dir), perm)
if err != nil {
return err
}
}
}

// 不要写在这里如果这个包被单独拿出去使用,不能配置 clog 包使用
// clog.Info("Initial storage successful")
return nil
}

0 comments on commit 426f2d5

Please sign in to comment.