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