1package config
 2
 3import (
 4	glog "log"
 5	"path/filepath"
 6
 7	"github.com/caarlos0/env/v6"
 8	"github.com/charmbracelet/log"
 9)
10
11// Callbacks provides an interface that can be used to run callbacks on different events.
12type Callbacks interface {
13	Tui(action string)
14	Push(repo string)
15	Fetch(repo string)
16}
17
18// Config is the configuration for Soft Serve.
19type Config struct {
20	BindAddr         string   `env:"SOFT_SERVE_BIND_ADDRESS" envDefault:""`
21	Host             string   `env:"SOFT_SERVE_HOST" envDefault:"localhost"`
22	Port             int      `env:"SOFT_SERVE_PORT" envDefault:"23231"`
23	KeyPath          string   `env:"SOFT_SERVE_KEY_PATH"`
24	RepoPath         string   `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
25	InitialAdminKeys []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
26	Callbacks        Callbacks
27	ErrorLog         *glog.Logger
28}
29
30// DefaultConfig returns a Config with the values populated with the defaults
31// or specified environment variables.
32func DefaultConfig() *Config {
33	cfg := &Config{ErrorLog: log.StandardLog(log.StandardLogOption{ForceLevel: log.ErrorLevel})}
34	if err := env.Parse(cfg); err != nil {
35		log.Fatal(err)
36	}
37	if cfg.KeyPath == "" {
38		// NB: cross-platform-compatible path
39		cfg.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
40	}
41	return cfg.WithCallbacks(nil)
42}
43
44// WithCallbacks applies the given Callbacks to the configuration.
45func (c *Config) WithCallbacks(callbacks Callbacks) *Config {
46	c.Callbacks = callbacks
47	return c
48}
49
50// WithErrorLogger sets the error logger for the configuration.
51func (c *Config) WithErrorLogger(logger *glog.Logger) *Config {
52	c.ErrorLog = logger
53	return c
54}