Skip to content

Commit

Permalink
[knf] Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Mar 4, 2024
1 parent 5bf087d commit e85ab2a
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 80 deletions.
1 change: 1 addition & 0 deletions .scripts/packages.list
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* + knf/validators/network
* + knf/validators/regexp
* + knf/validators/system
* + knf/value
* + lock
* + log
* + lscolors
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* `[options]` Fixed panic when parsing unsupported option with value passed with equal sign (`=`)
* `[options]` Code refactoring
* `[knf]` Code refactoring

### 12.102.0

Expand Down
87 changes: 7 additions & 80 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"errors"
"os"
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/essentialkaos/ek/v12/knf/value"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -405,32 +406,7 @@ func (c *Config) GetI64(name string, defvals ...int64) int64 {
val := c.data[strings.ToLower(name)]
c.mx.RUnlock()

if val == "" {
if len(defvals) == 0 {
return 0
}

return defvals[0]
}

// HEX Parsing
if len(val) >= 3 && val[0:2] == "0x" {
valHex, err := strconv.ParseInt(val[2:], 16, 0)

if err != nil {
return 0
}

return valHex
}

valInt, err := strconv.ParseInt(val, 10, 0)

if err != nil {
return 0
}

return valInt
return value.ParseInt64(val, defvals...)
}

// GetI returns configuration value as int
Expand Down Expand Up @@ -474,21 +450,7 @@ func (c *Config) GetF(name string, defvals ...float64) float64 {
val := c.data[strings.ToLower(name)]
c.mx.RUnlock()

if val == "" {
if len(defvals) == 0 {
return 0.0
}

return defvals[0]
}

valFl, err := strconv.ParseFloat(val, 64)

if err != nil {
return 0.0
}

return valFl
return value.ParseFloat(val, defvals...)
}

// GetB returns configuration value as boolean
Expand All @@ -505,20 +467,7 @@ func (c *Config) GetB(name string, defvals ...bool) bool {
val := c.data[strings.ToLower(name)]
c.mx.RUnlock()

if val == "" {
if len(defvals) == 0 {
return false
}

return defvals[0]
}

switch val {
case "", "0", "false", "no":
return false
default:
return true
}
return value.ParseBool(val, defvals...)
}

// GetM returns configuration value as file mode
Expand All @@ -535,21 +484,7 @@ func (c *Config) GetM(name string, defvals ...os.FileMode) os.FileMode {
val := c.data[strings.ToLower(name)]
c.mx.RUnlock()

if val == "" {
if len(defvals) == 0 {
return os.FileMode(0)
}

return defvals[0]
}

valM, err := strconv.ParseUint(val, 8, 32)

if err != nil {
return 0
}

return os.FileMode(valM)
return value.ParseMode(val, defvals...)
}

// GetD returns configuration value as duration
Expand All @@ -566,15 +501,7 @@ func (c *Config) GetD(name string, mod DurationMod, defvals ...time.Duration) ti
val := c.data[strings.ToLower(name)]
c.mx.RUnlock()

if val == "" {
if len(defvals) == 0 {
return time.Duration(0)
}

return defvals[0]
}

return time.Duration(c.GetI64(name)) * time.Duration(mod)
return value.ParseDuration(val, time.Duration(mod), defvals...)
}

// Is checks if given property contains given value
Expand Down
178 changes: 178 additions & 0 deletions knf/value/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package value

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"os"
"strconv"
"strings"
"time"
)

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

// ParseInt64 parses value as Int64
func ParseInt64(v string, defvals ...int64) int64 {
if v == "" {
if len(defvals) == 0 {
return 0
}

return defvals[0]
}

// HEX Parsing
if len(v) >= 3 && v[0:2] == "0x" {
vHex, err := strconv.ParseInt(v[2:], 16, 0)

if err != nil {
return 0
}

return vHex
}

vInt, err := strconv.ParseInt(v, 10, 0)

if err != nil {
return 0
}

return vInt
}

// ParseInt parses value as Int
func ParseInt(v string, defvals ...int) int {
if len(defvals) != 0 {
return int(ParseInt64(v, int64(defvals[0])))
}

return int(ParseInt64(v))
}

// ParseUint parses value as Uint
func ParseUint(v string, defvals ...uint) uint {
if len(defvals) != 0 {
return uint(ParseInt64(v, int64(defvals[0])))
}

return uint(ParseInt64(v))
}

// ParseUint64 parses value as Uint64
func ParseUint64(v string, defvals ...uint64) uint64 {
if len(defvals) != 0 {
return uint64(ParseInt64(v, int64(defvals[0])))
}

return uint64(ParseInt64(v))
}

// ParseFloat parses value as float
func ParseFloat(v string, defvals ...float64) float64 {
if v == "" {
if len(defvals) == 0 {
return 0.0
}

return defvals[0]
}

vF, err := strconv.ParseFloat(v, 64)

if err != nil {
return 0.0
}

return vF
}

// ParseFloat parses value as boolean
func ParseBool(v string, defvals ...bool) bool {
if v == "" {
if len(defvals) == 0 {
return false
}

return defvals[0]
}

switch strings.ToLower(v) {
case "", "0", "false", "no":
return false
default:
return true
}
}

// ParseMode parses value as file mode
func ParseMode(v string, defvals ...os.FileMode) os.FileMode {
if v == "" {
if len(defvals) == 0 {
return os.FileMode(0)
}

return defvals[0]
}

vM, err := strconv.ParseUint(v, 8, 32)

if err != nil {
return 0
}

return os.FileMode(vM)
}

// ParseDuration parses value as duration
func ParseDuration(v string, mod time.Duration, defvals ...time.Duration) time.Duration {
if v == "" {
if len(defvals) == 0 {
return time.Duration(0)
}

return defvals[0]
}

return time.Duration(ParseInt64(v)) * mod
}

// ParseTime parses value as time duration
func ParseTime(v string, defvals ...time.Duration) time.Duration {
if v == "" {
if len(defvals) == 0 {
return time.Duration(0)
}

return defvals[0]
}

v = strings.ToLower(v)
m := 1

switch v[len(v)-1:] {
case "s":
v, m = v[:len(v)-1], 1
case "m":
v, m = v[:len(v)-1], 60
case "h":
v, m = v[:len(v)-1], 3600
case "d":
v, m = v[:len(v)-1], 24*3600
case "w":
v, m = v[:len(v)-1], 7*24*3600
}

i, err := strconv.Atoi(v)

if err != nil {
return time.Duration(0)
}

return time.Duration(i*m) * time.Second
}
Loading

0 comments on commit e85ab2a

Please sign in to comment.