1package config
2
3import (
4 "log"
5 "path/filepath"
6
7 "github.com/caarlos0/env/v6"
8)
9
10//
11type GitHookOption struct {
12 OldSha string
13 NewSha string
14 RefName string
15}
16
17// GitHooks provides an interface for git server-side hooks.
18type GitHooks interface {
19 PreReceive(string, []GitHookOption)
20 Update(string, GitHookOption)
21 PostReceive(string, []GitHookOption)
22}
23
24// Callbacks provides an interface that can be used to run callbacks on different events.
25type Callbacks interface {
26 Tui(action string)
27 Push(repo string)
28 Fetch(repo string)
29}
30
31// Config is the configuration for Soft Serve.
32type Config struct {
33 BindAddr string `env:"SOFT_SERVE_BIND_ADDRESS" envDefault:""`
34 Host string `env:"SOFT_SERVE_HOST" envDefault:"localhost"`
35 Port int `env:"SOFT_SERVE_PORT" envDefault:"23231"`
36 KeyPath string `env:"SOFT_SERVE_KEY_PATH"`
37 InternalKeyPath string `env:"SOFT_SERVE_INTERNAL_KEY_PATH"`
38 RepoPath string `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
39 InitialAdminKeys []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
40 Callbacks Callbacks
41 ErrorLog *log.Logger
42}
43
44// DefaultConfig returns a Config with the values populated with the defaults
45// or specified environment variables.
46func DefaultConfig() *Config {
47 cfg := &Config{ErrorLog: log.Default()}
48 if err := env.Parse(cfg); err != nil {
49 log.Fatalln(err)
50 }
51 if cfg.KeyPath == "" {
52 // NB: cross-platform-compatible path
53 cfg.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
54 }
55 if cfg.InternalKeyPath == "" {
56 // NB: cross-platform-compatible path
57 cfg.InternalKeyPath = filepath.Join(".ssh", "soft_serve_internal_ed25519")
58 }
59 return cfg.WithCallbacks(nil)
60}
61
62// WithCallbacks applies the given Callbacks to the configuration.
63func (c *Config) WithCallbacks(callbacks Callbacks) *Config {
64 c.Callbacks = callbacks
65 return c
66}
67
68// WithErrorLogger sets the error logger for the configuration.
69func (c *Config) WithErrorLogger(logger *log.Logger) *Config {
70 c.ErrorLog = logger
71 return c
72}