add basic cli flags

This commit is contained in:
Nils 2021-12-24 15:32:27 +01:00
parent 8b1b28b274
commit dcb8ad72e3
Signed by: byreqz
GPG Key ID: 396A62D7D436749E

16
main.go
View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"net/http" "net/http"
"net" "net"
"flag"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -13,13 +14,20 @@ import (
var logstdout = log.New() var logstdout = log.New()
var logfile = log.New() var logfile = log.New()
var logfilepath string
var listenport int
func init() { func init() {
flag.StringVar(&logfilepath, "logfilepath", "probehost2.log", "sets the output file for the log")
flag.IntVar(&listenport, "port", 8000, "sets the port to listen on")
flag.Parse()
logstdout.SetFormatter(&log.TextFormatter{ logstdout.SetFormatter(&log.TextFormatter{
FullTimestamp: true}) FullTimestamp: true})
logstdout.SetOutput(os.Stdout) logstdout.SetOutput(os.Stdout)
logstdout.SetLevel(log.InfoLevel) logstdout.SetLevel(log.InfoLevel)
logpath, err := os.OpenFile("probehost2.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660) logpath, err := os.OpenFile(logfilepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660)
if err != nil { if err != nil {
logstdout.Fatal("Failed to initialize the logfile: ", err.Error()) logstdout.Fatal("Failed to initialize the logfile: ", err.Error())
} }
@ -106,7 +114,7 @@ func main() {
http.HandleFunc("/ping/", ping) http.HandleFunc("/ping/", ping)
http.HandleFunc("/mtr/", mtr) http.HandleFunc("/mtr/", mtr)
http.HandleFunc("/", showhelp) http.HandleFunc("/", showhelp)
logstdout.Info("Serving on :8000") logstdout.Info("Serving on :", listenport)
logfile.Info("Serving on :8000") logfile.Info("Serving on :", listenport)
http.ListenAndServe(":8000", nil) http.ListenAndServe(fmt.Sprint(":", listenport), nil)
} }