From a5504acb0e036b67bf8880fc70feff9adb63681e Mon Sep 17 00:00:00 2001 From: Moritz Marquardt Date: Sun, 20 Mar 2022 23:18:00 +0100 Subject: [PATCH] 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 Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/50 Co-authored-by: Moritz Marquardt Co-committed-by: Moritz Marquardt --- cmd/certs.go | 77 +++++++++++++++++++++++++++++++++++----------------- cmd/flags.go | 2 +- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/cmd/certs.go b/cmd/certs.go index 6603e55..071234b 100644 --- a/cmd/certs.go +++ b/cmd/certs.go @@ -2,43 +2,70 @@ package cmd import ( "fmt" - "os" - + "github.com/akrylysov/pogreb" "github.com/urfave/cli/v2" "codeberg.org/codeberg/pages/server/database" ) var Certs = &cli.Command{ - Name: "certs", - Usage: "manage certs manually", - Action: certs, + Name: "certs", + Usage: "manage certs manually", + 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 { - if ctx.Args().Len() >= 1 && ctx.Args().First() == "--remove-certificate" { - if ctx.Args().Len() == 1 { - println("--remove-certificate requires at least one domain as an argument") - os.Exit(1) - } +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) + } - domains := ctx.Args().Slice()[2:] - - // TODO: make "key-database.pogreb" set via flag - keyDatabase, err := database.New("key-database.pogreb") + items := keyDatabase.Items() + for domain, _, err := items.Next(); err != pogreb.ErrIterationDone; domain, _, err = items.Next() { if err != nil { - return fmt.Errorf("could not create database: %v", err) + return err } - - for _, domain := range domains { - if err := keyDatabase.Delete([]byte(domain)); err != nil { - panic(err) - } + if domain[0] == '.' { + fmt.Printf("*") } - if err := keyDatabase.Close(); err != nil { - panic(err) - } - os.Exit(0) + 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 + } + } + if err := keyDatabase.Close(); err != nil { + return err } return nil } diff --git a/cmd/flags.go b/cmd/flags.go index e1838c2..78040be 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -10,7 +10,7 @@ var ServeFlags = []cli.Flag{ // TODO: Usage EnvVars: []string{"DEBUG"}, }, - + // MainDomainSuffix specifies the main domain (starting with a dot) for which subdomains shall be served as static // pages, or used for comparison in CNAME lookups. Static pages can be accessed through // https://{owner}.{MainDomain}[/{repo}], with repo defaulting to "pages".