Skip to content

Commit

Permalink
Merge pull request #19 from ichikaway/issue-17-silent-mode
Browse files Browse the repository at this point in the history
Issue #17 silent mode
close #17
  • Loading branch information
ichikaway authored May 22, 2021
2 parents d604622 + 3f357ed commit 39b8a57
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 15 deletions.
29 changes: 20 additions & 9 deletions NSchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package main

import (
"flag"
"fmt"
"nschecker/checker"
"nschecker/notification"
"nschecker/printer"
"os"
)

var VERSION = "1.0.2"

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

Expand All @@ -22,26 +22,33 @@ func main() {
var domainName string
var nsListString string

fmt.Printf("=== NSchecker Version: %s === \n", VERSION)
if len(os.Args) < 4 {
showVersion()
showError()
}

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

qType = *qType2
domainName = *domainName2
nsListString = *nsListString2

if *mode == "silent" {
printer.SilentModeOn()
}
} else {
qType = os.Args[1]
domainName = os.Args[2]
nsListString = os.Args[3]
}

showVersion()

if qType != "NS" && qType != "MX" {
showError()
}
Expand All @@ -56,8 +63,12 @@ func main() {
}

func infoDump(domainName string, qType string, nsListString string) {
fmt.Println(" - Domain: " + domainName)
fmt.Println(" - Type: " + qType)
fmt.Println(" - List: " + nsListString)
fmt.Println("")
printer.Printf(" - Domain: %s\n", domainName)
printer.Printf(" - Type: %s\n", qType)
printer.Printf(" - List: %s\n", nsListString)
printer.Printf("")
}

func showVersion() {
printer.Printf("=== NSchecker Version: %s === \n", VERSION)
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ go run NSchecker.go -type Type(NS/MX) -domain <your domain> -expect <NS records
./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."
```

## options
### silent mode option
`-mode silent` no message to stdout. only output error messages to stderr.
```
./nschecker-linux-64bit -type NS -domain "vaddy.net" -expect "ns-1151.awsdns-15.org" -mode silent
```

## Results
Return status code 0 if there is no problem.
Return status code 1 or higher with error message if there there are problems.
Expand Down
5 changes: 3 additions & 2 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"nschecker/dnsmsg"
"nschecker/printer"
"strings"
)

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

func getMxRecords(domainName string) ([]string, error) {
var ret []string
fmt.Print(" ..lookup from local DNS cache server.\n\n")
printer.Printf(" ..lookup from local DNS cache server.\n\n")
nss, err := net.LookupMX(domainName)
if err != nil {
return nil, errors.New("MX Lookup Error.\n")
Expand Down Expand Up @@ -83,6 +84,6 @@ func CheckRecord(qType string, domainName string, expectString string) error {
return errors.New("Error: not match DNS record value.\n" + message)
}
}
fmt.Println("PASS. No problems.")
printer.Printf("PASS. No problems.\n")
return nil
}
6 changes: 3 additions & 3 deletions dnsmsg/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package dnsmsg

import (
"errors"
"fmt"
"net"
"nschecker/printer"
"strings"
)

func Lookup(domainName string) ([]string, error) {
server, err := getAuthorityServerName(domainName)
if err != nil {
fmt.Print(" ..lookup from local DNS cache server.\n\n")
printer.Printf(" ..lookup from local DNS cache server.\n\n")
return lookupFromDnsCacheServer(domainName)
}
fmt.Printf(" ..lookup from DNS root server: %s \n\n", server)
printer.Printf(" ..lookup from DNS root server: %s \n\n", server)
return lookupFromDnsRoot(domainName, server)
}

Expand Down
3 changes: 2 additions & 1 deletion notification/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"nschecker/printer"
"os"
)

Expand Down Expand Up @@ -73,7 +74,7 @@ func PostSlack(title, text string, domain string, qType string) {
return
}

fmt.Println("Sending warning message to slack.")
printer.Printf("Sending warning message to slack.\n")

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

Expand Down
22 changes: 22 additions & 0 deletions printer/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package printer

import (
"fmt"
)

var handlePrintf = defaultPrintf

func Printf(format string, a ...interface{}) {
handlePrintf(format, a...)
}

func SilentModeOn() {
handlePrintf = printerNothing
}

func defaultPrintf(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

func printerNothing(format string, a ...interface{}) {
}
10 changes: 10 additions & 0 deletions printer/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package printer

import (
"fmt"
"os"
)

func ErrorPrintf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
}

0 comments on commit 39b8a57

Please sign in to comment.