-
Notifications
You must be signed in to change notification settings - Fork 8
/
check.go
45 lines (38 loc) · 849 Bytes
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cmd
import (
"fmt"
"net"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func Check() *cli.Command {
return &cli.Command{
Name: "check",
Aliases: []string{"c"},
Usage: "Check if ip or host exists",
Action: check,
ArgsUsage: "[IP|HOST]",
}
}
func check(c *cli.Context) error {
if c.Args().Len() < 1 {
logrus.Infof("No input, pass an ip address or hostname to check.")
return nil
}
hf, err := loadHostsfile(c, true)
if err != nil {
return err
}
input := c.Args().First()
if net.ParseIP(input) != nil {
if hf.HasIP(input) {
logrus.Infof("%s exists in hosts file\n", input)
return nil
}
}
if hf.HasHostname(input) {
logrus.Infof("%s exists in hosts file\n", input)
return nil
}
return cli.Exit(fmt.Sprintf("%s does not match anything in the hosts file", input), 1)
}