2023-01-22 16:52:36 +00:00
|
|
|
package catalogue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/pkg/config"
|
|
|
|
gitPkg "coopcloud.tech/abra/pkg/git"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EnsureCatalogue ensures that the catalogue is cloned locally & present.
|
2023-09-07 16:50:25 +00:00
|
|
|
func EnsureCatalogue() error {
|
2023-01-22 16:52:36 +00:00
|
|
|
catalogueDir := path.Join(config.ABRA_DIR, "catalogue")
|
|
|
|
if _, err := os.Stat(catalogueDir); err != nil && os.IsNotExist(err) {
|
2023-09-30 06:19:16 +00:00
|
|
|
logrus.Warnf("local recipe catalogue is missing, retrieving now")
|
2023-01-22 16:52:36 +00:00
|
|
|
url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, config.CATALOGUE_JSON_REPO_NAME)
|
|
|
|
if err := gitPkg.Clone(catalogueDir, url); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("cloned catalogue repository to %s", catalogueDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:50:25 +00:00
|
|
|
// EnsureIsClean makes sure that the catalogue has no unstaged changes.
|
|
|
|
func EnsureIsClean() error {
|
2023-01-22 16:52:36 +00:00
|
|
|
isClean, err := gitPkg.IsClean(config.CATALOGUE_DIR)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isClean {
|
|
|
|
msg := "%s has locally unstaged changes? please commit/remove your changes before proceeding"
|
|
|
|
return fmt.Errorf(msg, config.CATALOGUE_DIR)
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:50:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-26 06:16:07 +00:00
|
|
|
|
2023-09-07 16:50:25 +00:00
|
|
|
// EnsureUpToDate ensures that the local catalogue is up to date.
|
|
|
|
func EnsureUpToDate() error {
|
2023-01-22 16:52:36 +00:00
|
|
|
repo, err := git.PlainOpen(config.CATALOGUE_DIR)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
remotes, err := repo.Remotes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(remotes) == 0 {
|
|
|
|
msg := "cannot ensure %s is up-to-date, no git remotes configured"
|
|
|
|
logrus.Debugf(msg, config.CATALOGUE_DIR)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
worktree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
branch, err := gitPkg.CheckoutDefaultBranch(repo, config.CATALOGUE_DIR)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &git.PullOptions{
|
|
|
|
Force: true,
|
|
|
|
ReferenceName: branch,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := worktree.Pull(opts); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "already up-to-date") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("fetched latest git changes for %s", config.CATALOGUE_DIR)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|