mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 02:48:21 +00:00
add common.Duration
This commit is contained in:
parent
cc7f8b85b5
commit
a9a0bebc2f
3 changed files with 41 additions and 9 deletions
34
internal/definitions/common/time.go
Normal file
34
internal/definitions/common/time.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Duration struct {
|
||||||
|
time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON for IPNet
|
||||||
|
func (i Duration) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(int(i.Seconds()))
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON for IPNet
|
||||||
|
func (i *Duration) UnmarshalJSON(b []byte) error {
|
||||||
|
var v interface{}
|
||||||
|
if err := json.Unmarshal(b, &v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch value := v.(type) {
|
||||||
|
case float64:
|
||||||
|
i.Duration = time.Second * time.Duration(value)
|
||||||
|
return nil
|
||||||
|
case int:
|
||||||
|
i.Duration = time.Second * time.Duration(value)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return errors.New("invalid duration")
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,12 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import "nfsense.net/nfsense/internal/definitions/common"
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DHCPv4Server struct {
|
type DHCPv4Server struct {
|
||||||
Interface string `json:"interface"`
|
Interface string `json:"interface"`
|
||||||
Pool []string `json:"pool"`
|
Pool []string `json:"pool"`
|
||||||
DefaultLeaseTime time.Duration `json:"default_lease_time"`
|
DefaultLeaseTime common.Duration `json:"default_lease_time"`
|
||||||
MaxLeaseTime time.Duration `json:"max_lease_time"`
|
MaxLeaseTime common.Duration `json:"max_lease_time"`
|
||||||
|
|
||||||
GatewayMode Mode `json:"gateway_mode"`
|
GatewayMode Mode `json:"gateway_mode"`
|
||||||
Gateway *string `json:"gateway,omitempty"`
|
Gateway *string `json:"gateway,omitempty"`
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"nfsense.net/nfsense/internal/definitions/common"
|
||||||
"nfsense.net/nfsense/internal/definitions/config"
|
"nfsense.net/nfsense/internal/definitions/config"
|
||||||
"nfsense.net/nfsense/internal/util"
|
"nfsense.net/nfsense/internal/util"
|
||||||
)
|
)
|
||||||
|
@ -66,7 +66,7 @@ func getAddressObjectAsPoolRange(conf config.Config, name string) string {
|
||||||
return strings.ReplaceAll(conf.Object.Addresses[name].Range.String(), "-", " ")
|
return strings.ReplaceAll(conf.Object.Addresses[name].Range.String(), "-", " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTimeInSecond(dur time.Duration) string {
|
func getTimeInSecond(dur common.Duration) string {
|
||||||
return fmt.Sprintf("%d", int(dur.Seconds()))
|
return fmt.Sprintf("%d", int(dur.Seconds()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue