chore: 并入 iotlcm-bot 历史到 bot/(原提交 31dbc1d)

This commit is contained in:
MiFaZhan
2026-11-02 13:28:00 +08:00
36 changed files with 3006 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
package config
import (
"fmt"
"os"
"time"
"gopkg.in/yaml.v3"
)
type Config struct {
Environment string `yaml:"environment"`
ArchiveDir string `yaml:"archiveDir"`
StateDir string `yaml:"stateDir"`
PollInterval string `yaml:"pollInterval"`
Feishu FeishuConfig `yaml:"feishu"`
PollDuration time.Duration `yaml:"-"`
}
type FeishuConfig struct {
AppID string `yaml:"appId"`
AppSecret string `yaml:"appSecret"`
ChatID string `yaml:"chatId"`
}
func Load(path string) (Config, error) {
body, err := os.ReadFile(path)
if err != nil {
return Config{}, fmt.Errorf("read config %s: %w", path, err)
}
var cfg Config
if err := yaml.Unmarshal(body, &cfg); err != nil {
return Config{}, fmt.Errorf("parse config %s: %w", path, err)
}
if cfg.Environment == "" {
cfg.Environment = "preview"
}
if cfg.ArchiveDir == "" {
cfg.ArchiveDir = "/data/full-sync"
}
if cfg.StateDir == "" {
cfg.StateDir = "/data/state"
}
if cfg.PollInterval == "" {
cfg.PollInterval = "10s"
}
cfg.PollDuration, err = time.ParseDuration(cfg.PollInterval)
if err != nil || cfg.PollDuration <= 0 {
return Config{}, fmt.Errorf("invalid pollInterval %q", cfg.PollInterval)
}
return cfg, nil
}
func (c Config) ValidateSend() error {
if c.Feishu.AppID == "" || c.Feishu.AppSecret == "" {
return fmt.Errorf("feishu.appId and feishu.appSecret are required")
}
if c.Feishu.ChatID == "" {
return fmt.Errorf("feishu.chatId is required")
}
return nil
}
+32
View File
@@ -0,0 +1,32 @@
package config
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestLoadYAML(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.yaml")
body := []byte("environment: preview\narchiveDir: /data/full-sync\nstateDir: /data/state\npollInterval: 5s\nfeishu:\n appId: app-id\n appSecret: secret\n chatId: chat-id\n")
if err := os.WriteFile(path, body, 0o600); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.PollDuration != 5*time.Second || cfg.Feishu.AppID != "app-id" || cfg.ArchiveDir != "/data/full-sync" {
t.Fatalf("unexpected config: %+v", cfg)
}
if err := cfg.ValidateSend(); err != nil {
t.Fatal(err)
}
}
func TestValidateSendRejectsMissingCredentials(t *testing.T) {
if err := (Config{}).ValidateSend(); err == nil {
t.Fatal("expected validation error")
}
}