feat: move remaining go scripts to main executable

This commit is contained in:
Sam 2023-05-11 01:13:32 +02:00
parent 4e056632c8
commit 7c7f948ad6
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
3 changed files with 59 additions and 5 deletions

11
main.go
View file

@ -8,6 +8,8 @@ import (
"codeberg.org/u1f320/pronouns.cc/backend/exporter" "codeberg.org/u1f320/pronouns.cc/backend/exporter"
"codeberg.org/u1f320/pronouns.cc/backend/server" "codeberg.org/u1f320/pronouns.cc/backend/server"
"codeberg.org/u1f320/pronouns.cc/scripts/cleandb" "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/migrate"
"codeberg.org/u1f320/pronouns.cc/scripts/seeddb" "codeberg.org/u1f320/pronouns.cc/scripts/seeddb"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -30,6 +32,15 @@ var app = &cli.App{
cleandb.Command, cleandb.Command,
}, },
}, },
{
Name: "generate",
Aliases: []string{"gen"},
Usage: "Generate various strings",
Subcommands: []*cli.Command{
genid.Command,
genkey.Command,
},
},
}, },
} }

View file

@ -1,11 +1,45 @@
package main package genid
import ( import (
"fmt" "fmt"
"time"
"github.com/rs/xid" "github.com/rs/xid"
"github.com/urfave/cli/v2"
) )
func main() { var Command = &cli.Command{
fmt.Println(xid.New()) 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
} }

View file

@ -1,12 +1,20 @@
package main package genkey
import ( import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"fmt" "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) b := make([]byte, 64)
_, err := rand.Read(b) _, err := rand.Read(b)
@ -17,4 +25,5 @@ func main() {
s := base64.URLEncoding.EncodeToString(b) s := base64.URLEncoding.EncodeToString(b)
fmt.Println(s) fmt.Println(s)
return nil
} }