pull/6/head
Evan Chen 2021-10-16 22:21:59 +08:00
parent 9810fe2a8e
commit c81cbffbe1
2 changed files with 23 additions and 7 deletions

View File

@ -1,3 +1,5 @@
SHELL := /bin/bash
VERSION=$(shell git describe --tags)
BUILD=$(shell git rev-parse --short HEAD)
PROJ := $(shell basename "$(PWD)")
@ -31,7 +33,7 @@ build-mac-m1:
$(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_darwin_arm64/$(PROJ))
build-zip:
cd dist; for f in * ;do tar -czf $${f}.tar.gz $${f}; done
cd dist; for f in * ;do if ! [[ $$f =~ "gz" ]] ; then tar -czf $${f}.tar.gz $${f}; fi done
binary: build-unix build-win build-mac-m1

View File

@ -8,6 +8,7 @@ import (
"net"
"net/http"
"os"
"sort"
"strings"
"text/tabwriter"
@ -32,6 +33,12 @@ var (
cronSpec string
)
type Iface struct {
IP string
HardwareAddr string
Name string
}
var ClientCmd = &cobra.Command{
Use: "myip",
Short: "myip is a easy way to get public ip of current system.",
@ -110,8 +117,8 @@ func Scan() error {
if !noPub {
fmt.Fprintf(w, "\t%v\t%v\t\n", "PublicIP", ip)
}
for k, v := range local {
fmt.Fprintf(w, "\t%v\t%v\t\n", k, v)
for _, iface := range local {
fmt.Fprintf(w, "\t%v\t%v\t\n", iface.Name, iface.IP)
}
return w.Flush()
@ -120,7 +127,7 @@ func Scan() error {
fmt.Printf("%v ", ip)
}
for _, v := range local {
fmt.Printf("%v ", v)
fmt.Printf("%v ", v.IP)
}
fmt.Printf("\n")
}
@ -128,8 +135,8 @@ func Scan() error {
return nil
}
func GetLocalIP() (map[string]string, error) {
ret := map[string]string{}
func GetLocalIP() ([]Iface, error) {
ret := []Iface{}
ifaces, err := net.Interfaces()
if err != nil {
log.Println(err)
@ -161,10 +168,17 @@ func GetLocalIP() (map[string]string, error) {
// continue
// }
}
ret[i.Name] = ip.String()
ret = append(ret, Iface{
IP: ip.String(),
Name: i.Name,
HardwareAddr: i.HardwareAddr.String(),
})
break
}
}
sort.Slice(ret, func(i, j int) bool {
return ret[i].Name < ret[j].Name
})
return ret, nil
}