Skip to content

Commit

Permalink
Use default if runme.yaml is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Aug 8, 2024
1 parent ec4e6ef commit 51cacfd
Show file tree
Hide file tree
Showing 20 changed files with 2,053 additions and 468 deletions.
2 changes: 1 addition & 1 deletion experimental/runme.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ runtime:

server:
# Also unix:///path/to/file.sock is supported.
address: localhost:7890
address: localhost:7998
tls:
enabled: true
# If not specified, default paths will be used.
Expand Down
20 changes: 12 additions & 8 deletions internal/config/autoconfig/autoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,26 @@ func getProjectFilters(c *config.Config) ([]project.Filter, error) {
}

func getRootConfig(cfgLoader *config.Loader, userCfgDir UserConfigDir) (*config.Config, error) {
var cfg *config.Config

content, err := cfgLoader.RootConfig()
if err != nil {
switch err {
case nil:
if cfg, err = config.ParseYAML(content); err != nil {
return nil, err
}
case config.ErrRootConfigNotFound:
cfg = &config.ConfigDefaults
default:
return nil, errors.WithMessage(err, "failed to load root configuration")
}

cfg, err := config.ParseYAML(content)
if err != nil {
return nil, err
}

if cfg.ServerTLSEnabled {
if cfg.ServerTLSCertFile == "" {
cfg.ServerTLSCertFile = filepath.Join(string(userCfgDir), "cert.pem")
cfg.ServerTLSCertFile = filepath.Join(string(userCfgDir), "runme", "tls", "cert.pem")
}
if cfg.ServerTLSKeyFile == "" {
cfg.ServerTLSKeyFile = filepath.Join(string(userCfgDir), "key.pem")
cfg.ServerTLSKeyFile = filepath.Join(string(userCfgDir), "runme", "tls", "key.pem")
}
}

Expand Down
60 changes: 36 additions & 24 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/wrapperspb"
"gopkg.in/yaml.v3"

configv1alpha1 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/config/v1alpha1"
Expand Down Expand Up @@ -42,6 +43,10 @@ type Config struct {
LogVerbose bool
}

func Defaults() Config {
return ConfigDefaults
}

func ParseYAML(data []byte) (*Config, error) {
version, err := parseVersionFromYAML(data)
if err != nil {
Expand Down Expand Up @@ -125,32 +130,39 @@ func configV1alpha1ToConfig(c *configv1alpha1.Config) (*Config, error) {
})
}

cfg := &Config{
ProjectRoot: project.GetRoot(),
ProjectFilename: project.GetFilename(),
ProjectFindRepoUpward: project.GetFindRepoUpward(),
ProjectIgnorePaths: project.GetIgnorePaths(),
ProjectDisableGitignore: project.GetDisableGitignore(),
ProjectEnvUseSystemEnv: project.GetEnv().GetUseSystemEnv(),
ProjectEnvSources: project.GetEnv().GetSources(),
ProjectFilters: filters,

RuntimeDockerEnabled: runtime.GetDocker().GetEnabled(),
RuntimeDockerImage: runtime.GetDocker().GetImage(),
RuntimeDockerBuildContext: runtime.GetDocker().GetBuild().GetContext(),
RuntimeDockerBuildDockerfile: runtime.GetDocker().GetBuild().GetDockerfile(),

ServerAddress: server.GetAddress(),
ServerTLSEnabled: server.GetTls().GetEnabled(),
ServerTLSCertFile: server.GetTls().GetCertFile(),
ServerTLSKeyFile: server.GetTls().GetKeyFile(),

LogEnabled: log.GetEnabled(),
LogPath: log.GetPath(),
LogVerbose: log.GetVerbose(),
cfg := Defaults()
cfg.ProjectRoot = project.GetRoot()
cfg.ProjectFilename = project.GetFilename()
cfg.ProjectFindRepoUpward = unwrapBool(project.GetFindRepoUpward(), cfg.ProjectFindRepoUpward)
cfg.ProjectIgnorePaths = project.GetIgnorePaths()
cfg.ProjectDisableGitignore = unwrapBool(project.GetDisableGitignore(), cfg.ProjectDisableGitignore)
cfg.ProjectEnvUseSystemEnv = unwrapBool(project.GetEnv().GetUseSystemEnv(), cfg.ProjectEnvUseSystemEnv)
cfg.ProjectEnvSources = project.GetEnv().GetSources()
cfg.ProjectFilters = filters

cfg.RuntimeDockerEnabled = unwrapBool(runtime.GetDocker().GetEnabled(), cfg.RuntimeDockerEnabled)
cfg.RuntimeDockerImage = runtime.GetDocker().GetImage()
cfg.RuntimeDockerBuildContext = runtime.GetDocker().GetBuild().GetContext()
cfg.RuntimeDockerBuildDockerfile = runtime.GetDocker().GetBuild().GetDockerfile()

cfg.ServerAddress = server.GetAddress()
cfg.ServerTLSEnabled = unwrapBool(server.GetTls().GetEnabled(), cfg.ServerTLSEnabled)
cfg.ServerTLSCertFile = server.GetTls().GetCertFile()
cfg.ServerTLSKeyFile = server.GetTls().GetKeyFile()

cfg.LogEnabled = unwrapBool(log.GetEnabled(), cfg.LogEnabled)
cfg.LogPath = log.GetPath()
cfg.LogVerbose = unwrapBool(log.GetVerbose(), cfg.LogVerbose)

return &cfg, nil
}

func unwrapBool(wrappedVal *wrapperspb.BoolValue, defaultVal bool) bool {
if wrappedVal != nil {
return wrappedVal.Value
}

return cfg, nil
return defaultVal
}

func validateConfig(cfg *Config) error {
Expand Down
77 changes: 77 additions & 0 deletions internal/config/config_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package config

var ConfigDefaults Config

func init() {
// todo(sebastian): use inline file for easier editing
yaml := []byte(`version: v1alpha1
# Settings that apply on at the project level.
project:
# Indicate the root of the runme project. "." means that
# the project root directory will be used.
root: "."
# If true, the project root will be searched upwards starting from "dir".
# If found, the repo root will be used as the project root.
find_repo_upward: true
ignore:
- "node_modules"
- ".venv"
disable_gitignore: false
# It's possible to point the project at a single file.
# filename: "README.md"
# List of dotenv files to load.
env:
use_system_env: false
sources:
- ".env"
- ".env.local"
# The list of filters to apply to blocks.
# "condition" must return a boolean value.
# You can learn about the syntax at https://expr-lang.org/docs/language-definition.
# Available fields are defined in [config.FilterDocumentEnv] and [config.FilterBlockEnv].
# filters:
# # Do not allow unnamed code blocks.
# # - type: "FILTER_TYPE_BLOCK"
# # condition: "is_named"
# # Do not allow code blocks without a language.
# - type: "FILTER_TYPE_BLOCK"
# condition: "language != ''"
# # Do not allow code blocks starting with "test".
# - type: "FILTER_TYPE_BLOCK"
# condition: "!hasPrefix(name, 'test')"
runtime:
# Optional Docker configuration to run code blocks in a container.
docker:
enabled: false
image: runme-runtime:latest
build:
context: ./experimental/docker
dockerfile: Dockerfile
server:
# Also unix:///path/to/file.sock is supported.
address: localhost:7998
tls:
enabled: true
# If not specified, default paths will be used.
# cert_file: "/path/to/cert.pem"
# key_file: "/path/to/key.pem"
log:
enabled: false
path: "/tmp/runme.log"
verbose: false
`)

cfg, err := ParseYAML(yaml)
if err != nil {
panic(err)
}

ConfigDefaults = *cfg
}
6 changes: 4 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestParseYAML(t *testing.T) {
project:
filename: REAEDME.md
`,
expectedConfig: &Config{ProjectFilename: "REAEDME.md"},
expectedConfig: &Config{ProjectFilename: "REAEDME.md", ProjectFindRepoUpward: true, ServerTLSEnabled: true},
},
{
name: "root and filename",
Expand All @@ -35,7 +35,7 @@ project:
root: "."
filename: README.md
`,
expectedConfig: &Config{ProjectRoot: ".", ProjectFilename: "README.md"},
expectedConfig: &Config{ProjectRoot: ".", ProjectFilename: "README.md", ProjectFindRepoUpward: true, ServerTLSEnabled: true},
},
{
name: "validate filter type",
Expand Down Expand Up @@ -144,5 +144,7 @@ log:
LogEnabled: true,
LogPath: "/var/tmp/runme.log",
LogVerbose: true,

ServerTLSEnabled: true,
}
)
25 changes: 18 additions & 7 deletions pkg/api/gen/proto/go/runme/ai/v1alpha1/ai_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 51cacfd

Please sign in to comment.