Skip to content

Commit 6af9581

Browse files
authored
Separate project and runtime options in runme.yaml (#584)
Replaces #553
1 parent 4231b00 commit 6af9581

File tree

16 files changed

+670
-617
lines changed

16 files changed

+670
-617
lines changed

experimental/runme.yaml

+34-32
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
# You can test it with the "runme beta" commands.
77
version: v1alpha1
88

9-
# Indicate the root of the runme project. "." means that
10-
# the project root directory will be used.
9+
# Settings that apply on at the project level.
1110
project:
12-
dir: "."
11+
# Indicate the root of the runme project. "." means that
12+
# the project root directory will be used.
13+
root: "."
1314
# If true, the project root will be searched upwards starting from "dir".
1415
# If found, the repo root will be used as the project root.
1516
find_repo_upward: true
@@ -18,38 +19,39 @@ project:
1819
- ".venv"
1920
disable_gitignore: false
2021

21-
# It's possible to point at a single file as well.
22-
# filename: "README.md"
22+
# It's possible to point the project at a single file.
23+
# filename: "README.md"
2324

24-
# List of dotenv files to load.
25-
env:
26-
use_system_env: true
27-
sources:
28-
- ".env"
29-
- ".env.local"
25+
# List of dotenv files to load.
26+
env:
27+
use_system_env: false
28+
sources:
29+
- ".env"
30+
- ".env.local"
3031

31-
# Optional Docker configuration to run code blocks in a container.
32-
docker:
33-
enabled: false
34-
image: runme-runtime:latest
35-
build:
36-
context: ./experimental/docker
37-
dockerfile: Dockerfile
32+
# The list of filters to apply to blocks.
33+
# "condition" must return a boolean value.
34+
# You can learn about the syntax at https://expr-lang.org/docs/language-definition.
35+
# Available fields are defined in [config.FilterDocumentEnv] and [config.FilterBlockEnv].
36+
filters:
37+
# Do not allow unnamed code blocks.
38+
# - type: "FILTER_TYPE_BLOCK"
39+
# condition: "is_named"
40+
# Do not allow code blocks without a language.
41+
- type: "FILTER_TYPE_BLOCK"
42+
condition: "language != ''"
43+
# Do not allow code blocks starting with "test".
44+
- type: "FILTER_TYPE_BLOCK"
45+
condition: "!hasPrefix(name, 'test')"
3846

39-
# The list of filters to apply to blocks.
40-
# "condition" must return a boolean value.
41-
# You can learn about the syntax at https://expr-lang.org/docs/language-definition.
42-
# Available fields are defined in [config.FilterDocumentEnv] and [config.FilterBlockEnv].
43-
filters:
44-
# Do not allow unnamed code blocks.
45-
# - type: "FILTER_TYPE_BLOCK"
46-
# condition: "is_named"
47-
# Do not allow code blocks without a language.
48-
- type: "FILTER_TYPE_BLOCK"
49-
condition: "language != ''"
50-
# Do not allow code blocks starting with "test".
51-
- type: "FILTER_TYPE_BLOCK"
52-
condition: "!hasPrefix(name, 'test')"
47+
runtime:
48+
# Optional Docker configuration to run code blocks in a container.
49+
docker:
50+
enabled: false
51+
image: runme-runtime:latest
52+
build:
53+
context: ./experimental/docker
54+
dockerfile: Dockerfile
5355

5456
server:
5557
# Also unix:///path/to/file.sock is supported.

internal/cmd/beta/beta_cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ All commands use the runme.yaml configuration file.`,
3030
return autoconfig.Invoke(func(cfg *config.Config) error {
3131
// Override the filename if provided.
3232
if cFlags.filename != "" {
33-
cfg.Filename = cFlags.filename
33+
cfg.ProjectFilename = cFlags.filename
3434
}
3535

3636
// Add a filter to run only tasks from the specified categories.
3737
if len(cFlags.categories) > 0 {
38-
cfg.Filters = append(cfg.Filters, &config.Filter{
38+
cfg.ProjectFilters = append(cfg.ProjectFilters, &config.Filter{
3939
Type: config.FilterTypeBlock,
4040
Condition: `len(intersection(categories, extra.categories)) > 0`,
4141
Extra: map[string]interface{}{"categories": cFlags.categories},

internal/config/autoconfig/autoconfig.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,24 @@ func getProject(c *config.Config, logger *zap.Logger) (*project.Project, error)
101101
project.WithLogger(logger),
102102
}
103103

104-
if c.Filename != "" {
105-
return project.NewFileProject(c.Filename, opts...)
104+
if c.ProjectFilename != "" {
105+
return project.NewFileProject(c.ProjectFilename, opts...)
106106
}
107107

108-
projDir := c.ProjectDir
108+
projDir := c.ProjectRoot
109109
// If no project directory is specified, use the current directory.
110110
if projDir == "" {
111111
projDir = "."
112112
}
113113

114114
opts = append(
115115
opts,
116-
project.WithIgnoreFilePatterns(c.IgnorePaths...),
117-
project.WithRespectGitignore(!c.DisableGitignore),
118-
project.WithEnvFilesReadOrder(c.EnvSourceFiles),
116+
project.WithIgnoreFilePatterns(c.ProjectIgnorePaths...),
117+
project.WithRespectGitignore(!c.ProjectDisableGitignore),
118+
project.WithEnvFilesReadOrder(c.ProjectEnvSources),
119119
)
120120

121-
if c.FindRepoUpward {
121+
if c.ProjectFindRepoUpward {
122122
opts = append(opts, project.WithFindRepoUpward())
123123
}
124124

@@ -128,7 +128,7 @@ func getProject(c *config.Config, logger *zap.Logger) (*project.Project, error)
128128
func getProjectFilters(c *config.Config) ([]project.Filter, error) {
129129
var filters []project.Filter
130130

131-
for _, filter := range c.Filters {
131+
for _, filter := range c.ProjectFilters {
132132
filter := filter
133133

134134
switch filter.Type {
@@ -197,11 +197,11 @@ func getRootConfig(cfgLoader *config.Loader, userCfgDir UserConfigDir) (*config.
197197
}
198198

199199
func getRuntime(c *config.Config, logger *zap.Logger) (command.Runtime, error) {
200-
if c.DockerEnabled {
200+
if c.RuntimeDockerEnabled {
201201
docker, err := dockerexec.New(&dockerexec.Options{
202-
BuildContext: c.DockerBuildContext,
203-
Dockerfile: c.DockerBuildDockerfile,
204-
Image: c.DockerImage,
202+
BuildContext: c.RuntimeDockerBuildContext,
203+
Dockerfile: c.RuntimeDockerBuildDockerfile,
204+
Image: c.RuntimeDockerImage,
205205
Logger: logger,
206206
})
207207
if err != nil {
@@ -215,7 +215,7 @@ func getRuntime(c *config.Config, logger *zap.Logger) (command.Runtime, error) {
215215
func getSession(cfg *config.Config, proj *project.Project) (*command.Session, error) {
216216
sess := command.NewSession()
217217

218-
if cfg.UseSystemEnv {
218+
if cfg.ProjectEnvUseSystemEnv {
219219
if err := sess.SetEnv(os.Environ()...); err != nil {
220220
return nil, err
221221
}

internal/config/autoconfig/autoconfig_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestInvokeConfig(t *testing.T) {
1717
Data: []byte("Hello, World!"),
1818
},
1919
"runme.yaml": {
20-
Data: []byte(fmt.Sprintf("version: v1alpha1\nfilename: %s\n", "README.md")),
20+
Data: []byte(fmt.Sprintf("version: v1alpha1\nproject:\n filename: %s\n", "README.md")),
2121
},
2222
}
2323

internal/config/config.go

+42-65
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,28 @@ import (
1818
// Config is a flatten configuration of runme.yaml. The purpose of it is to
1919
// unify all the different configuration versions into a single struct.
2020
type Config struct {
21-
// Dir- or git-based project fields.
22-
DisableGitignore bool
23-
IgnorePaths []string
24-
FindRepoUpward bool
25-
ProjectDir string
21+
ProjectRoot string
22+
ProjectFilename string
23+
ProjectFindRepoUpward bool
24+
ProjectIgnorePaths []string
25+
ProjectDisableGitignore bool
26+
ProjectEnvUseSystemEnv bool
27+
ProjectEnvSources []string
28+
ProjectFilters []*Filter
29+
30+
RuntimeDockerEnabled bool
31+
RuntimeDockerImage string
32+
RuntimeDockerBuildContext string
33+
RuntimeDockerBuildDockerfile string
2634

27-
// Filemode fields.
28-
Filename string
29-
30-
// Environment variable fields.
31-
EnvSourceFiles []string
32-
UseSystemEnv bool
33-
34-
Filters []*Filter
35-
36-
// Log related fields.
37-
LogEnabled bool
38-
LogPath string
39-
LogVerbose bool
40-
41-
// Server related fields.
4235
ServerAddress string
4336
ServerTLSEnabled bool
4437
ServerTLSCertFile string
4538
ServerTLSKeyFile string
4639

47-
// Docker configuration.
48-
DockerEnabled bool
49-
DockerImage string
50-
DockerBuildContext string
51-
DockerBuildDockerfile string
40+
LogEnabled bool
41+
LogPath string
42+
LogVerbose bool
5243
}
5344

5445
func ParseYAML(data []byte) (*Config, error) {
@@ -122,42 +113,41 @@ func parseYAMLv1alpha1(data []byte) (*configv1alpha1.Config, error) {
122113

123114
func configV1alpha1ToConfig(c *configv1alpha1.Config) (*Config, error) {
124115
project := c.GetProject()
116+
runtime := c.GetRuntime()
117+
server := c.GetServer()
125118
log := c.GetLog()
126119

127120
var filters []*Filter
128-
for _, f := range c.GetFilters() {
121+
for _, f := range c.GetProject().GetFilters() {
129122
filters = append(filters, &Filter{
130123
Type: f.GetType().String(),
131124
Condition: f.GetCondition(),
132125
})
133126
}
134127

135128
cfg := &Config{
136-
ProjectDir: project.GetDir(),
137-
FindRepoUpward: project.GetFindRepoUpward(),
138-
IgnorePaths: project.GetIgnorePaths(),
139-
DisableGitignore: project.GetDisableGitignore(),
140-
141-
Filename: c.GetFilename(),
142-
143-
UseSystemEnv: c.GetEnv().GetUseSystemEnv(),
144-
EnvSourceFiles: c.GetEnv().GetSources(),
145-
146-
Filters: filters,
129+
ProjectRoot: project.GetRoot(),
130+
ProjectFilename: project.GetFilename(),
131+
ProjectFindRepoUpward: project.GetFindRepoUpward(),
132+
ProjectIgnorePaths: project.GetIgnorePaths(),
133+
ProjectDisableGitignore: project.GetDisableGitignore(),
134+
ProjectEnvUseSystemEnv: project.GetEnv().GetUseSystemEnv(),
135+
ProjectEnvSources: project.GetEnv().GetSources(),
136+
ProjectFilters: filters,
137+
138+
RuntimeDockerEnabled: runtime.GetDocker().GetEnabled(),
139+
RuntimeDockerImage: runtime.GetDocker().GetImage(),
140+
RuntimeDockerBuildContext: runtime.GetDocker().GetBuild().GetContext(),
141+
RuntimeDockerBuildDockerfile: runtime.GetDocker().GetBuild().GetDockerfile(),
142+
143+
ServerAddress: server.GetAddress(),
144+
ServerTLSEnabled: server.GetTls().GetEnabled(),
145+
ServerTLSCertFile: server.GetTls().GetCertFile(),
146+
ServerTLSKeyFile: server.GetTls().GetKeyFile(),
147147

148148
LogEnabled: log.GetEnabled(),
149149
LogPath: log.GetPath(),
150150
LogVerbose: log.GetVerbose(),
151-
152-
ServerAddress: c.GetServer().GetAddress(),
153-
ServerTLSEnabled: c.GetServer().GetTls().GetEnabled(),
154-
ServerTLSCertFile: c.GetServer().GetTls().GetCertFile(),
155-
ServerTLSKeyFile: c.GetServer().GetTls().GetKeyFile(),
156-
157-
DockerEnabled: c.GetDocker().GetEnabled(),
158-
DockerImage: c.GetDocker().GetImage(),
159-
DockerBuildContext: c.GetDocker().GetBuild().GetContext(),
160-
DockerBuildDockerfile: c.GetDocker().GetBuild().GetDockerfile(),
161151
}
162152

163153
return cfg, nil
@@ -169,38 +159,25 @@ func validateConfig(cfg *Config) error {
169159
cwd = "."
170160
}
171161

172-
if err := validateProjectDir(cfg, cwd); err != nil {
162+
if err := validateInsideCwd(cfg.ProjectRoot, cwd); err != nil {
173163
return errors.Wrap(err, "failed to validate project dir")
174164
}
175165

176-
if err := validateFilename(cfg, cwd); err != nil {
166+
if err := validateInsideCwd(cfg.ProjectFilename, cwd); err != nil {
177167
return errors.Wrap(err, "failed to validate filename")
178168
}
179169

180170
return nil
181171
}
182172

183-
func validateProjectDir(cfg *Config, cwd string) error {
184-
rel, err := filepath.Rel(cwd, filepath.Join(cwd, cfg.ProjectDir))
185-
if err != nil {
186-
return errors.WithStack(err)
187-
}
188-
if strings.HasPrefix(rel, "..") {
189-
return errors.New("outside of current working directory")
190-
}
191-
192-
return nil
193-
}
194-
195-
func validateFilename(cfg *Config, cwd string) error {
196-
rel, err := filepath.Rel(cwd, filepath.Join(cwd, cfg.Filename))
173+
func validateInsideCwd(path, cwd string) error {
174+
rel, err := filepath.Rel(cwd, filepath.Join(cwd, path))
197175
if err != nil {
198176
return errors.WithStack(err)
199177
}
200178
if strings.HasPrefix(rel, "..") {
201-
return errors.New("outside of current working directory")
179+
return errors.New("outside of the current working directory")
202180
}
203-
204181
return nil
205182
}
206183

0 commit comments

Comments
 (0)