-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added list command to cli, along with new hosts methods ListHostsByIP…
…, ListHostsByCIDR and ListAddressesByHost
- Loading branch information
Showing
6 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(listCmd) | ||
} | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: "list [BY_TYPE] [IP|CIDR] [IP|CIDR] [IP|CIDR] ...", | ||
Short: "List hostnames for a IP addresses or CIDRs", | ||
Long: `List hostnames for a IP addresses or CIDRs present in /etc/hosts`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := cmd.Help() | ||
if err != nil { | ||
fmt.Printf("Error: can not display help, reason: %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Please specify a sub-command such as \"ip\" or \"cidr\"") | ||
os.Exit(1) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
listCmd.AddCommand(listByCidrCmd) | ||
} | ||
|
||
var listByCidrCmd = &cobra.Command{ | ||
Use: "cidr [CIDR] [CIDR] [CIDR]...", | ||
Short: "List hosts for one or more CIDRs", | ||
Long: `List hosts for one or more CIDRs from /etc/hosts`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("the \"list cidr\" command requires at least one CIDR address") | ||
} | ||
|
||
if ok, cidr := validateCIDRs(args); !ok { | ||
return errors.New(fmt.Sprintf("\"%s\" is not a valid CIDR", cidr)) | ||
} | ||
|
||
return nil | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ListByCIDRs(args) | ||
}, | ||
} | ||
|
||
func ListByCIDRs(cidrs []string) { | ||
for _, cidr := range cidrs { | ||
ipHosts := etcHosts.ListHostsByCIDR(cidr) | ||
for _, ih := range ipHosts { | ||
fmt.Printf("%s %s %s\n", cidr, ih[0], ih[1]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var exactHost bool | ||
|
||
func init() { | ||
listCmd.AddCommand(listByHostsCmd) | ||
listByHostsCmd.PersistentFlags().BoolVarP(&exactHost, "exact", "e", false, "use -e for exact match") | ||
} | ||
|
||
var listByHostsCmd = &cobra.Command{ | ||
Use: "host [hostname] [hostname] [hostname]...", | ||
Short: "List IP for one or more hostnames", | ||
Long: `List IP for one or more hostnames from /etc/hosts`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("the \"list host\" command requires at least one hostname") | ||
} | ||
|
||
if ok, cidr := validateHostnames(args); !ok { | ||
return errors.New(fmt.Sprintf("\"%s\" is not a valid hostname", cidr)) | ||
} | ||
|
||
return nil | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ListByHostnames(args) | ||
}, | ||
} | ||
|
||
func ListByHostnames(hostnames []string) { | ||
for _, hn := range hostnames { | ||
addrHosts := etcHosts.ListAddressesByHost(hn, exactHost) | ||
for _, addrHost := range addrHosts { | ||
fmt.Printf("%s %s\n", addrHost[0], addrHost[1]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
listCmd.AddCommand(listByIpCmd) | ||
} | ||
|
||
var listByIpCmd = &cobra.Command{ | ||
Use: "ip [IP] [IP] [IP]...", | ||
Short: "List hosts for one or more IP addresses", | ||
Long: `List hosts for one or more IP addresses from /etc/hosts`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("the \"list ip\" command requires at least one IP address") | ||
} | ||
|
||
if ok, ip := validateIPAddresses(args); !ok { | ||
return errors.New(fmt.Sprintf("\"%s\" is not a valid ip address", ip)) | ||
} | ||
|
||
return nil | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ListByIPs(args) | ||
}, | ||
} | ||
|
||
func ListByIPs(ips []string) { | ||
for _, ip := range ips { | ||
hosts := etcHosts.ListHostsByIP(ip) | ||
for _, h := range hosts { | ||
fmt.Printf("%s %s\n", ip, h) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters