142 lines
4.1 KiB
Go
142 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
"github.com/google/go-github/v55/github"
|
|
)
|
|
|
|
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)
|
|
os.Exit(1)
|
|
}
|
|
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)
|
|
os.Exit(1)
|
|
}
|
|
return *repo.CloneURL
|
|
}
|
|
|
|
func createGiteaRepo() string {
|
|
domainConfig := getDomainConfig(domain)
|
|
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,
|
|
}
|
|
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)
|
|
os.Exit(1)
|
|
}
|
|
return giteaRepo.CloneURL
|
|
}
|
|
|
|
func cloneRepo(url string) (*git.Repository, *git.Worktree) {
|
|
repository, err := git.Clone(storage, fs, &git.CloneOptions{URL: url, Auth: creds})
|
|
if err != nil {
|
|
fmt.Printf("Error clone git repo: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
workingTree, err := repo.Worktree()
|
|
if err != nil {
|
|
fmt.Printf("Error getting worktree from repo: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
return repository, workingTree
|
|
}
|
|
|
|
func addAndPushCerts() {
|
|
certFiles, err := os.ReadDir(config.GetAsString("Certificates.certs_path") + "/certificates")
|
|
if err != nil {
|
|
fmt.Printf("Error reading from directory: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
for _, file := range certFiles {
|
|
if strings.HasPrefix(file.Name(), domain) {
|
|
file, err := fs.Create(file.Name())
|
|
if err != nil {
|
|
fmt.Printf("Error copying file to memfs: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
certFile, err := os.ReadFile(config.GetAsString("Certificates.certs_path") + "/certificates/" + file.Name())
|
|
//certFile = encryptBytes(certFile)
|
|
_, err = file.Write(certFile)
|
|
err = file.Close()
|
|
if err != nil {
|
|
fmt.Printf("Error writing to memfs: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
_, err = workTree.Add(file.Name())
|
|
if err != nil {
|
|
fmt.Printf("Error adding file %v: %v", file.Name(), err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|
|
|
|
status, err := workTree.Status()
|
|
if err != nil {
|
|
fmt.Printf("Error getting repo status: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Work Tree Status:\n" + status.String())
|
|
signature := &object.Signature{
|
|
Name: "Cert Manager",
|
|
Email: config.GetAsString("Git.email"),
|
|
When: time.Now(),
|
|
}
|
|
_, err = 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)
|
|
os.Exit(1)
|
|
}
|
|
err = repo.Push(&git.PushOptions{Auth: creds, Force: true, RemoteName: "origin"})
|
|
if err != nil {
|
|
fmt.Printf("Error pushing to origin: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Println("Successfully uploaded to " + config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name") + "/" + domain + "-certificates.git")
|
|
}
|