Skip to content

Commit

Permalink
格式化配置内容,启动输出供检查校验
Browse files Browse the repository at this point in the history
  • Loading branch information
jorben committed Mar 12, 2024
1 parent 4acf6e5 commit 86729d8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
46 changes: 36 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"errors"
"fmt"
"github.com/jorben/rsync-object-storage/helper"
conf "github.com/ldigit/config"
"os"
"path/filepath"
Expand Down Expand Up @@ -41,6 +43,40 @@ type SyncConfig struct {
} `yaml:"sync"`
}

// GetConfig 获取解析好的配置
func GetConfig() (*SyncConfig, error) {
raw := conf.GetGlobalConfig()
if raw == nil {
return nil, errors.New("configuration is empty, please check the config file path")
}
cfg := raw.(*SyncConfig)
return cfg, nil
}

// GetString 格式化配置成字符串
func (c *SyncConfig) GetString() string {
s := fmt.Sprintln("****************** ROS *******************")
s += fmt.Sprintln("Local: -----------------------------------")
s += fmt.Sprintf(" Path:\t\t| %s\n", c.Local.Path)
s += fmt.Sprintln("Remote: ----------------------------------")
s += fmt.Sprintf(" Endpoint:\t| %s\n", c.Remote.Endpoint)
s += fmt.Sprintf(" SecretId:\t| %s\n", helper.HideSecret(c.Remote.SecretId, 12))
s += fmt.Sprintf(" SecretKey:\t| %s\n", helper.HideSecret(c.Remote.SecretKey, 12))
s += fmt.Sprintf(" Bucket:\t| %s\n", c.Remote.Bucket)
s += fmt.Sprintf(" Region:\t| %s\n", c.Remote.Region)
s += fmt.Sprintf(" Path:\t\t| %s\n", c.Remote.Path)
s += fmt.Sprintln("Sync: -----------------------------------")
s += fmt.Sprintln(" Real-time:")
s += fmt.Sprintf(" Enable:\t| %t\n", c.Sync.RealTime.Enable)
s += fmt.Sprintln(" Check-job:")
s += fmt.Sprintf(" Enable:\t| %t\n", c.Sync.CheckJob.Enable)
s += fmt.Sprintf(" Interval:\t| %d hour\n", c.Sync.CheckJob.Interval)
s += fmt.Sprintf(" Start-at:\t| %s\n", c.Sync.CheckJob.StartAt)
s += fmt.Sprintf(" Ignore:\t| %v\n", c.Sync.Ignore)
s += fmt.Sprint("******************************************")
return s
}

func loadConfig(path string) *SyncConfig {
cfg := &SyncConfig{}
if err := conf.LoadAndDecode(path, cfg); err != nil {
Expand All @@ -65,13 +101,3 @@ func loadConfig(path string) *SyncConfig {

return cfg
}

// GetConfig 获取配置
func GetConfig() (*SyncConfig, error) {
raw := conf.GetGlobalConfig()
if raw == nil {
return nil, errors.New("configuration is empty, please check the config file path")
}
cfg := raw.(*SyncConfig)
return cfg, nil
}
23 changes: 23 additions & 0 deletions helper/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helper

import (
"math"
"strings"
)

// HideSecret 隐藏字符串的中间字符
func HideSecret(secret string, count uint32) string {
length := len(secret)
if 0 == length {
return ""
}
mask := strings.Repeat("*", int(count))
if length <= int(count) {
return mask[0:length]
}

prefix := math.Ceil(float64(length-int(count)) / 2)
suffix := math.Floor(float64(length-int(count)) / 2)

return secret[0:int(prefix)] + mask + secret[length-int(suffix):]
}
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@ import (
"fmt"
"github.com/jorben/rsync-object-storage/config"
"log"
"os"
)

//const VERSION = "1.0.0"

func main() {

c, err := config.GetConfig()
if err != nil {
log.Fatalf("load config err: %s\n", err.Error())
log.Fatalf("Load config err: %s\n", err.Error())
}

// 输出配置供检查
fmt.Println(c.GetString())

// 检查本地路径可读性
if _, err = os.ReadDir(c.Local.Path); err != nil {
log.Fatalf("ReadDir err: %s\n", err.Error())
}

fmt.Printf("%v\n", c)
// 检查对象存储

// 监听本地变更事件

// 定期对账

}

0 comments on commit 86729d8

Please sign in to comment.