Skip to content

Commit

Permalink
At least one job must be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh2dsh committed Jun 14, 2024
1 parent cb79d40 commit d6997b6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
26 changes: 9 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"log/syslog"
"os"
Expand Down Expand Up @@ -28,7 +29,7 @@ func New() *Config {
}

type Config struct {
Jobs []JobEnum `yaml:"jobs" validate:"dive"`
Jobs []JobEnum `yaml:"jobs" validate:"min=1,dive"`
Global Global `yaml:"global"`
}

Expand Down Expand Up @@ -118,7 +119,7 @@ type SnapJob struct {
Name string `yaml:"name" validate:"required"`
Pruning PruningLocal `yaml:"pruning"`
Snapshotting SnapshottingEnum `yaml:"snapshotting"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"required"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"min=1"`
MonitorSnapshots MonitorSnapshots `yaml:"monitor"`
}

Expand Down Expand Up @@ -183,7 +184,7 @@ type PlaceholderRecvOptions struct {
type PushJob struct {
ActiveJob `yaml:",inline"`
Snapshotting SnapshottingEnum `yaml:"snapshotting"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"required"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"min=1"`
Send SendOptions `yaml:"send"`
}

Expand Down Expand Up @@ -241,7 +242,7 @@ func (j *SinkJob) GetRecvOptions() *RecvOptions { return &j.Recv }
type SourceJob struct {
PassiveJob `yaml:",inline"`
Snapshotting SnapshottingEnum `yaml:"snapshotting"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"required"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"min=1"`
Send SendOptions `yaml:"send"`
}

Expand Down Expand Up @@ -309,7 +310,7 @@ type Global struct {
RpcTimeout time.Duration `yaml:"rpc_timeout" default:"1m" validate:"gt=0s"`
ZfsBin string `yaml:"zfs_bin" default:"zfs" validate:"required"`

Logging LoggingOutletEnumList `yaml:"logging" validate:"required"`
Logging LoggingOutletEnumList `yaml:"logging" validate:"min=1"`
Monitoring []MonitoringEnum `yaml:"monitoring"`
Control GlobalControl `yaml:"control"`
Serve GlobalServe `yaml:"serve"`
Expand Down Expand Up @@ -547,7 +548,7 @@ type HookEnum struct {
type HookCommand struct {
Path string `yaml:"path" validate:"required"`
Timeout time.Duration `yaml:"timeout" default:"30s" validate:"gt=0s"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"required"`
Filesystems FilesystemsFilter `yaml:"filesystems" validate:"min=1"`
HookSettingsCommon `yaml:",inline"`
}

Expand Down Expand Up @@ -718,17 +719,8 @@ func ParseConfigBytes(bytes []byte) (*Config, error) {
return nil, fmt.Errorf("init config with defaults: %w", err)
} else if err := yaml.Unmarshal(bytes, &c); err != nil {
return nil, fmt.Errorf("config unmarshal: %w", err)
}

if c == nil {
// There was no yaml document in the file, deserialize from default.
// => See TestFromdefaultsEmptyDoc in yaml-config package.
if err := yaml.Unmarshal([]byte("{}"), &c); err != nil {
return nil, fmt.Errorf("empty config unmarshal: %w", err)
}
if c == nil {
panic("the fallback to deserialize from `{}` should work")
}
} else if c == nil {
return nil, errors.New("There was no yaml document in the file")
}
c.lateInit()

Expand Down
13 changes: 5 additions & 8 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ func TestSampleConfigsAreParsedWithoutErrors(t *testing.T) {
t.Errorf("glob failed: %+v", err)
}

paths = append(paths, "../packaging/systemd-default-zrepl.yml")

for _, p := range paths {

if path.Ext(p) != ".yml" {
Expand Down Expand Up @@ -93,14 +91,13 @@ func TestEmptyConfig(t *testing.T) {
cases := []string{
"",
"\n",
// "---",
// "---\n",
"---",
"---\n",
}
for _, input := range cases {
config := testValidConfig(t, input)
require.NotNil(t, config)
require.NotNil(t, config.Global)
require.Empty(t, config.Jobs)
_, err := testConfig(t, input)
t.Log(err)
require.Error(t, err)
}
}

Expand Down
Empty file removed config/samples/empty.yml
Empty file.

0 comments on commit d6997b6

Please sign in to comment.