From 428c8b35b2c07031b3bb111fb787b3d9cfeeff4d Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 17 Feb 2025 15:02:17 -0500 Subject: [PATCH] main: switch from 'flag' to 'github.com/urfave/cli' Signed-off-by: Vincent Batts --- go.mod | 6 +++-- main.go | 71 +++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index faecf5e..c6f7bce 100644 --- a/go.mod +++ b/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 diff --git a/main.go b/main.go index be879e4..21b9058 100644 --- a/main.go +++ b/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 { - log.SetLevel(log.DebugLevel) + 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, + }, + }, } - 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 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 }