Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use env to generate config.Environ() #414

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 10 additions & 36 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,46 +147,13 @@ type Config struct {

// DataPath is the path to the directory where Soft Serve will store its data.
DataPath string `env:"DATA_PATH" yaml:"-"`

env []string
}

// Environ returns the config as a list of environment variables.
func (c *Config) Environ() []string {
envs := []string{}
if c == nil {
return envs
}

// TODO: do this dynamically
envs = append(envs, []string{
fmt.Sprintf("SOFT_SERVE_DATA_PATH=%s", c.DataPath),
fmt.Sprintf("SOFT_SERVE_NAME=%s", c.Name),
fmt.Sprintf("SOFT_SERVE_INITIAL_ADMIN_KEYS=%s", strings.Join(c.InitialAdminKeys, "\n")),
fmt.Sprintf("SOFT_SERVE_SSH_LISTEN_ADDR=%s", c.SSH.ListenAddr),
fmt.Sprintf("SOFT_SERVE_SSH_PUBLIC_URL=%s", c.SSH.PublicURL),
fmt.Sprintf("SOFT_SERVE_SSH_KEY_PATH=%s", c.SSH.KeyPath),
fmt.Sprintf("SOFT_SERVE_SSH_CLIENT_KEY_PATH=%s", c.SSH.ClientKeyPath),
fmt.Sprintf("SOFT_SERVE_SSH_MAX_TIMEOUT=%d", c.SSH.MaxTimeout),
fmt.Sprintf("SOFT_SERVE_SSH_IDLE_TIMEOUT=%d", c.SSH.IdleTimeout),
fmt.Sprintf("SOFT_SERVE_GIT_LISTEN_ADDR=%s", c.Git.ListenAddr),
fmt.Sprintf("SOFT_SERVE_GIT_PUBLIC_URL=%s", c.Git.PublicURL),
fmt.Sprintf("SOFT_SERVE_GIT_MAX_TIMEOUT=%d", c.Git.MaxTimeout),
fmt.Sprintf("SOFT_SERVE_GIT_IDLE_TIMEOUT=%d", c.Git.IdleTimeout),
fmt.Sprintf("SOFT_SERVE_GIT_MAX_CONNECTIONS=%d", c.Git.MaxConnections),
fmt.Sprintf("SOFT_SERVE_HTTP_LISTEN_ADDR=%s", c.HTTP.ListenAddr),
fmt.Sprintf("SOFT_SERVE_HTTP_TLS_KEY_PATH=%s", c.HTTP.TLSKeyPath),
fmt.Sprintf("SOFT_SERVE_HTTP_TLS_CERT_PATH=%s", c.HTTP.TLSCertPath),
fmt.Sprintf("SOFT_SERVE_HTTP_PUBLIC_URL=%s", c.HTTP.PublicURL),
fmt.Sprintf("SOFT_SERVE_STATS_LISTEN_ADDR=%s", c.Stats.ListenAddr),
fmt.Sprintf("SOFT_SERVE_LOG_FORMAT=%s", c.Log.Format),
fmt.Sprintf("SOFT_SERVE_LOG_TIME_FORMAT=%s", c.Log.TimeFormat),
fmt.Sprintf("SOFT_SERVE_DB_DRIVER=%s", c.DB.Driver),
fmt.Sprintf("SOFT_SERVE_DB_DATA_SOURCE=%s", c.DB.DataSource),
fmt.Sprintf("SOFT_SERVE_LFS_ENABLED=%t", c.LFS.Enabled),
fmt.Sprintf("SOFT_SERVE_LFS_SSH_ENABLED=%t", c.LFS.SSHEnabled),
fmt.Sprintf("SOFT_SERVE_JOBS_MIRROR_PULL=%s", c.Jobs.MirrorPull),
}...)

return envs
return c.env
}

// IsDebug returns true if the server is running in debug mode.
Expand Down Expand Up @@ -232,6 +199,13 @@ func parseEnv(cfg *Config) error {
// Override with environment variables
if err := env.ParseWithOptions(cfg, env.Options{
Prefix: "SOFT_SERVE_",
OnSet: func(tag string, value interface{}, _ bool) {
val := fmt.Sprintf("%v", value)
if val == "" {
return
}
cfg.env = append(cfg.env, fmt.Sprintf("%s=%v", tag, value))
},
}); err != nil {
return fmt.Errorf("parse environment variables: %w", err)
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"os"
"testing"

"github.com/matryer/is"
Expand All @@ -10,24 +9,23 @@ import (
func TestParseMultipleKeys(t *testing.T) {
is := is.New(t)
td := t.TempDir()
is.NoErr(os.Setenv("SOFT_SERVE_INITIAL_ADMIN_KEYS", "testdata/k1.pub\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8 a@b"))
is.NoErr(os.Setenv("SOFT_SERVE_DATA_PATH", td))
t.Cleanup(func() {
is.NoErr(os.Unsetenv("SOFT_SERVE_INITIAL_ADMIN_KEYS"))
is.NoErr(os.Unsetenv("SOFT_SERVE_DATA_PATH"))
})
t.Setenv("SOFT_SERVE_INITIAL_ADMIN_KEYS", "testdata/k1.pub\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8 a@b")
t.Setenv("SOFT_SERVE_DATA_PATH", td)
cfg := DefaultConfig()
is.NoErr(cfg.ParseEnv())
is.Equal(cfg.InitialAdminKeys, []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINMwLvyV3ouVrTysUYGoJdl5Vgn5BACKov+n9PlzfPwH",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8",
})
is.Equal(cfg.Environ(), []string{
"SOFT_SERVE_INITIAL_ADMIN_KEYS=testdata/k1.pub\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8 a@b",
"SOFT_SERVE_DATA_PATH=" + td,
})
}

func TestMergeInitAdminKeys(t *testing.T) {
is := is.New(t)
is.NoErr(os.Setenv("SOFT_SERVE_INITIAL_ADMIN_KEYS", "testdata/k1.pub"))
t.Cleanup(func() { is.NoErr(os.Unsetenv("SOFT_SERVE_INITIAL_ADMIN_KEYS")) })
t.Setenv("SOFT_SERVE_INITIAL_ADMIN_KEYS", "testdata/k1.pub")
cfg := &Config{
DataPath: t.TempDir(),
InitialAdminKeys: []string{"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8 a@b"},
Expand Down
13 changes: 3 additions & 10 deletions pkg/ssh/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,10 @@ func TestSession(t *testing.T) {

func setup(tb testing.TB) (*gossh.Session, func() error) {
tb.Helper()
is := is.New(tb)
dp := tb.TempDir()
is.NoErr(os.Setenv("SOFT_SERVE_DATA_PATH", dp))
is.NoErr(os.Setenv("SOFT_SERVE_GIT_LISTEN_ADDR", ":9418"))
is.NoErr(os.Setenv("SOFT_SERVE_SSH_LISTEN_ADDR", fmt.Sprintf(":%d", test.RandomPort())))
tb.Cleanup(func() {
is.NoErr(os.Unsetenv("SOFT_SERVE_DATA_PATH"))
is.NoErr(os.Unsetenv("SOFT_SERVE_GIT_LISTEN_ADDR"))
is.NoErr(os.Unsetenv("SOFT_SERVE_SSH_LISTEN_ADDR"))
is.NoErr(os.RemoveAll(dp))
})
tb.Setenv("SOFT_SERVE_DATA_PATH", dp)
tb.Setenv("SOFT_SERVE_GIT_LISTEN_ADDR", ":9418")
tb.Setenv("SOFT_SERVE_SSH_LISTEN_ADDR", fmt.Sprintf(":%d", test.RandomPort()))
ctx := context.TODO()
cfg := config.DefaultConfig()
if err := cfg.Validate(); err != nil {
Expand Down
Loading