Skip to content

Commit

Permalink
use kong for cli flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtazz authored Feb 26, 2022
1 parent 52003bc commit 137b4eb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
64 changes: 32 additions & 32 deletions certcal.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package main

import (
"fmt"
"github.com/alecthomas/kong"
"github.com/mrtazz/certcal/handler"
"github.com/mrtazz/certcal/hosts"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strings"
"time"
)

var (
version = ""
goversion = ""
)

// CLI defines the command line arguments
var CLI struct {
Serve struct {
Hosts []string `required:"" help:"hosts to check certs for." env:"CERTCAL_HOSTS"`
Interval string `required:"" help:"interval in which to check certs" env:"CERTCAL_INTERVAL" default:"24h"`
Port int `help:"port for the server to listen on" env:"PORT" default:"3000"`
} `cmd:"" help:"run the server."`
}

func main() {

log.SetFormatter(&log.TextFormatter{
Expand All @@ -19,42 +33,28 @@ func main() {
"package": "main",
})

checkHosts := os.Getenv("CERTCAL_HOSTS")
if checkHosts == "" {
logger.Error("missing env var CERTCAL_HOSTS")
os.Exit(1)
}

var duration time.Duration
durationString := os.Getenv("CERTCAL_INTERVAL")
switch durationString {
case "":
logger.Error("missing env var CERTCAL_INTERVAL, defaulting to 1 day")
duration = 24 * time.Hour
default:
var err error
duration, err = time.ParseDuration(durationString)
ctx := kong.Parse(&CLI)
switch ctx.Command() {
case "serve":
duration, err := time.ParseDuration(CLI.Serve.Interval)
if err != nil {
logger.Error("unable to parse CERTCAL_INTERVAL, defaulting to 1 day")
duration = 24 * time.Hour
}
}

var port string
if port = os.Getenv("PORT"); port == "" {
port = "3000"
}
hosts.AddHosts(CLI.Serve.Hosts)
hosts.UpdateEvery(duration)

hosts.AddHosts(strings.Split(checkHosts, ","))
hosts.UpdateEvery(duration)
address := fmt.Sprintf(":%d", CLI.Serve.Port)

address := ":" + port
logger.WithFields(log.Fields{
"address": address,
}).Info("starting web server")
http.HandleFunc("/hosts", handler.Handler)
//http.HandleFunc("/metrics", headers)
http.ListenAndServe(address, nil)

logger.WithFields(log.Fields{
"address": address,
}).Info("starting web server")
http.HandleFunc("/hosts", handler.Handler)
//http.HandleFunc("/metrics", headers)
http.ListenAndServe(address, nil)
default:
logger.Error("Unknown command: " + ctx.Command())
}

}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
)

require (
github.com/alecthomas/kong v0.4.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
github.com/alecthomas/kong v0.4.1 h1:0sFnMts+ijOiFuSHsMB9MlDi3NGINBkx9KIw1/gcuDw=
github.com/alecthomas/kong v0.4.1/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
Expand All @@ -15,3 +20,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 137b4eb

Please sign in to comment.