Permalink
Browse files

refactor output

interface and header output is now more dynamic, changing according to
the interface name, the only part of the output line whose length we
don't know in advance. We now no longer need to have different output
lines for different OSs as well.
  • Loading branch information...
robphoenix committed Jun 7, 2017
1 parent 8a20314 commit fd3899f562fb296ba67676608b9cb80e97819cee
Showing with 48 additions and 54 deletions.
  1. +48 −32 main.go
  2. +0 −22 trawl.go
View
80 main.go
@@ -6,7 +6,6 @@ import (
"log"
"net"
"regexp"
"runtime"
"strings"
"github.com/rdegges/go-ipify"
@@ -15,10 +14,6 @@ import (
const (
// Version of current release
Version = "v0.2.1"
opSys = runtime.GOOS
win = "windows"
linux = "linux"
darwin = "darwin"
underlineChar = "-"
nameHeader = "Name"
ipv4AddrHeader = "IPv4 Address"
@@ -27,9 +22,7 @@ const (
mtuHeader = "MTU"
macHeader = "MAC Address"
ipv6AddrHeader = "IPv6 Address"
windowsString = "%-35s %-15s %-15s %-18s %-5s %-17s %s\n"
linuxString = "%-10s %-15s %-15s %-18s %-5s %-17s %s\n"
darwinString = "%-10s %-15s %-15s %-18s %-5s %-17s %s\n"
outputString = "%-*s %-15s %-15s %-18s %-5s %-17s %s\n"
)
var (
@@ -125,47 +118,47 @@ func main() {
fmt.Printf("%s\n", i.IPv6Addr)
return
}
maxLen := len(i.Name)
if names {
printHeaders()
fmt.Printf(headerString(maxLen))
}
fmt.Printf(i.String())
fmt.Printf(ifaceString(maxLen, i))
}
return
}
if names {
printHeaders()
}
var maxLen int
var ifs []*Iface
for _, iface := range getIfaces(loopback, filter) {
i, err := New(iface)
if err != nil {
log.Fatal(err)
}
fmt.Printf(i.String())
ifs = append(ifs, i)
if nameLen := len(i.Name); nameLen > maxLen {
maxLen = nameLen
}
}
if names {
fmt.Printf(headerString(maxLen))
}
for _, i := range ifs {
fmt.Printf(ifaceString(maxLen, i))
}
}
func underline(s string) string {
return strings.Repeat(underlineChar, len(s))
}
func osString() (s string) {
switch opSys {
case win:
s = windowsString
case linux:
s = linuxString
case darwin:
s = darwinString
}
return
}
func printHeaders() {
headersString := osString()
fmt.Printf(
headersString,
func headerString(l int) string {
var s string
s += fmt.Sprintf(
outputString,
l,
nameHeader,
ipv4AddrHeader,
ipv4MaskHeader,
@@ -174,8 +167,9 @@ func printHeaders() {
macHeader,
ipv6AddrHeader,
)
fmt.Printf(
headersString,
s += fmt.Sprintf(
outputString,
l,
underline(nameHeader),
underline(ipv4AddrHeader),
underline(ipv4MaskHeader),
@@ -184,6 +178,21 @@ func printHeaders() {
underline(macHeader),
underline(ipv6AddrHeader),
)
return s
}
func ifaceString(l int, i *Iface) string {
return fmt.Sprintf(
outputString,
l,
setMissingValue(i.Name),
setMissingValue(i.IPv4Addr),
setMissingValue(i.IPv4Mask),
setMissingValue(i.IPv4Network),
setMissingValue(i.MTU),
setMissingValue(i.HardwareAddr),
setMissingValue(i.IPv6Addr),
)
}
func getIfaces(loopback bool, filter string) []net.Interface {
@@ -228,3 +237,10 @@ func availableIfaces() string {
}
return strings.Join(availIfaces, ", ")
}
func setMissingValue(s string) string {
if s == "" {
return "-"
}
return s
}
View
@@ -1,7 +1,6 @@
package main
import (
"fmt"
"log"
"net"
"strconv"
@@ -56,27 +55,6 @@ func New(netIface net.Interface) (*Iface, error) {
}, nil
}
func setMissingValue(s string) string {
if s == "" {
return "-"
}
return s
}
func (iface *Iface) String() string {
ifaceString := osString()
return fmt.Sprintf(
ifaceString,
iface.Name,
setMissingValue(iface.IPv4Addr),
setMissingValue(iface.IPv4Mask),
setMissingValue(iface.IPv4Network),
setMissingValue(iface.MTU),
setMissingValue(iface.HardwareAddr),
setMissingValue(iface.IPv6Addr),
)
}
func extractAddrs(addrs []net.Addr) (ipv4, ipv6 string) {
for _, addr := range addrs {
a := addr.String()

0 comments on commit fd3899f

Please sign in to comment.