about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--import/main.go18
-rw-r--r--internal/config/config.go17
-rw-r--r--internal/server/server.go3
3 files changed, 28 insertions, 10 deletions
diff --git a/import/main.go b/import/main.go
index 1e6b87d..b8ae633 100644
--- a/import/main.go
+++ b/import/main.go
@@ -13,15 +13,31 @@ import (
 	"searchix/internal/search"
 	"strings"
 	"time"
+
+	"github.com/ardanlabs/conf/v3"
 )
 
 const timeout = 30 * time.Minute
 
+type Config struct {
+	ConfigFile string `conf:"short:c"`
+}
+
 func main() {
 	if _, found := os.LookupEnv("DEBUG"); found {
 		slog.SetLogLoggerLevel(slog.LevelDebug)
 	}
-	cfg, err := config.GetConfig()
+	var runtimeConfig Config
+	help, err := conf.Parse("", &runtimeConfig)
+	if err != nil {
+		if errors.Is(err, conf.ErrHelpWanted) {
+			log.Println(help)
+			os.Exit(1)
+		}
+		log.Panicf("parsing runtime configuration: %v", err)
+	}
+
+	cfg, err := config.GetConfig(runtimeConfig.ConfigFile)
 	if err != nil {
 		log.Fatal(err)
 	}
diff --git a/internal/config/config.go b/internal/config/config.go
index 2cf5def..5801847 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -5,7 +5,6 @@ import (
 	"maps"
 	"net/url"
 	"os"
-	"searchix/internal/file"
 	"searchix/internal/importer"
 	"time"
 
@@ -93,15 +92,16 @@ var defaultConfig = Config{
 	},
 }
 
-func GetConfig() (*Config, error) {
+func GetConfig(filename string) (*Config, error) {
 	config := defaultConfig
-	slog.Debug("reading config.toml")
-	f, err := os.Open("config.toml")
-	if err := file.NeedNotExist(err); err != nil {
-		return nil, errors.Wrap(err, "reading config.toml failed")
-	}
-	if f != nil {
+	if filename != "" {
+		slog.Debug("reading config", "filename", filename)
+		f, err := os.Open(filename)
+		if err != nil {
+			return nil, errors.Wrap(err, "reading config failed")
+		}
 		defer f.Close()
+
 		dec := toml.NewDecoder(f)
 		err = dec.Decode(&config)
 		if err != nil {
@@ -113,6 +113,7 @@ func GetConfig() (*Config, error) {
 			return nil, errors.Wrap(err, "config error")
 		}
 	}
+
 	maps.DeleteFunc(config.Sources, func(_ string, v importer.Source) bool {
 		return !v.Enable
 	})
diff --git a/internal/server/server.go b/internal/server/server.go
index 5def347..f15d011 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -43,6 +43,7 @@ type Config struct {
 	ListenAddress string  `conf:"default:localhost"`
 	Port          string  `conf:"default:3000,short:p"`
 	BaseURL       cfg.URL `conf:"default:http://localhost:3000,short:b"`
+	ConfigFile    string  `conf:"short:c"`
 }
 
 type HTTPError struct {
@@ -81,7 +82,7 @@ func applyDevModeOverrides(config *cfg.Config) {
 
 func New(runtimeConfig *Config) (*Server, error) {
 	var err error
-	config, err = cfg.GetConfig()
+	config, err = cfg.GetConfig(runtimeConfig.ConfigFile)
 	if err != nil {
 		return nil, errors.WithMessage(err, "error parsing configuration file")
 	}