2021-09-04 23:55:10 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-09-04 23:55:10 +00:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EnsureExists checks whether a recipe has been cloned locally or not.
|
|
|
|
func EnsureExists(recipe string) error {
|
|
|
|
recipeDir := path.Join(config.ABRA_DIR, "apps", strings.ToLower(recipe))
|
|
|
|
|
|
|
|
if _, err := os.Stat(recipeDir); os.IsNotExist(err) {
|
|
|
|
url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, recipe)
|
|
|
|
_, err := git.PlainClone(recipeDir, false, &git.CloneOptions{URL: url, Tags: git.AllTags})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureVersion checks whether a specific version exists for a recipe.
|
|
|
|
func EnsureVersion(version string) error {
|
|
|
|
recipeDir := path.Join(config.ABRA_DIR, "apps", strings.ToLower(version))
|
|
|
|
|
|
|
|
repo, err := git.PlainOpen(recipeDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, err := repo.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var tagRef plumbing.ReferenceName
|
|
|
|
if err := tags.ForEach(func(ref *plumbing.Reference) (err error) {
|
|
|
|
if ref.Name().Short() == version {
|
|
|
|
tagRef = ref.Name()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagRef.String() == "" {
|
|
|
|
return fmt.Errorf("%s is not available?", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
worktree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &git.CheckoutOptions{Branch: tagRef, Keep: true}
|
|
|
|
if err := worktree.Checkout(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|