Skip to content

Commit 39b8a57

Browse files
authored
Merge pull request #19 from ichikaway/issue-17-silent-mode
Issue #17 silent mode close #17
2 parents d604622 + 3f357ed commit 39b8a57

7 files changed

Lines changed: 67 additions & 15 deletions

File tree

NSchecker.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package main
22

33
import (
44
"flag"
5-
"fmt"
65
"nschecker/checker"
76
"nschecker/notification"
7+
"nschecker/printer"
88
"os"
99
)
1010

1111
var VERSION = "1.0.2"
1212

1313
func showError() {
14-
fmt.Printf("USAGE: go run NsCheck.go -type NS -domain domainName -expect 'ns records' \n")
15-
fmt.Printf(" or \n")
16-
fmt.Printf("USAGE (Deplicated): go run NsCheck.go type(NS or MX) 'domain' 'ns records' \n")
14+
printer.ErrorPrintf("USAGE: go run NsCheck.go -type NS -domain domainName -expect 'ns records' \n")
15+
printer.ErrorPrintf(" or \n")
16+
printer.ErrorPrintf("USAGE (Deplicated): go run NsCheck.go type(NS or MX) 'domain' 'ns records' \n")
1717
os.Exit(1)
1818
}
1919

@@ -22,26 +22,33 @@ func main() {
2222
var domainName string
2323
var nsListString string
2424

25-
fmt.Printf("=== NSchecker Version: %s === \n", VERSION)
2625
if len(os.Args) < 4 {
26+
showVersion()
2727
showError()
2828
}
2929

3030
if len(os.Args) != 4 {
3131
qType2 := flag.String("type", "NS", "type: NS or MX")
3232
domainName2 := flag.String("domain", "", "domain name: vaddy.net")
3333
nsListString2 := flag.String("expect", "", "ex: 'ns1.vaddy.net, ns2.vaddy.net'")
34+
mode := flag.String("mode", "", "optional: silent")
3435
flag.Parse()
3536

3637
qType = *qType2
3738
domainName = *domainName2
3839
nsListString = *nsListString2
40+
41+
if *mode == "silent" {
42+
printer.SilentModeOn()
43+
}
3944
} else {
4045
qType = os.Args[1]
4146
domainName = os.Args[2]
4247
nsListString = os.Args[3]
4348
}
4449

50+
showVersion()
51+
4552
if qType != "NS" && qType != "MX" {
4653
showError()
4754
}
@@ -56,8 +63,12 @@ func main() {
5663
}
5764

5865
func infoDump(domainName string, qType string, nsListString string) {
59-
fmt.Println(" - Domain: " + domainName)
60-
fmt.Println(" - Type: " + qType)
61-
fmt.Println(" - List: " + nsListString)
62-
fmt.Println("")
66+
printer.Printf(" - Domain: %s\n", domainName)
67+
printer.Printf(" - Type: %s\n", qType)
68+
printer.Printf(" - List: %s\n", nsListString)
69+
printer.Printf("")
70+
}
71+
72+
func showVersion() {
73+
printer.Printf("=== NSchecker Version: %s === \n", VERSION)
6374
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ go run NSchecker.go -type Type(NS/MX) -domain <your domain> -expect <NS records
4747
./nschecker-linux-64bit -type NS -domain "vaddy.net" -expect "ns-1151.awsdns-15.org. , ns-1908.awsdns-46.co.uk. , ns-457.awsdns-57.com. , ns-700.awsdns-23.net."
4848
```
4949

50+
## options
51+
### silent mode option
52+
`-mode silent` no message to stdout. only output error messages to stderr.
53+
```
54+
./nschecker-linux-64bit -type NS -domain "vaddy.net" -expect "ns-1151.awsdns-15.org" -mode silent
55+
```
56+
5057
## Results
5158
Return status code 0 if there is no problem.
5259
Return status code 1 or higher with error message if there there are problems.

checker/checker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net"
77
"nschecker/dnsmsg"
8+
"nschecker/printer"
89
"strings"
910
)
1011

@@ -37,7 +38,7 @@ func getNsRecords(domainName string) ([]string, error) {
3738

3839
func getMxRecords(domainName string) ([]string, error) {
3940
var ret []string
40-
fmt.Print(" ..lookup from local DNS cache server.\n\n")
41+
printer.Printf(" ..lookup from local DNS cache server.\n\n")
4142
nss, err := net.LookupMX(domainName)
4243
if err != nil {
4344
return nil, errors.New("MX Lookup Error.\n")
@@ -83,6 +84,6 @@ func CheckRecord(qType string, domainName string, expectString string) error {
8384
return errors.New("Error: not match DNS record value.\n" + message)
8485
}
8586
}
86-
fmt.Println("PASS. No problems.")
87+
printer.Printf("PASS. No problems.\n")
8788
return nil
8889
}

dnsmsg/lookup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package dnsmsg
22

33
import (
44
"errors"
5-
"fmt"
65
"net"
6+
"nschecker/printer"
77
"strings"
88
)
99

1010
func Lookup(domainName string) ([]string, error) {
1111
server, err := getAuthorityServerName(domainName)
1212
if err != nil {
13-
fmt.Print(" ..lookup from local DNS cache server.\n\n")
13+
printer.Printf(" ..lookup from local DNS cache server.\n\n")
1414
return lookupFromDnsCacheServer(domainName)
1515
}
16-
fmt.Printf(" ..lookup from DNS root server: %s \n\n", server)
16+
printer.Printf(" ..lookup from DNS root server: %s \n\n", server)
1717
return lookupFromDnsRoot(domainName, server)
1818
}
1919

notification/slack.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"nschecker/printer"
89
"os"
910
)
1011

@@ -73,7 +74,7 @@ func PostSlack(title, text string, domain string, qType string) {
7374
return
7475
}
7576

76-
fmt.Println("Sending warning message to slack.")
77+
printer.Printf("Sending warning message to slack.\n")
7778

7879
params := createSlackMessage(title, text, domain, qType)
7980

printer/default.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package printer
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var handlePrintf = defaultPrintf
8+
9+
func Printf(format string, a ...interface{}) {
10+
handlePrintf(format, a...)
11+
}
12+
13+
func SilentModeOn() {
14+
handlePrintf = printerNothing
15+
}
16+
17+
func defaultPrintf(format string, a ...interface{}) {
18+
fmt.Printf(format, a...)
19+
}
20+
21+
func printerNothing(format string, a ...interface{}) {
22+
}

printer/error.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package printer
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func ErrorPrintf(format string, a ...interface{}) {
9+
fmt.Fprintf(os.Stderr, format, a...)
10+
}

0 commit comments

Comments
 (0)