45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.nevets.tech/Keys/certman/common"
|
|
)
|
|
|
|
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
|
|
}
|