use internal check function for error checking

This commit is contained in:
Antoine 2020-11-09 03:08:09 +01:00
parent 74bdbb38ac
commit 967b604bba
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
3 changed files with 19 additions and 14 deletions

View File

@ -9,6 +9,8 @@ import (
"os" "os"
"strings" "strings"
check "antoine-roux.ml/projects/go/dns-tools/internal"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -24,26 +26,20 @@ func main() {
var err error var err error
if *file != "" { if *file != "" {
if *file == "-" { if *file == "-" {
*file = "/dev/stdin" *file = os.Stdin.Name()
} }
body, err = ioutil.ReadFile(*file) body, err = ioutil.ReadFile(*file)
if err != nil { check.Check(err)
log.Fatalln(err)
}
msg := &dns.Msg{} msg := &dns.Msg{}
err = msg.Unpack(body) err = msg.Unpack(body)
if err != nil { check.Check(err)
log.Fatalln(err)
}
log.Println(msg) log.Println(msg)
} else if *domainToResolve != "" { } else if *domainToResolve != "" {
if *domainToResolve == "-" { if *domainToResolve == "-" {
readDomainToResolve, err := ioutil.ReadFile("/dev/stdin") readDomainToResolve, err := ioutil.ReadFile(os.Stdin.Name())
if err != nil { check.Check(err)
log.Fatalln(err)
}
*domainToResolve = strings.TrimSuffix(string(readDomainToResolve), "\n") *domainToResolve = strings.TrimSuffix(string(readDomainToResolve), "\n")
} }
msg := &dns.Msg{ msg := &dns.Msg{
@ -56,9 +52,7 @@ func main() {
}, },
} }
b, err := msg.Pack() b, err := msg.Pack()
if err != nil { check.Check(err)
log.Fatalln(err)
}
binary.Write(os.Stdout, binary.LittleEndian, b) binary.Write(os.Stdout, binary.LittleEndian, b)
} else { } else {
log.Println(errors.New("Too few argument pass")) log.Println(errors.New("Too few argument pass"))

2
go.mod
View File

@ -3,3 +3,5 @@ module antoine-roux.ml/projects/go/dns-tools
go 1.15 go 1.15
require github.com/miekg/dns v1.1.35 require github.com/miekg/dns v1.1.35
replace antoine-roux.ml/projects/go/dns-tools/internal => ./internal

9
internal/check.go Normal file
View File

@ -0,0 +1,9 @@
package check
import "log"
func Check(err error) {
if err != nil {
log.Fatalln(err)
}
}