Skip to content

Commit c9105fa

Browse files
authored
refact pkg/acquisition: journalctl configuration (#4057)
1 parent a6b0802 commit c9105fa

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

cmd/crowdsec-cli/clisetup/setup/detect_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ detect:
390390
source: journalctl
391391
filename: /path/to/file.log`,
392392
want: nil,
393-
wantErr: `invalid acquisition spec for foobar: cannot parse journalctl acquisition config: [1:1] unknown field "filename"`,
393+
wantErr: `invalid acquisition spec for foobar: cannot parse: [1:1] unknown field "filename"`,
394394
}, {
395395
name: "source file: required fields",
396396
config: `

pkg/acquisition/modules/journalctl/config.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,58 @@ type Configuration struct {
2121
since string // set only by DSN
2222
}
2323

24-
func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
25-
config := Configuration{}
24+
func ConfigurationFromYAML(y []byte) (Configuration, error) {
25+
var cfg Configuration
2626

27-
// defaults
28-
config.Mode = configuration.TAIL_MODE
27+
if err := yaml.UnmarshalWithOptions(y, &cfg, yaml.Strict()); err != nil {
28+
return cfg, fmt.Errorf("cannot parse: %s", yaml.FormatError(err, false, false))
29+
}
2930

30-
err := yaml.UnmarshalWithOptions(yamlConfig, &config, yaml.Strict())
31-
if err != nil {
32-
return fmt.Errorf("cannot parse journalctl acquisition config: %s", yaml.FormatError(err, false, false))
31+
cfg.SetDefaults()
32+
33+
if err := cfg.Validate(); err != nil {
34+
return cfg, err
3335
}
3436

35-
if len(config.Filters) == 0 {
37+
return cfg, nil
38+
}
39+
40+
func (c *Configuration) SetDefaults() {
41+
if c.Mode == "" {
42+
c.Mode = configuration.TAIL_MODE
43+
}
44+
}
45+
46+
func (c *Configuration) Validate() error {
47+
if len(c.Filters) == 0 {
3648
return errors.New("journalctl_filter is required")
3749
}
3850

39-
s.config = config
51+
return nil
52+
}
53+
54+
func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
55+
cfg, err := ConfigurationFromYAML(yamlConfig)
56+
if err != nil {
57+
return err
58+
}
59+
60+
s.config = cfg
61+
4062
s.setSrc(s.config.Filters)
4163

4264
return nil
4365
}
4466

4567
func (s *Source) Configure(_ context.Context, yamlConfig []byte, logger *log.Entry, metricsLevel metrics.AcquisitionMetricsLevel) error {
46-
s.metricsLevel = metricsLevel
47-
4868
if err := s.UnmarshalConfig(yamlConfig); err != nil {
4969
return err
5070
}
5171

5272
s.setLogger(logger, 0, s.src)
5373

74+
s.metricsLevel = metricsLevel
75+
5476
return nil
5577
}
5678

pkg/acquisition/modules/journalctl/journalctl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestBadConfiguration(t *testing.T) {
3333
}{
3434
{
3535
config: `foobar: asd.log`,
36-
expectedErr: `cannot parse journalctl acquisition config: [1:1] unknown field "foobar"`,
36+
expectedErr: `cannot parse: [1:1] unknown field "foobar"`,
3737
},
3838
{
3939
config: `

0 commit comments

Comments
 (0)