2021-12-03 01:12:51 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-12-03 03:15:48 +00:00
|
|
|
"fmt"
|
2022-03-27 19:54:06 +00:00
|
|
|
|
2022-03-20 22:18:00 +00:00
|
|
|
"github.com/akrylysov/pogreb"
|
2021-12-03 01:12:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2021-12-03 03:15:48 +00:00
|
|
|
"codeberg.org/codeberg/pages/server/database"
|
2021-12-03 01:12:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Certs = &cli.Command{
|
2022-03-20 22:18:00 +00:00
|
|
|
Name: "certs",
|
|
|
|
Usage: "manage certs manually",
|
|
|
|
Subcommands: []*cli.Command{
|
2022-03-27 19:54:06 +00:00
|
|
|
{
|
2022-03-20 22:18:00 +00:00
|
|
|
Name: "list",
|
|
|
|
Usage: "list all certificates in the database",
|
|
|
|
Action: listCerts,
|
|
|
|
},
|
2022-03-27 19:54:06 +00:00
|
|
|
{
|
2022-03-20 22:18:00 +00:00
|
|
|
Name: "remove",
|
|
|
|
Usage: "remove a certificate from the database",
|
|
|
|
Action: removeCert,
|
|
|
|
},
|
|
|
|
},
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 22:18:00 +00:00
|
|
|
func listCerts(ctx *cli.Context) error {
|
|
|
|
// TODO: make "key-database.pogreb" set via flag
|
|
|
|
keyDatabase, err := database.New("key-database.pogreb")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create database: %v", err)
|
|
|
|
}
|
2021-12-03 01:12:51 +00:00
|
|
|
|
2022-03-20 22:18:00 +00:00
|
|
|
items := keyDatabase.Items()
|
|
|
|
for domain, _, err := items.Next(); err != pogreb.ErrIterationDone; domain, _, err = items.Next() {
|
2021-12-03 03:15:48 +00:00
|
|
|
if err != nil {
|
2022-03-20 22:18:00 +00:00
|
|
|
return err
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
2022-03-20 22:18:00 +00:00
|
|
|
if domain[0] == '.' {
|
|
|
|
fmt.Printf("*")
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
2022-03-20 22:18:00 +00:00
|
|
|
fmt.Printf("%s\n", domain)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeCert(ctx *cli.Context) error {
|
|
|
|
if ctx.Args().Len() < 1 {
|
|
|
|
return fmt.Errorf("'certs remove' requires at least one domain as an argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
domains := ctx.Args().Slice()
|
|
|
|
|
|
|
|
// TODO: make "key-database.pogreb" set via flag
|
|
|
|
keyDatabase, err := database.New("key-database.pogreb")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range domains {
|
|
|
|
fmt.Printf("Removing domain %s from the database...\n", domain)
|
2022-06-11 21:02:06 +00:00
|
|
|
if err := keyDatabase.Delete(domain); err != nil {
|
2022-03-20 22:18:00 +00:00
|
|
|
return err
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
2022-03-20 22:18:00 +00:00
|
|
|
}
|
|
|
|
if err := keyDatabase.Close(); err != nil {
|
|
|
|
return err
|
2021-12-03 01:12:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|