config.go

 1package config
 2
 3import (
 4	"log"
 5
 6	"github.com/charmbracelet/soft/internal/git"
 7	"github.com/charmbracelet/soft/stats"
 8	"github.com/meowgorithm/babyenv"
 9)
10
11// Config is the configuration for the soft-serve.
12type Config struct {
13	Host            string `env:"SOFT_SERVE_HOST" default:""`
14	Port            int    `env:"SOFT_SERVE_PORT" default:"23231"`
15	KeyPath         string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
16	RepoPath        string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
17	InitialAdminKey string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" default:""`
18	RepoSource      *git.RepoSource
19	Stats           stats.Stats
20}
21
22// DefaultConfig returns a Config with the values populated with the defaults
23// or specified environment variables.
24func DefaultConfig() *Config {
25	var scfg Config
26	err := babyenv.Parse(&scfg)
27	if err != nil {
28		log.Fatalln(err)
29	}
30	rs := git.NewRepoSource(scfg.RepoPath)
31	return scfg.WithRepoSource(rs).WithStats(stats.NewStats())
32}
33
34func (cfg *Config) WithStats(s stats.Stats) *Config {
35	cfg.Stats = s
36	return cfg
37}
38
39func (cfg *Config) WithRepoSource(rs *git.RepoSource) *Config {
40	cfg.RepoSource = rs
41	return cfg
42}