Add support to on/off arguments in the list command

pull/17/head
Taylon Silmer 8 years ago
parent 4ac0eeb896
commit 007ab721c3

@ -193,6 +193,17 @@ func OnOff(c *cli.Context) {
// Ls command shows a list of hostnames in the hosts file
func Ls(c *cli.Context) {
var status bool
printall := true
if len(c.Args()) != 0 {
printall = false
status = false
if c.Args()[0] == "on" {
status = true
}
}
hostsfile := AlwaysLoadHostFile(c)
maxdomain := 0
maxip := 0
@ -207,12 +218,22 @@ func Ls(c *cli.Context) {
}
}
for _, hostname := range hostsfile.Hosts {
printHost := func(hostname *Hostname) {
fmt.Printf("%s -> %s %s\n",
StrPadRight(hostname.Domain, maxdomain),
StrPadRight(hostname.IP.String(), maxip),
hostname.FormatEnabled())
}
for _, hostname := range hostsfile.Hosts {
if printall {
printHost(hostname)
} else {
if hostname.Enabled == status {
printHost(hostname)
}
}
}
}
const fixHelp = `Programmatically rewrite your hostsfile.

@ -2,6 +2,7 @@ package hostess
import (
"flag"
"io/ioutil"
"os"
"testing"
@ -15,9 +16,45 @@ func TestStrPadRight(t *testing.T) {
assert.Equal(t, "string", StrPadRight("string", 0), "6-length 0 padding")
}
func captureOutput(f func()) string {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout
return string(out[:])
}
func TestLs(t *testing.T) {
os.Setenv("HOSTESS_PATH", "test-fixtures/hostfile1")
defer os.Setenv("HOSTESS_PATH", "")
c := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
Ls(c)
app := cli.NewApp()
context := cli.NewContext(app, &flag.FlagSet{}, nil)
Ls(context)
// Test on/off arguments functionality
os.Setenv("HOSTESS_PATH", "test-fixtures/ls_on_off")
set := flag.NewFlagSet("test", 0)
set.Parse([]string{"list", "on"})
context = cli.NewContext(app, set, nil)
command := cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "Testing Ls",
Description: "Testing Ls",
Action: Ls,
}
output := captureOutput(func() {
command.Run(context)
})
assert.Equal(t, "chocolate.pie.example.com -> fe:23b3:890e:342e::ef (On)\n", output)
}

@ -0,0 +1,2 @@
# fe:23b3:890e:342e::ef dev.strawberry.pie.example.com
fe:23b3:890e:342e::ef chocolate.pie.example.com
Loading…
Cancel
Save