Add an experimental -ifaddrs builtin.

This commit is contained in:
Qi Xiao 2016-11-06 10:47:20 +01:00
parent 3c2df84afa
commit 410354b1df

View File

@ -11,6 +11,7 @@ import (
"io/ioutil"
"math"
"math/rand"
"net"
"os"
"path/filepath"
"reflect"
@ -192,6 +193,8 @@ func init() {
&BuiltinFn{"-stack", WrapFn(_stack)},
&BuiltinFn{"-log", WrapFn(_log)},
&BuiltinFn{"-time", WrapFn(_time)},
&BuiltinFn{"-ifaddrs", WrapFn(_ifaddrs)},
}
for _, b := range builtinFns {
builtinNamespace[FnPrefix+b.Name] = NewRoVariable(b)
@ -1135,6 +1138,16 @@ func _time(ec *EvalCtx, f FnValue) {
fmt.Fprintln(ec.ports[1].File, dt)
}
func _ifaddrs(ec *EvalCtx) {
out := ec.ports[1].Chan
addrs, err := net.InterfaceAddrs()
maybeThrow(err)
for _, addr := range addrs {
out <- String(addr.String())
}
}
func exec(ec *EvalCtx, args ...string) {
if len(args) == 0 {
args = []string{"elvish"}