main: switch from 'flag' to 'github.com/urfave/cli'
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
f79c74c16c
commit
428c8b35b2
2 changed files with 60 additions and 17 deletions
6
go.mod
6
go.mod
|
@ -3,6 +3,8 @@ module git.batts.cloud/vbatts/too-soon
|
|||
go 1.22.5
|
||||
|
||||
require (
|
||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/urfave/cli/v3 v3.0.0-beta1
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
|
|
69
main.go
69
main.go
|
@ -1,27 +1,66 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"flag"
|
||||
"net/mail"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
cli "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var retCode int = 0
|
||||
fDays := flag.Int("d", 20, "number of days to alert on")
|
||||
fDebug := flag.Bool("D", false, "debug mode")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *fDebug {
|
||||
cmd := &cli.Command{
|
||||
Name: "too-soon",
|
||||
Usage: "check if something expires too soon",
|
||||
Version: "0.1",
|
||||
Authors: []any{
|
||||
&mail.Address{Name: "Vincent Batts", Address: "vbatts@hashbangbash.com"},
|
||||
},
|
||||
DefaultCommand: "pem",
|
||||
Flags: []cli.Flag{
|
||||
&cli.IntFlag{
|
||||
Name: "days",
|
||||
Value: 20,
|
||||
Usage: "days within range to alert about",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Aliases: []string{"D"},
|
||||
Value: false,
|
||||
Usage: "output debug verbose info",
|
||||
},
|
||||
},
|
||||
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
||||
if cmd.Bool("debug") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
return ctx, nil
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
&cli.Command{
|
||||
Name: "pem",
|
||||
Action: fPEMCheck,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := cmd.Run(context.Background(), os.Args); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func fPEMCheck(ctx context.Context, cmd *cli.Command) error {
|
||||
retCode := 0
|
||||
for i := 0; i <= cmd.Args().Len(); i++ {
|
||||
file := cmd.Args().Get(i)
|
||||
if file == "" {
|
||||
break
|
||||
}
|
||||
|
||||
for _, file := range flag.Args() {
|
||||
var certs []*x509.Certificate
|
||||
|
||||
buf, err := os.ReadFile(file)
|
||||
|
@ -54,20 +93,22 @@ func main() {
|
|||
continue
|
||||
}
|
||||
|
||||
hours := time.Duration(*fDays * -24)
|
||||
hours := time.Duration(cmd.Int("days") * -24)
|
||||
alertTime := cert.NotAfter.Add(hours * time.Hour)
|
||||
today := time.Now()
|
||||
if today.After(alertTime) {
|
||||
log.Warnf("%q : TIME TO RENEW CERTIFICATE (expires in less than %d days)", file, *fDays)
|
||||
log.Warnf("%q : TIME TO RENEW CERTIFICATE (expires in less than %d days)", file, cmd.Int("days"))
|
||||
log.Infof("%q : %v", file, cert.NotAfter)
|
||||
log.Infof("%q : %v", file, cert.DNSNames)
|
||||
retCode += 1
|
||||
retCode++
|
||||
} else {
|
||||
log.Debugf("%q : %v", file, cert.NotAfter)
|
||||
log.Debugf("%q : %v", file, cert.DNSNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(retCode)
|
||||
if retCode != 0 {
|
||||
return cli.Exit("certificates need to be renewed", retCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue