lang: core: net: Add is_mac function
This commit is contained in:
@@ -40,6 +40,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
simple.ModuleRegister(ModuleName, "is_mac", &simple.Scaffold{
|
||||||
|
T: types.NewType("func(a str) bool"),
|
||||||
|
F: IsMac,
|
||||||
|
})
|
||||||
simple.ModuleRegister(ModuleName, "macfmt", &simple.Scaffold{
|
simple.ModuleRegister(ModuleName, "macfmt", &simple.Scaffold{
|
||||||
T: types.NewType("func(a str) str"),
|
T: types.NewType("func(a str) str"),
|
||||||
F: MacFmt,
|
F: MacFmt,
|
||||||
@@ -50,6 +54,24 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsMac takes a string and returns true if it's a mac in the standard format.
|
||||||
|
func IsMac(ctx context.Context, input []types.Value) (types.Value, error) {
|
||||||
|
mac := input[0].Str()
|
||||||
|
|
||||||
|
// Check if the MAC address is valid.
|
||||||
|
if len(mac) != len("00:00:00:00:00:00") {
|
||||||
|
return nil, fmt.Errorf("invalid MAC address length: %s", mac)
|
||||||
|
}
|
||||||
|
hw, err := net.ParseMAC(mac)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &types.BoolValue{
|
||||||
|
V: hw.String() == mac,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// MacFmt takes a MAC address with hyphens and converts it to a format with
|
// MacFmt takes a MAC address with hyphens and converts it to a format with
|
||||||
// colons.
|
// colons.
|
||||||
func MacFmt(ctx context.Context, input []types.Value) (types.Value, error) {
|
func MacFmt(ctx context.Context, input []types.Value) (types.Value, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user