forked from toolshed/abra
		
	fix: much hacking, maybe fixed catalogue generation
This commit is contained in:
		| @ -4,7 +4,6 @@ import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"os" | ||||
| 	"path" | ||||
|  | ||||
| 	"coopcloud.tech/abra/cli/formatter" | ||||
| @ -104,17 +103,18 @@ A new catalogue copy can be published to the recipes repository by passing the | ||||
| 			logrus.Fatal(err) | ||||
| 		} | ||||
|  | ||||
| 		logrus.Debugf("ensuring %v recipe(s) are locally present and up-to-date", len(repos)) | ||||
|  | ||||
| 		var barLength int | ||||
| 		var logMsg string | ||||
| 		if recipeName != "" { | ||||
| 			barLength = 1 | ||||
| 			logMsg = fmt.Sprintf("ensuring %v recipe is up-to-date", barLength) | ||||
| 		} else { | ||||
| 			barLength = len(repos) | ||||
| 			logMsg = fmt.Sprintf("ensuring %v recipes are up-to-date", barLength) | ||||
| 		} | ||||
|  | ||||
| 		if !internal.SkipUpdates { | ||||
| 			logrus.Warnf("ensuring %v recipe(s) are up-to-date", barLength) | ||||
| 			logrus.Warn(logMsg) | ||||
| 			if err := updateRepositories(repos, recipeName); err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
| @ -154,6 +154,7 @@ A new catalogue copy can be published to the recipes repository by passing the | ||||
| 				Category:      category, | ||||
| 				Features:      features, | ||||
| 			} | ||||
|  | ||||
| 			catlBar.Add(1) | ||||
| 		} | ||||
|  | ||||
| @ -162,25 +163,25 @@ A new catalogue copy can be published to the recipes repository by passing the | ||||
| 			logrus.Fatal(err) | ||||
| 		} | ||||
|  | ||||
| 		if _, err := os.Stat(config.RECIPES_JSON); err != nil && os.IsNotExist(err) { | ||||
| 		if recipeName == "" { | ||||
| 			if err := ioutil.WriteFile(config.RECIPES_JSON, recipesJSON, 0764); err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
| 		} else { | ||||
| 			if recipeName != "" { | ||||
| 				catlFS, err := catalogue.ReadRecipeCatalogue() | ||||
| 				if err != nil { | ||||
| 					logrus.Fatal(err) | ||||
| 				} | ||||
| 				catlFS[recipeName] = catl[recipeName] | ||||
| 			catlFS, err := catalogue.ReadRecipeCatalogue() | ||||
| 			if err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
|  | ||||
| 				updatedRecipesJSON, err := json.MarshalIndent(catlFS, "", "    ") | ||||
| 				if err != nil { | ||||
| 					logrus.Fatal(err) | ||||
| 				} | ||||
| 				if err := ioutil.WriteFile(config.RECIPES_JSON, updatedRecipesJSON, 0764); err != nil { | ||||
| 					logrus.Fatal(err) | ||||
| 				} | ||||
| 			catlFS[recipeName] = catl[recipeName] | ||||
|  | ||||
| 			updatedRecipesJSON, err := json.MarshalIndent(catlFS, "", "    ") | ||||
| 			if err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
|  | ||||
| 			if err := ioutil.WriteFile(config.RECIPES_JSON, updatedRecipesJSON, 0764); err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| @ -207,7 +208,6 @@ A new catalogue copy can be published to the recipes repository by passing the | ||||
| 					logrus.Fatal(err) | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 		} | ||||
|  | ||||
| 		return nil | ||||
| @ -270,7 +270,7 @@ func updateRepositories(repos catalogue.RepoCatalogue, recipeName string) error | ||||
| 				logrus.Fatalf("%s has locally unstaged changes", rm.Name) | ||||
| 			} | ||||
|  | ||||
| 			if err := catalogue.EnsureUpToDate(rm.Name); err != nil { | ||||
| 			if err := recipe.EnsureUpToDate(rm.Name); err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
|  | ||||
|  | ||||
| @ -15,7 +15,6 @@ import ( | ||||
| 	"coopcloud.tech/abra/cli/formatter" | ||||
| 	"coopcloud.tech/abra/pkg/client" | ||||
| 	"coopcloud.tech/abra/pkg/config" | ||||
| 	gitPkg "coopcloud.tech/abra/pkg/git" | ||||
| 	"coopcloud.tech/abra/pkg/recipe" | ||||
| 	"coopcloud.tech/abra/pkg/web" | ||||
| 	"github.com/docker/distribution/reference" | ||||
| @ -357,6 +356,8 @@ func ReadReposMetadata() (RepoCatalogue, error) { | ||||
| 		bar.Add(1) | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println() // newline for spinner | ||||
|  | ||||
| 	return reposMeta, nil | ||||
| } | ||||
|  | ||||
| @ -464,22 +465,11 @@ func GetRecipeVersions(recipeName string) (RecipeVersions, error) { | ||||
| 		return versions, err | ||||
| 	} | ||||
|  | ||||
| 	branch, err := gitPkg.GetCurrentBranch(repo) | ||||
| 	_, err = recipe.CheckoutDefaultBranch(repo, recipeName) | ||||
| 	if err != nil { | ||||
| 		return versions, err | ||||
| 	} | ||||
|  | ||||
| 	checkOutOpts := &git.CheckoutOptions{ | ||||
| 		Create: false, | ||||
| 		Force:  true, | ||||
| 		Branch: plumbing.ReferenceName(branch), | ||||
| 	} | ||||
| 	if err := worktree.Checkout(checkOutOpts); err != nil { | ||||
| 		logrus.Debugf("failed to check out %s in %s", branch, recipeDir) | ||||
| 		logrus.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	logrus.Debugf("switched back to %s in %s", branch, recipeDir) | ||||
| 	logrus.Debugf("collected %s for %s", versions, recipeName) | ||||
|  | ||||
| 	return versions, nil | ||||
|  | ||||
| @ -310,72 +310,6 @@ func GetVersionLabelLocal(recipe Recipe) (string, error) { | ||||
| 	return label, nil | ||||
| } | ||||
|  | ||||
| // EnsureUpToDate ensures that the local repo is synced to the remote | ||||
| func EnsureUpToDate(recipeName string) error { | ||||
| 	isClean, err := gitPkg.IsClean(recipeName) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if !isClean { | ||||
| 		return fmt.Errorf("%s has locally unstaged changes", recipeName) | ||||
| 	} | ||||
|  | ||||
| 	recipeDir := path.Join(config.RECIPES_DIR, recipeName) | ||||
| 	repo, err := git.PlainOpen(recipeDir) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	worktree, err := repo.Worktree() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	branch, err := gitPkg.GetCurrentBranch(repo) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	checkOutOpts := &git.CheckoutOptions{ | ||||
| 		Create: false, | ||||
| 		Force:  true, | ||||
| 		Branch: plumbing.ReferenceName(branch), | ||||
| 	} | ||||
|  | ||||
| 	if err := worktree.Checkout(checkOutOpts); err != nil { | ||||
| 		logrus.Debugf("failed to check out %s in %s", branch, recipeDir) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	logrus.Debugf("successfully checked out %s in %s", branch, recipeDir) | ||||
|  | ||||
| 	remotes, err := repo.Remotes() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if len(remotes) == 0 { | ||||
| 		logrus.Debugf("cannot ensure %s is up-to-date, no git remotes configured", recipeName) | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	opts := &git.PullOptions{ | ||||
| 		Force:         true, | ||||
| 		ReferenceName: plumbing.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", recipeName) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func GetRecipeFeaturesAndCategory(recipeName string) (Features, string, error) { | ||||
| 	feat := Features{} | ||||
|  | ||||
| @ -503,3 +437,102 @@ func GetStringInBetween(str, start, end string) (result string, err error) { | ||||
|  | ||||
| 	return str[s : s+e], nil | ||||
| } | ||||
|  | ||||
| // EnsureUpToDate ensures that the local repo is synced to the remote | ||||
| func EnsureUpToDate(recipeName string) error { | ||||
| 	recipeDir := path.Join(config.RECIPES_DIR, recipeName) | ||||
|  | ||||
| 	isClean, err := gitPkg.IsClean(recipeName) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if !isClean { | ||||
| 		return fmt.Errorf("%s has locally unstaged changes", recipeName) | ||||
| 	} | ||||
|  | ||||
| 	repo, err := git.PlainOpen(recipeDir) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	remotes, err := repo.Remotes() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if len(remotes) == 0 { | ||||
| 		logrus.Debugf("cannot ensure %s is up-to-date, no git remotes configured", recipeName) | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	worktree, err := repo.Worktree() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	branch, err := CheckoutDefaultBranch(repo, recipeName) | ||||
| 	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", recipeName) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func GetDefaultBranch(repo *git.Repository, recipeName string) (plumbing.ReferenceName, error) { | ||||
| 	recipeDir := path.Join(config.RECIPES_DIR, recipeName) | ||||
|  | ||||
| 	branch := "master" | ||||
| 	if _, err := repo.Branch("master"); err != nil { | ||||
| 		if _, err := repo.Branch("main"); err != nil { | ||||
| 			logrus.Debugf("failed to select branch in %s", recipeDir) | ||||
| 			return "", err | ||||
| 		} | ||||
| 		branch = "main" | ||||
| 	} | ||||
|  | ||||
| 	return plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)), nil | ||||
| } | ||||
|  | ||||
| func CheckoutDefaultBranch(repo *git.Repository, recipeName string) (plumbing.ReferenceName, error) { | ||||
| 	recipeDir := path.Join(config.RECIPES_DIR, recipeName) | ||||
|  | ||||
| 	branch, err := GetDefaultBranch(repo, recipeName) | ||||
| 	if err != nil { | ||||
| 		return plumbing.ReferenceName(""), err | ||||
| 	} | ||||
|  | ||||
| 	worktree, err := repo.Worktree() | ||||
| 	if err != nil { | ||||
| 		return plumbing.ReferenceName(""), err | ||||
| 	} | ||||
|  | ||||
| 	checkOutOpts := &git.CheckoutOptions{ | ||||
| 		Create: false, | ||||
| 		Force:  true, | ||||
| 		Branch: branch, | ||||
| 	} | ||||
|  | ||||
| 	if err := worktree.Checkout(checkOutOpts); err != nil { | ||||
| 		recipeDir := path.Join(config.RECIPES_DIR, recipeName) | ||||
| 		logrus.Debugf("failed to check out %s in %s", branch, recipeDir) | ||||
| 		return branch, err | ||||
| 	} | ||||
|  | ||||
| 	logrus.Debugf("successfully checked out %v in %s", branch, recipeDir) | ||||
|  | ||||
| 	return branch, nil | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user