69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.nevets.tech/Steven/certman/app"
|
|
"git.nevets.tech/Steven/certman/client"
|
|
"git.nevets.tech/Steven/certman/common"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
renewCertSubCmd = &cobra.Command{
|
|
Use: "renew",
|
|
Short: "Renews a domains certificate",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return renewCert(args[0])
|
|
},
|
|
}
|
|
|
|
updateCertLinkSubCmd = &cobra.Command{
|
|
Use: "update-link",
|
|
Short: "Update linked certificates",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return updateLinks(args[0])
|
|
},
|
|
}
|
|
|
|
decryptCertsSubCmd = &cobra.Command{
|
|
Use: "decrypt [certPath] [cryptoKey]",
|
|
Short: "Decrypt certificates",
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return client.DecryptCertificates(args[0], args[1])
|
|
},
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
renewCertSubCmd.AddCommand(updateCertLinkSubCmd, decryptCertsSubCmd)
|
|
app.CertCmd.AddCommand(renewCertSubCmd)
|
|
}
|
|
|
|
func renewCert(domain string) error {
|
|
config := app.Config()
|
|
domainConfig, exists := app.DomainStore().Get(domain)
|
|
if !exists {
|
|
return app.ErrConfigNotFound
|
|
}
|
|
url := common.RepoURL(config, domainConfig, domain)
|
|
ws := common.NewGitWorkspace(domain, url)
|
|
if err := common.CloneRepo(ws, config); err != nil {
|
|
return fmt.Errorf("clone %s: %w", domain, err)
|
|
}
|
|
certsDir := common.CertsDir(config, domainConfig, domain)
|
|
return client.DecryptAndWriteCertificates(certsDir, domainConfig, ws)
|
|
}
|
|
|
|
func updateLinks(domain string) error {
|
|
domainConfig, exists := app.DomainStore().Get(domain)
|
|
if !exists {
|
|
return fmt.Errorf("domain %s does not exist", domain)
|
|
}
|
|
certsDir := common.CertsDir(app.Config(), domainConfig, domain)
|
|
return client.UpdateSymlinks(domain, domainConfig, certsDir)
|
|
}
|