Skip to content

Commit

Permalink
Organize config pkg (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta authored Aug 4, 2024
1 parent 9666c33 commit 5ac8c51
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 186 deletions.
10 changes: 5 additions & 5 deletions cmd/dry.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func dryCmd() *cobra.Command {
log.Fatalf("Failed to load config: %v", err)
}
initLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Debug: cfg.Debug,
Format: cfg.LogFormat,
})

params, err := cmd.Flags().GetString("params")
Expand Down Expand Up @@ -76,9 +76,9 @@ func dryCmd() *cobra.Command {
defer logFile.Close()

agentLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
LogFile: logFile,
Debug: cfg.Debug,
Format: cfg.LogFormat,
LogFile: logFile,
})

dataStore := newDataStores(cfg)
Expand Down
14 changes: 7 additions & 7 deletions cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func restartCmd() *cobra.Command {
}

initLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Quiet: quiet,
Debug: cfg.Debug,
Format: cfg.LogFormat,
Quiet: quiet,
})

// Load the DAG file and stop the DAG if it is running.
Expand Down Expand Up @@ -110,10 +110,10 @@ func restartCmd() *cobra.Command {
defer logFile.Close()

agentLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
LogFile: logFile,
Quiet: quiet,
Debug: cfg.Debug,
Format: cfg.LogFormat,
LogFile: logFile,
Quiet: quiet,
})

agentLogger.Info("Workflow restart initiated",
Expand Down
10 changes: 5 additions & 5 deletions cmd/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func retryCmd() *cobra.Command {
log.Fatalf("Configuration load failed: %v", err)
}
initLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Debug: cfg.Debug,
Format: cfg.LogFormat,
})

requestID, err := cmd.Flags().GetString("req")
Expand Down Expand Up @@ -98,9 +98,9 @@ func retryCmd() *cobra.Command {
defer logFile.Close()

agentLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
LogFile: logFile,
Debug: cfg.Debug,
Format: cfg.LogFormat,
LogFile: logFile,
})

cli := newClient(cfg, dataStore, agentLogger)
Expand Down
5 changes: 0 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package cmd

import (
"github.com/daguflow/dagu/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -70,8 +69,4 @@ func initialize() {
viper.SetConfigFile(cfgFile)
return
}

viper.AddConfigPath(config.ConfigDir)
viper.SetConfigType("yaml")
viper.SetConfigName("admin")
}
5 changes: 2 additions & 3 deletions cmd/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func schedulerCmd() *cobra.Command {
log.Fatalf("Configuration load failed: %v", err)
}
logger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Debug: cfg.Debug,
Format: cfg.LogFormat,
})

if dagsOpt, _ := cmd.Flags().GetString("dags"); dagsOpt != "" {
Expand All @@ -48,7 +48,6 @@ func schedulerCmd() *cobra.Command {

logger.Info("Scheduler initialization",
"specsDirectory", cfg.DAGs,
"logLevel", cfg.LogLevel,
"logFormat", cfg.LogFormat)

ctx := cmd.Context()
Expand Down
4 changes: 2 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func serverCmd() *cobra.Command {
log.Fatalf("Configuration load failed: %v", err)
}
logger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Debug: cfg.Debug,
Format: cfg.LogFormat,
})

logger.Info("Server initialization", "host", cfg.Host, "port", cfg.Port)
Expand Down
14 changes: 7 additions & 7 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func startCmd() *cobra.Command {
}

initLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Quiet: quiet,
Debug: cfg.Debug,
Format: cfg.LogFormat,
Quiet: quiet,
})

params, err := cmd.Flags().GetString("params")
Expand Down Expand Up @@ -83,10 +83,10 @@ func startCmd() *cobra.Command {
defer logFile.Close()

agentLogger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
LogFile: logFile,
Quiet: quiet,
Debug: cfg.Debug,
Format: cfg.LogFormat,
LogFile: logFile,
Quiet: quiet,
})

dataStore := newDataStores(cfg)
Expand Down
4 changes: 2 additions & 2 deletions cmd/start_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func startAllCmd() *cobra.Command {
log.Fatalf("Configuration load failed: %v", err)
}
logger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Debug: cfg.Debug,
Format: cfg.LogFormat,
})

if dagsDir, _ := cmd.Flags().GetString("dags"); dagsDir != "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func statusCmd() *cobra.Command {
log.Fatalf("Configuration load failed: %v", err)
}
logger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Debug: cfg.Debug,
Format: cfg.LogFormat,
})

// Load the DAG file and get the current running status.
Expand Down
6 changes: 3 additions & 3 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func stopCmd() *cobra.Command {
}

logger := logger.NewLogger(logger.NewLoggerArgs{
LogLevel: cfg.LogLevel,
LogFormat: cfg.LogFormat,
Quiet: quiet,
Debug: cfg.Debug,
Format: cfg.LogFormat,
Quiet: quiet,
})

workflow, err := dag.Load(cfg.BaseConfig, args[0], "")
Expand Down
149 changes: 29 additions & 120 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Config struct {
AuthToken string // Auth token for API
LatestStatusToday bool // Show latest status today or the latest status
APIBaseURL string // Base URL for API
LogLevel string // Log level
Debug bool // Enable debug mode (verbose logging)
LogFormat string // Log format
}

Expand All @@ -63,15 +63,10 @@ type TLS struct {

var configLock sync.Mutex

const envPrefix = "DAGU"

func Load() (*Config, error) {
configLock.Lock()
defer configLock.Unlock()

viper.SetEnvPrefix(envPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// Set default values for config keys.
if err := setupViper(); err != nil {
return nil, err
Expand Down Expand Up @@ -110,22 +105,39 @@ const (
appName = "dagu"
)

var (
envPrefix = strings.ToUpper(appName)
)

func setupViper() error {
homeDir := getHomeDir()

var xdgCfg XDGConfig
xdgCfg.DataHome = xdg.DataHome
xdgCfg.ConfigHome = filepath.Join(homeDir, ".config")
if v := os.Getenv("XDG_CONFIG_HOME"); v != "" {
xdgCfg.ConfigHome = v
}

r := newResolver("DAGU_HOME", filepath.Join(homeDir, ".dagu"), xdgCfg)

viper.AddConfigPath(r.configDir)
viper.SetConfigType("yaml")
viper.SetConfigName("admin")

viper.SetEnvPrefix(envPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// Bind environment variables with config keys.
bindEnvs()

// Set default values for config keys.

// Directories
baseDirs := getBaseDirs()
viper.SetDefault("dags", baseDirs.dags)
viper.SetDefault("suspendFlagsDir", baseDirs.suspendFlags)
viper.SetDefault("dataDir", baseDirs.data)
viper.SetDefault("logDir", baseDirs.logs)
viper.SetDefault("adminLogsDir", baseDirs.adminLogs)

// Base config file
viper.SetDefault("baseConfig", getBaseConfigPath(baseDirs))
viper.SetDefault("dags", r.dagsDir)
viper.SetDefault("suspendFlagsDir", r.suspendFlagsDir)
viper.SetDefault("dataDir", r.dataDir)
viper.SetDefault("logDir", r.logsDir)
viper.SetDefault("adminLogsDir", r.adminLogsDir)
viper.SetDefault("baseConfig", r.baseConfigFile)

// Logging configurations
viper.SetDefault("logLevel", "info")
Expand All @@ -142,45 +154,6 @@ func setupViper() error {
return setExecutableDefault()
}

type baseDirs struct {
config string
dags string
suspendFlags string
data string
logs string
adminLogs string
}

const (
// Constants for config.
legacyConfigDir = ".dagu"
legacyConfigDirEnvKey = "DAGU_HOME"

// default directories
dagsDir = "dags"
suspendDir = "suspend"
)

var (
// ConfigDir is the directory to store DAGs and other configuration files.
ConfigDir = getConfigDir()
// DataDir is the directory to store history data.
DataDir = getDataDir()
// LogsDir is the directory to store logs.
LogsDir = getLogsDir()
)

func getBaseDirs() baseDirs {
return baseDirs{
config: ConfigDir,
dags: filepath.Join(ConfigDir, dagsDir),
suspendFlags: filepath.Join(ConfigDir, suspendDir),
data: DataDir,
logs: LogsDir,
adminLogs: filepath.Join(LogsDir, "admin"),
}
}

func setExecutableDefault() error {
executable, err := os.Executable()
if err != nil {
Expand All @@ -190,70 +163,6 @@ func setExecutableDefault() error {
return nil
}

func getLogsDir() string {
if v, ok := getLegacyConfigPath(); ok {
// For backward compatibility.
return filepath.Join(v, "logs")
}
return filepath.Join(xdg.DataHome, appName, "logs")
}

func getDataDir() string {
if v, ok := getLegacyConfigPath(); ok {
// For backward compatibility.
return filepath.Join(v, "data")
}
return filepath.Join(xdg.DataHome, appName, "history")
}

func getConfigDir() string {
if v, ok := getLegacyConfigPath(); ok {
return v
}
if v := os.Getenv("XDG_CONFIG_HOME"); v != "" {
return filepath.Join(v, appName)
}
return filepath.Join(getHomeDir(), ".config", appName)
}

func getHomeDir() string {
dir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("could not determine home directory: %v", err)
return ""
}
return dir
}

const (
// Base config file name for all DAGs.
baseConfig = "base.yaml"
// Legacy config path for backward compatibility.
legacyBaseConfig = "base.yaml"
)

func getBaseConfigPath(b baseDirs) string {
legacyPath := filepath.Join(b.config, legacyBaseConfig)
if _, err := os.Stat(legacyPath); err == nil {
return legacyPath
}
return filepath.Join(b.config, baseConfig)
}

func getLegacyConfigPath() (string, bool) {
// For backward compatibility.
// If the environment variable is set, use it.
if v := os.Getenv(legacyConfigDirEnvKey); v != "" {
return v, true
}
// If not, check if the legacyPath config directory exists.
legacyPath := filepath.Join(getHomeDir(), legacyConfigDir)
if _, err := os.Stat(legacyPath); err == nil {
return legacyPath, true
}
return "", false
}

func bindEnvs() {
// Server configurations
_ = viper.BindEnv("logEncodingCharset", "DAGU_LOG_ENCODING_CHARSET")
Expand Down
Loading

0 comments on commit 5ac8c51

Please sign in to comment.