lots o progress
This commit is contained in:
139
git.go
Normal file
139
git.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/google/go-github/v55/github"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
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 {
|
||||
options := gitea.CreateRepoFromTemplateOption{
|
||||
Avatar: true,
|
||||
Description: "Certificates storage for " + domain,
|
||||
GitContent: true,
|
||||
GitHooks: true,
|
||||
Labels: true,
|
||||
Name: domain + "-certificates",
|
||||
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() {
|
||||
certs, 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 _, cert := range certs {
|
||||
if strings.HasPrefix(cert.Name(), domain) {
|
||||
file, err := fs.Create(cert.Name())
|
||||
if err != nil {
|
||||
fmt.Printf("Error copying cert to memfs: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
certFile, err := os.ReadFile(config.GetAsString("Certificates.certs_path") + "/certificates/" + cert.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(cert.Name())
|
||||
if err != nil {
|
||||
fmt.Printf("Error adding certificate %v: %v", cert.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")
|
||||
}
|
||||
Reference in New Issue
Block a user