@@ -12,34 +12,96 @@ func Check() *cli.Command {
1212 return & cli.Command {
1313 Name : "check" ,
1414 Aliases : []string {"c" },
15- Usage : "Check if ip or host exists" ,
15+ Usage : "Check if ip or host exists. With IP and hosts: check if all hosts are mapped to IP " ,
1616 Action : check ,
17- ArgsUsage : "[IP|HOST]" ,
17+ ArgsUsage : "[IP|HOST] or [IP] [HOST] ([HOST]...)" ,
18+ Flags : []cli.Flag {
19+ & cli.BoolFlag {
20+ Name : "quiet" ,
21+ Aliases : []string {"q" },
22+ Usage : "Suppress output, only return exit code" ,
23+ },
24+ & cli.BoolFlag {
25+ Name : "any" ,
26+ Usage : "With multiple hosts: succeed if ANY host matches (default: ALL must match)" ,
27+ },
28+ },
1829 }
1930}
31+
2032func check (c * cli.Context ) error {
2133 if c .Args ().Len () < 1 {
2234 logrus .Infof ("No input, pass an ip address or hostname to check." )
2335 return nil
2436 }
2537
26- hf , err := loadHostsfile (c , true )
38+ hf , err := loadHostsfile (c , true ) // readOnly=true, no sudo needed
2739 if err != nil {
2840 return err
2941 }
30- input := c .Args ().First ()
3142
32- if net .ParseIP (input ) != nil {
33- if hf .HasIP (input ) {
34- logrus .Infof ("%s exists in hosts file\n " , input )
43+ args := c .Args ().Slice ()
44+ firstArg := args [0 ]
45+ quiet := c .Bool ("quiet" )
46+
47+ // Single argument: check if IP or hostname exists (backward compatible)
48+ if c .Args ().Len () == 1 {
49+ if net .ParseIP (firstArg ) != nil {
50+ if hf .HasIP (firstArg ) {
51+ if ! quiet {
52+ logrus .Infof ("%s exists in hosts file\n " , firstArg )
53+ }
54+ return nil
55+ }
56+ }
57+
58+ if hf .HasHostname (firstArg ) {
59+ if ! quiet {
60+ logrus .Infof ("%s exists in hosts file\n " , firstArg )
61+ }
62+ return nil
63+ }
64+
65+ return cli .Exit (fmt .Sprintf ("%s does not match anything in the hosts file" , firstArg ), 1 )
66+ }
67+
68+ // Multiple arguments: first must be IP, rest are hostnames
69+ if net .ParseIP (firstArg ) == nil {
70+ return cli .Exit (fmt .Sprintf ("%s is not a valid IP address. When checking multiple hosts, first argument must be an IP." , firstArg ), 2 )
71+ }
72+
73+ ip := firstArg
74+ hosts := args [1 :]
75+
76+ if c .Bool ("any" ) {
77+ // Check if ANY host is mapped to IP
78+ if hf .HasAny (ip , hosts ... ) {
79+ if ! quiet {
80+ logrus .Infof ("At least one host is mapped to %s\n " , ip )
81+ }
3582 return nil
3683 }
84+ return cli .Exit (fmt .Sprintf ("None of the specified hosts are mapped to %s" , ip ), 1 )
3785 }
3886
39- if hf .HasHostname (input ) {
40- logrus .Infof ("%s exists in hosts file\n " , input )
87+ // Default: Check if ALL hosts are mapped to IP
88+ if hf .HasAll (ip , hosts ... ) {
89+ if ! quiet {
90+ logrus .Infof ("All specified hosts are mapped to %s\n " , ip )
91+ }
4192 return nil
4293 }
4394
44- return cli .Exit (fmt .Sprintf ("%s does not match anything in the hosts file" , input ), 1 )
95+ // Report which hosts are missing
96+ if ! quiet {
97+ missing := []string {}
98+ for _ , host := range hosts {
99+ if ! hf .Has (ip , host ) {
100+ missing = append (missing , host )
101+ }
102+ }
103+ return cli .Exit (fmt .Sprintf ("Missing hosts for %s: %v" , ip , missing ), 1 )
104+ }
105+
106+ return cli .Exit ("" , 1 )
45107}
0 commit comments