feat: add configuration flags and enhance config initialization

This commit is contained in:
krau
2025-12-19 13:49:13 +08:00
parent d3cc56c8e6
commit df64ec3069
4 changed files with 119 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/krau/SaveAny-Bot/config"
"github.com/spf13/cobra"
)
@@ -13,6 +14,10 @@ var rootCmd = &cobra.Command{
Run: Run,
}
func init() {
config.RegisterFlags(rootCmd)
}
func Execute(ctx context.Context) {
if err := rootCmd.ExecuteContext(ctx); err != nil {
fmt.Println(err)

View File

@@ -34,7 +34,7 @@ func Run(cmd *cobra.Command, _ []string) {
})
ctx = log.WithContext(ctx, logger)
exitChan, err := initAll(ctx)
exitChan, err := initAll(ctx, cmd)
if err != nil {
logger.Fatal("Init failed", "error", err)
}
@@ -51,8 +51,9 @@ func Run(cmd *cobra.Command, _ []string) {
cleanCache()
}
func initAll(ctx context.Context) (<-chan struct{}, error) {
if err := config.Init(ctx); err != nil {
func initAll(ctx context.Context, cmd *cobra.Command) (<-chan struct{}, error) {
configFile := config.GetConfigFile(cmd)
if err := config.Init(ctx, configFile); err != nil {
return nil, fmt.Errorf("failed to load config: %w", err)
}
cache.Init()