From abe243e815135ac5d8f768de89fca3ccd23394f6 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 22:21:59 +0800 Subject: [PATCH] feat: #2 --- Makefile | 4 +++- cmd/client.go | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 4fc470f..a8b9883 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/client.go b/cmd/client.go index b467518..b423cc6 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -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 }