Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to on/off arguments in the list command and Fish shell completions #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions autocompletions/hostess.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function list_domains
hostess list $argv | cut -d " " -f 1
end

complete -c hostess -x -n '__fish_use_subcommand' -a add -d 'Add or replace a hosts entry.'
complete -c hostess -x -n '__fish_use_subcommand' -a aff -d 'Add or replace a hosts entry in an off state.'
complete -c hostess -x -n '__fish_use_subcommand' -a has -d 'Exit code 0 if the domain is in your hostfile, 1 otherwise.'
complete -c hostess -f -n '__fish_use_subcommand' -a fix -d 'Rewrite your hosts file.'
complete -c hostess -x -n '__fish_use_subcommand' -a dump -d 'Dump your hostfile as JSON.'
complete -c hostess -r -n '__fish_use_subcommand' -a apply -d 'Add entries from a JSON file.'

complete -c hostess -f -n '__fish_use_subcommand' -a list -d 'List domains, target ips, and on/off status.'
complete -c hostess -f -n '__fish_use_subcommand' -a ls -d 'List domains, target ips, and on/off status.'
complete -c hostess -f -A -n '__fish_seen_subcommand_from list' -a off -d 'List only disabled domains.'
complete -c hostess -f -A -n '__fish_seen_subcommand_from ls' -a off -d 'List only disabled domains.'
complete -c hostess -f -A -n '__fish_seen_subcommand_from list' -a on -d 'List only enabled domains.'
complete -c hostess -f -A -n '__fish_seen_subcommand_from ls' -a on -d 'List only enabled domains.'

complete -c hostess -x -n '__fish_use_subcommand' -a del -d 'Remove a host entry.'
complete -c hostess -x -n '__fish_use_subcommand' -a rm -d 'Remove a host entry.'
complete -c hostess -f -A -n '__fish_seen_subcommand_from del' -a '(list_domains)'
complete -c hostess -f -A -n '__fish_seen_subcommand_from rm' -a '(list_domains)'

complete -c hostess -x -n '__fish_use_subcommand' -a off -d "Disable a domain (but don't remove it completely)."
complete -c hostess -f -A -n '__fish_seen_subcommand_from off' -a '(list_domains on)'

complete -c hostess -x -n '__fish_use_subcommand' -a on -d 'Re-enable a domain that was disabled.'
complete -c hostess -f -A -n '__fish_seen_subcommand_from on' -a '(list_domains off)'
23 changes: 22 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
41 changes: 39 additions & 2 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hostess

import (
"flag"
"io/ioutil"
"os"
"testing"

Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions test-fixtures/ls_on_off
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# fe:23b3:890e:342e::ef dev.strawberry.pie.example.com
fe:23b3:890e:342e::ef chocolate.pie.example.com