main: switch from 'flag' to 'github.com/urfave/cli'

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2025-02-17 15:02:17 -05:00
parent f79c74c16c
commit 428c8b35b2
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED
2 changed files with 60 additions and 17 deletions

6
go.mod
View file

@ -3,6 +3,8 @@ module git.batts.cloud/vbatts/too-soon
go 1.22.5 go 1.22.5
require ( require (
github.com/sirupsen/logrus v1.9.3 // indirect github.com/sirupsen/logrus v1.9.3
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect github.com/urfave/cli/v3 v3.0.0-beta1
) )
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

71
main.go
View file

@ -1,27 +1,66 @@
package main package main
import ( import (
"context"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"flag" "net/mail"
"os" "os"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
cli "github.com/urfave/cli/v3"
) )
func main() { func main() {
var retCode int = 0 cmd := &cli.Command{
fDays := flag.Int("d", 20, "number of days to alert on") Name: "too-soon",
fDebug := flag.Bool("D", false, "debug mode") Usage: "check if something expires too soon",
Version: "0.1",
flag.Parse() Authors: []any{
&mail.Address{Name: "Vincent Batts", Address: "vbatts@hashbangbash.com"},
if *fDebug { },
log.SetLevel(log.DebugLevel) 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,
},
},
} }
for _, file := range flag.Args() { 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
}
var certs []*x509.Certificate var certs []*x509.Certificate
buf, err := os.ReadFile(file) buf, err := os.ReadFile(file)
@ -54,20 +93,22 @@ func main() {
continue continue
} }
hours := time.Duration(*fDays * -24) hours := time.Duration(cmd.Int("days") * -24)
alertTime := cert.NotAfter.Add(hours * time.Hour) alertTime := cert.NotAfter.Add(hours * time.Hour)
today := time.Now() today := time.Now()
if today.After(alertTime) { 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.NotAfter)
log.Infof("%q : %v", file, cert.DNSNames) log.Infof("%q : %v", file, cert.DNSNames)
retCode += 1 retCode++
} else { } else {
log.Debugf("%q : %v", file, cert.NotAfter) log.Debugf("%q : %v", file, cert.NotAfter)
log.Debugf("%q : %v", file, cert.DNSNames) log.Debugf("%q : %v", file, cert.DNSNames)
} }
} }
} }
if retCode != 0 {
os.Exit(retCode) return cli.Exit("certificates need to be renewed", retCode)
}
return nil
} }