From 7c7f948ad64c6f7611c032d998f6b07bb6f01d1e Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 11 May 2023 01:13:32 +0200 Subject: [PATCH] feat: move remaining go scripts to main executable --- main.go | 11 +++++++++++ scripts/genid/main.go | 40 +++++++++++++++++++++++++++++++++++++--- scripts/genkey/main.go | 13 +++++++++++-- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 40b4534..14e9b03 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "codeberg.org/u1f320/pronouns.cc/backend/exporter" "codeberg.org/u1f320/pronouns.cc/backend/server" "codeberg.org/u1f320/pronouns.cc/scripts/cleandb" + "codeberg.org/u1f320/pronouns.cc/scripts/genid" + "codeberg.org/u1f320/pronouns.cc/scripts/genkey" "codeberg.org/u1f320/pronouns.cc/scripts/migrate" "codeberg.org/u1f320/pronouns.cc/scripts/seeddb" "github.com/urfave/cli/v2" @@ -30,6 +32,15 @@ var app = &cli.App{ cleandb.Command, }, }, + { + Name: "generate", + Aliases: []string{"gen"}, + Usage: "Generate various strings", + Subcommands: []*cli.Command{ + genid.Command, + genkey.Command, + }, + }, }, } diff --git a/scripts/genid/main.go b/scripts/genid/main.go index 08c6224..9baf90b 100644 --- a/scripts/genid/main.go +++ b/scripts/genid/main.go @@ -1,11 +1,45 @@ -package main +package genid import ( "fmt" + "time" "github.com/rs/xid" + "github.com/urfave/cli/v2" ) -func main() { - fmt.Println(xid.New()) +var Command = &cli.Command{ + Name: "id", + Usage: "Generate a time-based ID", + Flags: []cli.Flag{ + &cli.TimestampFlag{ + Name: "timestamp", + Aliases: []string{"T"}, + Usage: "The timestamp to generate an ID for (format: 2006-01-02T15:04:05)", + Layout: "2006-01-02T15:04:05", + }, + &cli.UintFlag{ + Name: "days-ago", + Aliases: []string{"D"}, + Usage: "The number of days ago to generate an ID for", + Value: 0, + }, + }, + Action: run, +} + +func run(c *cli.Context) error { + if t := c.Timestamp("timestamp"); t != nil { + fmt.Printf("ID for %v: %v\n", t.Format(time.RFC1123), xid.NewWithTime(*t)) + return nil + } + if daysAgo := c.Uint("days-ago"); daysAgo != 0 { + t := time.Now().Add(time.Duration(-daysAgo) * 24 * time.Hour) + + fmt.Printf("ID for %v days ago: %v\n", daysAgo, xid.NewWithTime(t)) + return nil + } + + fmt.Printf("ID for now: %v\n", xid.New()) + return nil } diff --git a/scripts/genkey/main.go b/scripts/genkey/main.go index 93c5ce6..433da90 100644 --- a/scripts/genkey/main.go +++ b/scripts/genkey/main.go @@ -1,12 +1,20 @@ -package main +package genkey import ( "crypto/rand" "encoding/base64" "fmt" + + "github.com/urfave/cli/v2" ) -func main() { +var Command = &cli.Command{ + Name: "key", + Usage: "Generate a secure 64-byte base 64 key", + Action: run, +} + +func run(c *cli.Context) error { b := make([]byte, 64) _, err := rand.Read(b) @@ -17,4 +25,5 @@ func main() { s := base64.URLEncoding.EncodeToString(b) fmt.Println(s) + return nil }