package main import ( "code.gitea.io/sdk/gitea" "fmt" "git.nevets.tech/Steven/ezconf" "os/exec" "github.com/go-git/go-billy/v5" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/storage/memory" "os" ) var config *ezconf.Configuration var giteaClient *gitea.Client var domain string var legoBaseArgs []string var storage *memory.Storage var fs billy.Filesystem var workTree *git.Worktree var creds *http.BasicAuth var repo *git.Repository func main() { var err error args := os.Args // -c hasConfig, configIndex := contains(args, "-c") if hasConfig { config = ezconf.NewConfiguration(args[configIndex+1]) } else { fmt.Printf("Error, no config passed. Please add '-c /path/to/config.ini' to the command") os.Exit(1) } // -d hasDomain, domainIndex := contains(args, "-d") if hasDomain { domain = args[domainIndex+1] } else { fmt.Printf("Error, no domain passed. Please add '-d domain.tld' to the command") os.Exit(1) } legoBaseArgs = []string{ "-a", "--dns", "cloudflare", "--email=" + config.GetAsString("Cloudflare.cf_email"), "--domains=" + domain, "--domains=*." + domain, "--path=" + config.GetAsString("Certificates.certs_path"), } legoNewSiteArgs := append(legoBaseArgs, "run") legoRenewSiteArgs := append(legoBaseArgs, "renew", "--days", "90") subdomains := config.GetAsStrings("Certificates.subdomains") if subdomains != nil { for i, subdomain := range subdomains { insert(legoBaseArgs, 5+i, "--domains=*."+subdomain) } } err = os.Setenv("CF_API_TOKEN", config.GetAsString("Cloudflare.cf_api_token")) err = os.Setenv("CF_EMAIL", config.GetAsString("Cloudflare.cf_email")) if err != nil { fmt.Printf("Error setting environment variable: %v", err) os.Exit(1) } creds = &http.BasicAuth{ Username: config.GetAsString("Git.username"), Password: config.GetAsString("Git.api_token"), } giteaClient, err = gitea.NewClient(config.GetAsString("Git.server"), gitea.SetBasicAuth(config.GetAsString("Git.username"), config.GetAsString("Git.api_token"))) if err != nil { fmt.Printf("Error connecting to gitea instance: %v", err) os.Exit(1) } switch args[len(args)-1] { case "gen": { url := createGiteaRepo() cloneRepo(url) exec.Command("lego", legoNewSiteArgs...) } case "renew": { cloneRepo(config.GetAsString("Git.server") + "/" + config.GetAsString("Git.org_name")) exec.Command("lego", legoRenewSiteArgs...) } default: { fmt.Println("Missing arguments: conclude command with 'gen' or 'renew'") os.Exit(1) } } } func createGiteaRepo() string { options := gitea.CreateRepoFromTemplateOption{ Owner: config.GetAsString("Git.repo_owner"), Name: domain + "-certificates", Description: "Certificates storage for " + domain, Private: 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", err) os.Exit(1) } return giteaRepo.CloneURL } func cloneRepo(url string) { var err error repo, err = git.Clone(storage, fs, &git.CloneOptions{URL: url, Auth: creds}) if err != nil { fmt.Printf("Error clone git repo: %v", err) os.Exit(1) } workTree, err = repo.Worktree() if err != nil { fmt.Printf("Error getting worktree from repo: %v", err) os.Exit(1) } } func addAndPushCerts() { // Copy certs to memfs //file, err := fs.Create("") //if err != nil { // return //} _, err = workTree.Add(domain + "*") if err != nil { fmt.Printf("Error adding certificates to workTree: %v", err) os.Exit(1) } status, err := workTree.Status() if err != nil { fmt.Printf("Error getting repo status: %v", err) os.Exit(1) } fmt.Println(status.String()) } func contains(slice []string, value string) (sliceHas bool, index int) { for i, entry := range slice { if entry == value { return true, i } } return false, -1 } func insert(a []string, index int, value string) []string { last := len(a) - 1 a = append(a, a[last]) copy(a[index+1:], a[index:last]) a[index] = value return a }