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

Version 13.20.2 #557

Merged
merged 7 commits into from
Mar 8, 2025
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Changelog

### [13.20.2](https://kaos.sh/ek/13.20.2)

- **`[knf/validators/time]`** Added timezone validator
- **`[timeutil]`** Added method `DurationAs`
- **`[timeutil]`** Method `Period.DurationIn` renamed to `Period.DurationAs`
- **`[knf/validators]`** Code refactoring
- **`[timeutil]`** Code refactoring

### [13.20.1](https://kaos.sh/ek/13.20.1)

- **`[timeutil]`** Added method `Period.Stringf`
Expand Down
9 changes: 5 additions & 4 deletions knf/validators/cron/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

var (
// Expression returns an error if config property contains invalid cron expression
// Expression returns an error if configuration property contains invalid cron
// expression
Expression = validateCronExpression
)

// ////////////////////////////////////////////////////////////////////////////////// //

func validateCronExpression(config knf.IConfig, prop string, value any) error {
confVal := config.GetS(prop)
v := config.GetS(prop)

if confVal == "" {
if v == "" {
return nil
}

_, err := cron.Parse(confVal)
_, err := cron.Parse(v)

if err != nil {
return fmt.Errorf("Property %s contains invalid cron expression: %w", prop, err)
Expand Down
4 changes: 2 additions & 2 deletions knf/validators/cron/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *ValidatorSuite) TestCronValidator(c *check.C) {
err = knf.Global(configFile)
c.Assert(err, check.IsNil)

errs := knf.Validate([]*knf.Validator{
errs := knf.Validate(knf.Validators{
{"cron:test1", Expression, nil},
{"cron:test2", Expression, nil},
{"cron:test3", Expression, nil},
Expand All @@ -60,7 +60,7 @@ func (s *ValidatorSuite) TestCronValidator(c *check.C) {

c.Assert(errs, check.HasLen, 0)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"cron:test5", Expression, nil},
})

Expand Down
58 changes: 29 additions & 29 deletions knf/validators/fs/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

var (
// Perms returns error if config property contains path to object with given
// Perms returns error if configuration property contains path to object with given
// permissions
Perms = validatePerms

// Owner returns error if config property contains path to object with other
// Owner returns error if configuration property contains path to object with other
// owner
Owner = validateOwner

// OwnerGroup returns error if config property contains path to object with other
// owner group
// OwnerGroup returns error if configuration property contains path to object with
// other owner group
OwnerGroup = validateOwnerGroup

// FileMode returns error if config property contains path to object with other
// file mode
// FileMode returns error if configuration property contains path to object with
// other file mode
FileMode = validateFileMode

// MatchPattern returns error if config property contains path which doesn't match
// given shell pattern
// MatchPattern returns error if configuration property contains path which doesn't
// match given shell pattern
MatchPattern = validateMatchPattern
)

// ////////////////////////////////////////////////////////////////////////////////// //

func validatePerms(config knf.IConfig, prop string, value any) error {
target := config.GetS(prop)
v := config.GetS(prop)

if target == "" {
if v == "" {
return nil
}

Expand All @@ -65,7 +65,7 @@ func validatePerms(config knf.IConfig, prop string, value any) error {
return getValidatorInputError("Perms", prop, value)
}

if !fsutil.CheckPerms(perms, target) {
if !fsutil.CheckPerms(perms, v) {
switch perms {
case "F":
return fmt.Errorf("Property %s must be path to file", prop)
Expand Down Expand Up @@ -94,9 +94,9 @@ func validatePerms(config knf.IConfig, prop string, value any) error {
}

func validateOwner(config knf.IConfig, prop string, value any) error {
target := config.GetS(prop)
v := config.GetS(prop)

if target == "" {
if v == "" {
return nil
}

Expand All @@ -120,23 +120,23 @@ func validateOwner(config knf.IConfig, prop string, value any) error {
return fmt.Errorf("Can't find user %q on system", owner)
}

uid, _, err := fsutil.GetOwner(target)
uid, _, err := fsutil.GetOwner(v)

if err != nil {
return fmt.Errorf("Can't get owner for %q", target)
return fmt.Errorf("Can't get owner for %q", v)
}

if user.UID != uid {
return fmt.Errorf("User %s must be owner of %s", owner, target)
return fmt.Errorf("User %s must be owner of %s", owner, v)
}

return nil
}

func validateOwnerGroup(config knf.IConfig, prop string, value any) error {
target := config.GetS(prop)
v := config.GetS(prop)

if target == "" {
if v == "" {
return nil
}

Expand All @@ -160,23 +160,23 @@ func validateOwnerGroup(config knf.IConfig, prop string, value any) error {
return fmt.Errorf("Can't find group %q on system", ownerGroup)
}

_, gid, err := fsutil.GetOwner(target)
_, gid, err := fsutil.GetOwner(v)

if err != nil {
return fmt.Errorf("Can't get owner group for %q", target)
return fmt.Errorf("Can't get owner group for %q", v)
}

if group.GID != gid {
return fmt.Errorf("Group %s must be owner of %s", ownerGroup, target)
return fmt.Errorf("Group %s must be owner of %s", ownerGroup, v)
}

return nil
}

func validateFileMode(config knf.IConfig, prop string, value any) error {
target := config.GetS(prop)
v := config.GetS(prop)

if target == "" {
if v == "" {
return nil
}

Expand All @@ -194,24 +194,24 @@ func validateFileMode(config knf.IConfig, prop string, value any) error {
return getValidatorInputError("FileMode", prop, value)
}

targetPerms := fsutil.GetMode(target)
targetPerms := fsutil.GetMode(v)

if targetPerms == 0 {
return fmt.Errorf("Can't get mode for %q", target)
return fmt.Errorf("Can't get mode for %q", v)
}

if mode != targetPerms {
return fmt.Errorf(
"%s has different mode (%o != %o)", target, targetPerms, mode)
"%s has different mode (%o != %o)", v, targetPerms, mode)
}

return nil
}

func validateMatchPattern(config knf.IConfig, prop string, value any) error {
target := config.GetS(prop)
v := config.GetS(prop)

if target == "" {
if v == "" {
return nil
}

Expand All @@ -229,7 +229,7 @@ func validateMatchPattern(config knf.IConfig, prop string, value any) error {
return getValidatorInputError("MatchPattern", prop, value)
}

isMatch, err := path.Match(pattern, target)
isMatch, err := path.Match(pattern, v)

if err != nil {
return fmt.Errorf("Can't parse shell pattern: %v", err)
Expand Down
28 changes: 14 additions & 14 deletions knf/validators/fs/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *ValidatorSuite) TestPermsValidator(c *C) {
err := knf.Global(configFile)
c.Assert(err, IsNil)

errs := knf.Validate([]*knf.Validator{
errs := knf.Validate(knf.Validators{
{"test:test0", Perms, "FR"},
{"test:test1", Perms, "FR"},
})
Expand All @@ -60,7 +60,7 @@ func (s *ValidatorSuite) TestPermsValidator(c *C) {
err = knf.Global(configFile)
c.Assert(err, IsNil)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", Perms, "F"},
{"test:test1", Perms, "FR"},
{"test:test1", Perms, "FW"},
Expand Down Expand Up @@ -99,14 +99,14 @@ func (s *ValidatorSuite) TestOwnerValidator(c *C) {
err := knf.Global(configFile)
c.Assert(err, IsNil)

errs := knf.Validate([]*knf.Validator{
errs := knf.Validate(knf.Validators{
{"test:test0", Owner, "root"},
{"test:test1", Owner, "root"},
})

c.Assert(errs, HasLen, 0)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", Owner, "nobody"},
{"test:test1", Owner, "somerandomuser"},
{"test:test1", Owner, ""},
Expand All @@ -125,7 +125,7 @@ func (s *ValidatorSuite) TestOwnerValidator(c *C) {
err = knf.Global(configFile)
c.Assert(err, IsNil)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", Owner, "root"},
})

Expand All @@ -142,20 +142,20 @@ func (s *ValidatorSuite) TestOwnerGroupValidator(c *C) {
var errs []error

if runtime.GOOS == "darwin" {
errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test0", OwnerGroup, "wheel"},
{"test:test1", OwnerGroup, "wheel"},
})
} else {
errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test0", OwnerGroup, "root"},
{"test:test1", OwnerGroup, "root"},
})
}

c.Assert(errs, HasLen, 0)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", OwnerGroup, "daemon"},
{"test:test1", OwnerGroup, "somerandomgroup"},
{"test:test1", OwnerGroup, ""},
Expand All @@ -174,7 +174,7 @@ func (s *ValidatorSuite) TestOwnerGroupValidator(c *C) {
err = knf.Global(configFile)
c.Assert(err, IsNil)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", OwnerGroup, "daemon"},
})

Expand All @@ -188,14 +188,14 @@ func (s *ValidatorSuite) TestFileModeValidator(c *C) {
err := knf.Global(configFile)
c.Assert(err, IsNil)

errs := knf.Validate([]*knf.Validator{
errs := knf.Validate(knf.Validators{
{"test:test0", FileMode, os.FileMode(0644)},
{"test:test1", FileMode, os.FileMode(0644)},
})

c.Assert(errs, HasLen, 0)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", FileMode, os.FileMode(0777)},
})

Expand All @@ -207,7 +207,7 @@ func (s *ValidatorSuite) TestFileModeValidator(c *C) {
err = knf.Global(configFile)
c.Assert(err, IsNil)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", FileMode, os.FileMode(0644)},
{"test:test1", FileMode, os.FileMode(0)},
{"test:test1", FileMode, 100},
Expand All @@ -226,14 +226,14 @@ func (s *ValidatorSuite) TestMatchPattern(c *C) {
err := knf.Global(configFile)
c.Assert(err, IsNil)

errs := knf.Validate([]*knf.Validator{
errs := knf.Validate(knf.Validators{
{"test:test0", MatchPattern, "/etc/*"},
{"test:test1", MatchPattern, "/etc/*"},
})

c.Assert(errs, HasLen, 0)

errs = knf.Validate([]*knf.Validator{
errs = knf.Validate(knf.Validators{
{"test:test1", MatchPattern, "/var/*"},
{"test:test1", MatchPattern, "[]a"},
{"test:test1", MatchPattern, ""},
Expand Down
Loading