pull/5/head
Evan Chen 2021-10-16 22:21:59 +08:00
parent db3ff61a99
commit abe243e815
2 changed files with 23 additions and 7 deletions

View File

@ -1,3 +1,5 @@
SHELL := /bin/bash
VERSION=$(shell git describe --tags) VERSION=$(shell git describe --tags)
BUILD=$(shell git rev-parse --short HEAD) BUILD=$(shell git rev-parse --short HEAD)
PROJ := $(shell basename "$(PWD)") 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)) $(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_darwin_arm64/$(PROJ))
build-zip: 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 binary: build-unix build-win build-mac-m1

View File

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