mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-18 10:29:43 +00:00
Fix cert removal command (#50)
The command was using parts from the old os.Args approach and parts from the cli package, and together they didn't work at all. This fixes that and makes the command `pages-server certs remove [domain...]`. Co-authored-by: Moritz Marquardt <git@momar.de> Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/50 Co-authored-by: Moritz Marquardt <momar@noreply.codeberg.org> Co-committed-by: Moritz Marquardt <momar@noreply.codeberg.org>
This commit is contained in:
parent
f5e613bfdb
commit
a5504acb0e
2 changed files with 53 additions and 26 deletions
77
cmd/certs.go
77
cmd/certs.go
|
@ -2,43 +2,70 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"github.com/akrylysov/pogreb"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"codeberg.org/codeberg/pages/server/database"
|
"codeberg.org/codeberg/pages/server/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Certs = &cli.Command{
|
var Certs = &cli.Command{
|
||||||
Name: "certs",
|
Name: "certs",
|
||||||
Usage: "manage certs manually",
|
Usage: "manage certs manually",
|
||||||
Action: certs,
|
Subcommands: []*cli.Command{
|
||||||
|
&cli.Command{
|
||||||
|
Name: "list",
|
||||||
|
Usage: "list all certificates in the database",
|
||||||
|
Action: listCerts,
|
||||||
|
},
|
||||||
|
&cli.Command{
|
||||||
|
Name: "remove",
|
||||||
|
Usage: "remove a certificate from the database",
|
||||||
|
Action: removeCert,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func certs(ctx *cli.Context) error {
|
func listCerts(ctx *cli.Context) error {
|
||||||
if ctx.Args().Len() >= 1 && ctx.Args().First() == "--remove-certificate" {
|
// TODO: make "key-database.pogreb" set via flag
|
||||||
if ctx.Args().Len() == 1 {
|
keyDatabase, err := database.New("key-database.pogreb")
|
||||||
println("--remove-certificate requires at least one domain as an argument")
|
if err != nil {
|
||||||
os.Exit(1)
|
return fmt.Errorf("could not create database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
domains := ctx.Args().Slice()[2:]
|
items := keyDatabase.Items()
|
||||||
|
for domain, _, err := items.Next(); err != pogreb.ErrIterationDone; domain, _, err = items.Next() {
|
||||||
// TODO: make "key-database.pogreb" set via flag
|
|
||||||
keyDatabase, err := database.New("key-database.pogreb")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not create database: %v", err)
|
return err
|
||||||
}
|
}
|
||||||
|
if domain[0] == '.' {
|
||||||
for _, domain := range domains {
|
fmt.Printf("*")
|
||||||
if err := keyDatabase.Delete([]byte(domain)); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if err := keyDatabase.Close(); err != nil {
|
fmt.Printf("%s\n", domain)
|
||||||
panic(err)
|
}
|
||||||
}
|
return nil
|
||||||
os.Exit(0)
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := keyDatabase.Close(); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue