pages-server/cmd/certs.go

73 lines
1.5 KiB
Go
Raw Normal View History

2021-12-03 01:12:51 +00:00
package cmd
import (
2021-12-03 03:15:48 +00:00
"fmt"
"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{
Name: "certs",
Usage: "manage certs manually",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "list all certificates in the database",
Action: listCerts,
},
{
Name: "remove",
Usage: "remove a certificate from the database",
Action: removeCert,
},
},
2021-12-03 01:12:51 +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
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 {
return err
2021-12-03 01:12:51 +00:00
}
if domain[0] == '.' {
fmt.Printf("*")
2021-12-03 01:12:51 +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)
if err := keyDatabase.Delete([]byte(domain)); err != nil {
return err
2021-12-03 01:12:51 +00:00
}
}
if err := keyDatabase.Close(); err != nil {
return err
2021-12-03 01:12:51 +00:00
}
return nil
}