All checks were successful
Build (artifact) / build (push) Has been skipped
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.nevets.tech/Steven/certman/common"
|
|
)
|
|
|
|
func WriteCommitHash(hash, dataRoot string) error {
|
|
//TODO: unfuck this logic, maybe use a domain struct with a flag for non-standard data root?
|
|
//var dataRoot string
|
|
//if domainConfig.Certificates.DataRoot == "" {
|
|
// dataRoot = filepath.Join(config.Certificates.DataRoot, "certificates", domainConfig.Domain.DomainName)
|
|
//} else {
|
|
// dataRoot = domainConfig.Certificates.DataRoot
|
|
//}
|
|
|
|
err := os.WriteFile(filepath.Join(dataRoot, "hash"), []byte(hash), 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func LocalCommitHash(domain string, certsDir string) (string, error) {
|
|
data, err := os.ReadFile(filepath.Join(certsDir, "hash"))
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
fmt.Printf("Error reading file for domain %s: %v\n", domain, err)
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
return strings.TrimSpace(string(data)), nil
|
|
}
|
|
|
|
func RemoteCommitHash(domain string, gitSource common.GitSource, config *common.AppConfig, domainConfig *common.DomainConfig) (string, error) {
|
|
switch gitSource {
|
|
case common.Gitea:
|
|
return getRemoteCommitHashGitea(config.Git.OrgName, domain+domainConfig.Repo.RepoSuffix, "master", config)
|
|
default:
|
|
fmt.Printf("Unimplemented git source %v\n", gitSource)
|
|
return "", errors.New("unimplemented git source")
|
|
}
|
|
}
|
|
|
|
func getRemoteCommitHashGitea(org, repo, branchName string, config *common.AppConfig) (string, error) {
|
|
giteaClient := common.CreateGiteaClient(config)
|
|
branch, _, err := giteaClient.GetRepoBranch(org, repo, branchName)
|
|
if err != nil {
|
|
fmt.Printf("Error getting repo branch: %v\n", err)
|
|
return "", err
|
|
}
|
|
//TODO catch repo not found as ErrRepoNotInit
|
|
return branch.Commit.ID, nil
|
|
}
|