Migrated to cobra for command handling and viper for config handling

This commit is contained in:
2026-02-25 21:17:23 +01:00
parent 61b65bf81c
commit 9eeb7a6ec0
13 changed files with 646 additions and 580 deletions

47
git.go
View File

@@ -10,7 +10,6 @@ import (
"time"
"code.gitea.io/sdk/gitea"
"git.nevets.tech/Steven/ezconf"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5"
gitconf "github.com/go-git/go-git/v5/config"
@@ -18,6 +17,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v55/github"
"github.com/spf13/viper"
)
type GitWorkspace struct {
@@ -57,11 +57,11 @@ func strToGitSource(s string) (GitSource, error) {
}
func createGithubClient() *github.Client {
return github.NewClient(nil).WithAuthToken(config.GetAsString("Git.api_token"))
return github.NewClient(nil).WithAuthToken(config.GetString("Git.api_token"))
}
func createGiteaClient() *gitea.Client {
client, err := gitea.NewClient(config.GetAsString("Git.server"), gitea.SetToken(config.GetAsString("Git.api_token")))
client, err := gitea.NewClient(config.GetString("Git.server"), gitea.SetToken(config.GetString("Git.api_token")))
if err != nil {
fmt.Printf("Error connecting to gitea instance: %v\n", err)
return nil
@@ -71,7 +71,7 @@ func createGiteaClient() *gitea.Client {
func createGithubRepo(domain *Domain, client *github.Client) string {
name := domain.name
owner := domain.config.GetAsString("Repo.owner")
owner := domain.config.GetString("Repo.owner")
description := domain.description
private := true
includeAllBranches := false
@@ -84,7 +84,7 @@ func createGithubRepo(domain *Domain, client *github.Client) string {
Private: &private,
IncludeAllBranches: &includeAllBranches,
}
repo, _, err := client.Repositories.CreateFromTemplate(ctx, config.GetAsString("Git.org_name"), config.GetAsString("Git.template_name"), template)
repo, _, err := client.Repositories.CreateFromTemplate(ctx, config.GetString("Git.org_name"), config.GetString("Git.template_name"), template)
if err != nil {
fmt.Println("Error creating repository from template,", err)
return ""
@@ -93,7 +93,7 @@ func createGithubRepo(domain *Domain, client *github.Client) string {
}
func createGiteaRepo(domain string, giteaClient *gitea.Client) string {
domainConfig, exists := getDomainConfig(domain)
domainConfig, exists := domainStore.Get(domain)
if !exists {
fmt.Printf("Domain %s config does not exist\n", domain)
return ""
@@ -111,7 +111,7 @@ func createGiteaRepo(domain string, giteaClient *gitea.Client) string {
// Webhooks: true,
//}
options := gitea.CreateRepoOption{
Name: domain + domainConfig.GetAsString("Repo.repo_suffix"),
Name: domain + domainConfig.GetString("Repo.repo_suffix"),
Description: "Certificate storage for " + domain,
Private: true,
IssueLabels: "",
@@ -124,8 +124,7 @@ func createGiteaRepo(domain string, giteaClient *gitea.Client) string {
TrustModel: gitea.TrustModelDefault,
}
giteaRepo, _, err := giteaClient.CreateOrgRepo(config.GetAsString("Git.org_name"), options)
//giteaRepo, _, err := giteaClient.CreateRepoFromTemplate(config.GetAsString("Git.org_name"), config.GetAsString("Git.template_name"), options)
giteaRepo, _, err := giteaClient.CreateOrgRepo(config.GetString("Git.org_name"), options)
if err != nil {
fmt.Printf("Error creating repo: %v\n", err)
return ""
@@ -161,8 +160,8 @@ func initRepo(url string, ws *GitWorkspace) error {
func cloneRepo(url string, ws *GitWorkspace) error {
creds := &http.BasicAuth{
Username: config.GetAsString("Git.username"),
Password: config.GetAsString("Git.api_token"),
Username: config.GetString("Git.username"),
Password: config.GetString("Git.api_token"),
}
var err error
ws.Repo, err = git.Clone(ws.Storage, ws.FS, &git.CloneOptions{URL: url, Auth: creds})
@@ -179,15 +178,15 @@ func cloneRepo(url string, ws *GitWorkspace) error {
}
func addAndPushCerts(domain string, ws *GitWorkspace) error {
domainConfig, exists := getDomainConfig(domain)
domainConfig, exists := domainStore.Get(domain)
if !exists {
fmt.Printf("Domain %s config does not exist\n", domain)
return ConfigNotFound
return ErrConfigNotFound
}
certsDir, err := getDomainCertsDirWConf(domain, domainConfig)
if err != nil {
if errors.Is(err, ConfigNotFound) {
if errors.Is(err, ErrConfigNotFound) {
fmt.Printf("Domain %s config not found: %v\n", domain, err)
return err
}
@@ -244,7 +243,7 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
fmt.Println("Work Tree Status:\n" + status.String())
signature := &object.Signature{
Name: "Cert Manager",
Email: config.GetAsString("Certificates.email"),
Email: config.GetString("Certificates.email"),
When: time.Now(),
}
commitHash, err := ws.WorkTree.Commit("Update "+domain+" @ "+time.Now().Format("Mon Jan _2 2006 15:04:05 MST"), &git.CommitOptions{Author: signature, Committer: signature})
@@ -253,8 +252,8 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
return err
}
creds := &http.BasicAuth{
Username: config.GetAsString("Git.username"),
Password: config.GetAsString("Git.api_token"),
Username: config.GetString("Git.username"),
Password: config.GetString("Git.api_token"),
}
err = ws.Repo.Push(&git.PushOptions{
Auth: creds,
@@ -269,7 +268,7 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
return err
}
fmt.Println("Successfully uploaded to " + config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name") + "/" + domain + domainConfig.GetAsString("Repo.repo_suffix") + ".git")
fmt.Println("Successfully uploaded to " + config.GetString("Git.server") + "/" + config.GetString("Git.org_name") + "/" + domain + domainConfig.GetString("Repo.repo_suffix") + ".git")
err = writeCommitHash(commitHash.String(), domainConfig)
if err != nil {
@@ -280,10 +279,10 @@ func addAndPushCerts(domain string, ws *GitWorkspace) error {
return nil
}
func writeCommitHash(hash string, domainConfig *ezconf.Configuration) error {
func writeCommitHash(hash string, domainConfig *viper.Viper) error {
certsDir, err := getDomainCertsDirWOnlyConf(domainConfig)
if err != nil {
if errors.Is(err, ConfigNotFound) {
if errors.Is(err, ErrConfigNotFound) {
return err
}
return err
@@ -300,7 +299,7 @@ func writeCommitHash(hash string, domainConfig *ezconf.Configuration) error {
func getLocalCommitHash(domain string) (string, error) {
certsDir, err := getDomainCertsDir(domain)
if err != nil {
if errors.Is(err, ConfigNotFound) {
if errors.Is(err, ErrConfigNotFound) {
fmt.Printf("Domain %s config not found: %v\n", domain, err)
return "", err
}
@@ -317,15 +316,15 @@ func getLocalCommitHash(domain string) (string, error) {
}
func getRemoteCommitHash(domain string, gitSource GitSource) (string, error) {
domainConfig, exists := getDomainConfig(domain)
domainConfig, exists := domainStore.Get(domain)
if !exists {
fmt.Printf("Domain %s config does not exist\n", domain)
return "", ConfigNotFound
return "", ErrConfigNotFound
}
switch gitSource {
case Gitea:
return getRemoteCommitHashGitea(config.GetAsString("Git.org_name"), domain+domainConfig.GetAsString("Repo.repo_suffix"), "master")
return getRemoteCommitHashGitea(config.GetString("Git.org_name"), domain+domainConfig.GetString("Repo.repo_suffix"), "master")
default:
fmt.Printf("Unimplemented git source %v\n", gitSource)
return "", errors.New("unimplemented git source")