config.go

 1package config
 2
 3import (
 4	"log"
 5
 6	"github.com/meowgorithm/babyenv"
 7)
 8
 9// Callbacks provides an interface that can be used to run callbacks on different events.
10type Callbacks interface {
11	Tui(action string)
12	Push(repo string)
13	Fetch(repo string)
14}
15
16// Config is the configuration for Soft Serve.
17type Config struct {
18	Host            string `env:"SOFT_SERVE_HOST" default:""`
19	Port            int    `env:"SOFT_SERVE_PORT" default:"23231"`
20	KeyPath         string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
21	RepoPath        string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
22	InitialAdminKey string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" default:""`
23	Callbacks       Callbacks
24}
25
26// DefaultConfig returns a Config with the values populated with the defaults
27// or specified environment variables.
28func DefaultConfig() *Config {
29	var scfg Config
30	err := babyenv.Parse(&scfg)
31	if err != nil {
32		log.Fatalln(err)
33	}
34	return scfg.WithCallbacks(nil)
35}
36
37// WithCallbacks applies the given Callbacks to the configuration.
38func (cfg *Config) WithCallbacks(c Callbacks) *Config {
39	cfg.Callbacks = c
40	return cfg
41}