262 lines
7.0 KiB
Go
262 lines
7.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"github.com/go-git/go-billy/v5"
|
|
"github.com/go-git/go-git/v5"
|
|
gitconf "github.com/go-git/go-git/v5/config"
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
"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"
|
|
)
|
|
|
|
type GitWorkspace struct {
|
|
Repo *git.Repository
|
|
Storage *memory.Storage
|
|
FS billy.Filesystem
|
|
WorkTree *git.Worktree
|
|
}
|
|
|
|
func createGithubClient() *github.Client {
|
|
return github.NewClient(nil).WithAuthToken(config.GetAsString("Git.api_token"))
|
|
}
|
|
|
|
func createGiteaClient() *gitea.Client {
|
|
client, err := gitea.NewClient(config.GetAsString("Git.server"), gitea.SetToken(config.GetAsString("Git.api_token")))
|
|
if err != nil {
|
|
fmt.Printf("Error connecting to gitea instance: %v\n", err)
|
|
return nil
|
|
}
|
|
return client
|
|
}
|
|
|
|
func createGithubRepo(domain *Domain, client *github.Client) string {
|
|
name := domain.name
|
|
owner := domain.config.GetAsString("Repo.owner")
|
|
description := domain.description
|
|
private := true
|
|
includeAllBranches := false
|
|
|
|
ctx := context.Background()
|
|
template := &github.TemplateRepoRequest{
|
|
Name: name,
|
|
Owner: &owner,
|
|
Description: description,
|
|
Private: &private,
|
|
IncludeAllBranches: &includeAllBranches,
|
|
}
|
|
repo, _, err := client.Repositories.CreateFromTemplate(ctx, config.GetAsString("Git.org_name"), config.GetAsString("Git.template_name"), template)
|
|
if err != nil {
|
|
fmt.Println("Error creating repository from template,", err)
|
|
return ""
|
|
}
|
|
return *repo.CloneURL
|
|
}
|
|
|
|
func createGiteaRepo(domain string, giteaClient *gitea.Client) string {
|
|
domainConfig, exists := getDomainConfig(domain)
|
|
if !exists {
|
|
fmt.Printf("Domain %s config does not exist\n", domain)
|
|
return ""
|
|
}
|
|
//options := gitea.CreateRepoFromTemplateOption{
|
|
// Avatar: true,
|
|
// Description: "Certificates storage for " + domain,
|
|
// GitContent: true,
|
|
// GitHooks: true,
|
|
// Labels: true,
|
|
// Name: domain + domainConfig.GetAsString("Repo.repo_suffix"),
|
|
// Owner: config.GetAsString("Git.org_name"),
|
|
// Private: true,
|
|
// Topics: true,
|
|
// Webhooks: true,
|
|
//}
|
|
options := gitea.CreateRepoOption{
|
|
Name: domain + domainConfig.GetAsString("Repo.repo_suffix"),
|
|
Description: "Certificate storage for " + domain,
|
|
Private: true,
|
|
IssueLabels: "",
|
|
AutoInit: false,
|
|
Template: false,
|
|
Gitignores: "",
|
|
License: "",
|
|
Readme: "",
|
|
DefaultBranch: "master",
|
|
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)
|
|
if err != nil {
|
|
fmt.Printf("Error creating repo: %v\n", err)
|
|
return ""
|
|
}
|
|
return giteaRepo.CloneURL
|
|
}
|
|
|
|
func initRepo(url string, ws *GitWorkspace) error {
|
|
var err error
|
|
ws.Repo, err = git.Init(ws.Storage, ws.FS)
|
|
if err != nil {
|
|
fmt.Printf("Error initializing local repo: %v\n", err)
|
|
return err
|
|
}
|
|
|
|
_, err = ws.Repo.CreateRemote(&gitconf.RemoteConfig{
|
|
Name: "origin",
|
|
URLs: []string{url},
|
|
})
|
|
if err != nil && !errors.Is(err, git.ErrRemoteExists) {
|
|
fmt.Printf("Error creating remote origin repo: %v\n", err)
|
|
return err
|
|
}
|
|
|
|
ws.WorkTree, err = ws.Repo.Worktree()
|
|
if err != nil {
|
|
fmt.Printf("Error getting worktree from local repo: %v\n", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func cloneRepo(url string, ws *GitWorkspace) error {
|
|
creds := &http.BasicAuth{
|
|
Username: config.GetAsString("Git.username"),
|
|
Password: config.GetAsString("Git.api_token"),
|
|
}
|
|
var err error
|
|
ws.Repo, err = git.Clone(ws.Storage, ws.FS, &git.CloneOptions{URL: url, Auth: creds})
|
|
if err != nil {
|
|
fmt.Printf("Error cloning repo: %v\n", err)
|
|
}
|
|
|
|
ws.WorkTree, err = ws.Repo.Worktree()
|
|
if err != nil {
|
|
fmt.Printf("Error getting worktree from cloned repo: %v\n", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func addAndPushCerts(domain string, ws *GitWorkspace) error {
|
|
domainConfig, exists := getDomainConfig(domain)
|
|
if !exists {
|
|
fmt.Printf("Domain %s config does not exist\n", domain)
|
|
return ConfigNotFound
|
|
}
|
|
|
|
effectiveDataRoot, err := getEffectiveString(domainConfig, "Certificates.data_root")
|
|
if err != nil {
|
|
fmt.Printf("Error getting effective data root for domain %s: %v\n", domain, err)
|
|
return err
|
|
}
|
|
|
|
certFiles, err := os.ReadDir(filepath.Join(effectiveDataRoot, "certificates", domain))
|
|
if err != nil {
|
|
fmt.Printf("Error reading from directory: %v\n", err)
|
|
return err
|
|
}
|
|
for _, entry := range certFiles {
|
|
if strings.HasSuffix(entry.Name(), ".crpt") {
|
|
file, err := ws.FS.Create(entry.Name())
|
|
if err != nil {
|
|
fmt.Printf("Error copying file to memfs: %v\n", err)
|
|
return err
|
|
}
|
|
certFile, err := os.ReadFile(filepath.Join(effectiveDataRoot, "certificates", domain, file.Name()))
|
|
if err != nil {
|
|
fmt.Printf("Error reading file to memfs: %v\n", err)
|
|
file.Close()
|
|
return err
|
|
}
|
|
_, err = file.Write(certFile)
|
|
if err != nil {
|
|
fmt.Printf("Error writing to memfs: %v\n", err)
|
|
file.Close()
|
|
return err
|
|
}
|
|
_, err = ws.WorkTree.Add(file.Name())
|
|
if err != nil {
|
|
fmt.Printf("Error adding file %v: %v\n", file.Name(), err)
|
|
file.Close()
|
|
return err
|
|
}
|
|
err = file.Close()
|
|
if err != nil {
|
|
fmt.Printf("Error closing file: %v\n", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
status, err := ws.WorkTree.Status()
|
|
if err != nil {
|
|
fmt.Printf("Error getting repo status: %v\n", err)
|
|
return err
|
|
}
|
|
if status.IsClean() {
|
|
fmt.Printf("Repository is clean, skipping commit...\n")
|
|
return nil
|
|
}
|
|
|
|
fmt.Println("Work Tree Status:\n" + status.String())
|
|
signature := &object.Signature{
|
|
Name: "Cert Manager",
|
|
Email: config.GetAsString("Certificates.email"),
|
|
When: time.Now(),
|
|
}
|
|
_, err = ws.WorkTree.Commit("Update "+domain+" @ "+time.Now().Format("Mon Jan _2 2006 15:04:05 MST"), &git.CommitOptions{Author: signature, Committer: signature})
|
|
if err != nil {
|
|
fmt.Printf("Error committing certs: %v\n", err)
|
|
return err
|
|
}
|
|
creds := &http.BasicAuth{
|
|
Username: config.GetAsString("Git.username"),
|
|
Password: config.GetAsString("Git.api_token"),
|
|
}
|
|
err = ws.Repo.Push(&git.PushOptions{
|
|
Auth: creds,
|
|
Force: true,
|
|
RemoteName: "origin",
|
|
RefSpecs: []gitconf.RefSpec{
|
|
"refs/heads/master:refs/heads/master",
|
|
},
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("Error pushing to origin: %v\n", err)
|
|
return err
|
|
}
|
|
|
|
fmt.Println("Successfully uploaded to " + config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name") + "/" + domain + domainConfig.GetAsString("Repo.repo_suffix") + ".git")
|
|
return nil
|
|
}
|
|
|
|
func getLocalCommitHash(domain string) (string, error) {
|
|
|
|
return "", nil
|
|
}
|
|
|
|
func writeCommitHash(domain string, ws *GitWorkspace) error {
|
|
//ref, err := ws.Repo.Head()
|
|
//if err != nil {
|
|
// fmt.Printf("Error getting HEAD: %v\n", err)
|
|
// return err
|
|
//}
|
|
//hash := ref.Hash()
|
|
return nil
|
|
}
|
|
|
|
func getRemoteCommitHash(domain string) (string, error) {
|
|
|
|
return "", nil
|
|
}
|