2024-02-15 19:08:29 +03:00
package cli
2021-12-03 04:12:51 +03:00
import (
2021-12-03 06:15:48 +03:00
"fmt"
2023-02-10 06:00:14 +03:00
"time"
2022-03-27 22:54:06 +03:00
2021-12-03 04:12:51 +03:00
"github.com/urfave/cli/v2"
)
var Certs = & cli . Command {
2022-03-21 01:18:00 +03:00
Name : "certs" ,
Usage : "manage certs manually" ,
Subcommands : [ ] * cli . Command {
2022-03-27 22:54:06 +03:00
{
2022-03-21 01:18:00 +03:00
Name : "list" ,
Usage : "list all certificates in the database" ,
Action : listCerts ,
} ,
2022-03-27 22:54:06 +03:00
{
2022-03-21 01:18:00 +03:00
Name : "remove" ,
Usage : "remove a certificate from the database" ,
Action : removeCert ,
} ,
} ,
2023-02-11 05:04:57 +03:00
Flags : CertStorageFlags ,
2023-02-10 06:00:14 +03:00
}
func listCerts ( ctx * cli . Context ) error {
2024-02-15 19:08:29 +03:00
certDB , closeFn , err := OpenCertDB ( ctx )
2023-02-10 06:00:14 +03:00
if err != nil {
return err
}
defer closeFn ( )
items , err := certDB . Items ( 0 , 0 )
if err != nil {
return err
}
fmt . Printf ( "Domain\tValidTill\n\n" )
for _ , cert := range items {
fmt . Printf ( "%s\t%s\n" ,
cert . Domain ,
time . Unix ( cert . ValidTill , 0 ) . Format ( time . RFC3339 ) )
2022-03-21 01:18:00 +03:00
}
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 ( )
2024-02-15 19:08:29 +03:00
certDB , closeFn , err := OpenCertDB ( ctx )
2022-03-21 01:18:00 +03:00
if err != nil {
2023-02-10 06:00:14 +03:00
return err
2022-03-21 01:18:00 +03:00
}
2023-02-10 06:00:14 +03:00
defer closeFn ( )
2022-03-21 01:18:00 +03:00
for _ , domain := range domains {
fmt . Printf ( "Removing domain %s from the database...\n" , domain )
2023-02-10 06:00:14 +03:00
if err := certDB . Delete ( domain ) ; err != nil {
2022-03-21 01:18:00 +03:00
return err
2021-12-03 04:12:51 +03:00
}
2022-03-21 01:18:00 +03:00
}
2021-12-03 04:12:51 +03:00
return nil
}