forked from toolshed/abra
		
	fix: much hacking, maybe fixed catalogue generation
This commit is contained in:
		| @ -4,7 +4,6 @@ import ( | |||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"os" |  | ||||||
| 	"path" | 	"path" | ||||||
|  |  | ||||||
| 	"coopcloud.tech/abra/cli/formatter" | 	"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.Fatal(err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		logrus.Debugf("ensuring %v recipe(s) are locally present and up-to-date", len(repos)) |  | ||||||
|  |  | ||||||
| 		var barLength int | 		var barLength int | ||||||
|  | 		var logMsg string | ||||||
| 		if recipeName != "" { | 		if recipeName != "" { | ||||||
| 			barLength = 1 | 			barLength = 1 | ||||||
|  | 			logMsg = fmt.Sprintf("ensuring %v recipe is up-to-date", barLength) | ||||||
| 		} else { | 		} else { | ||||||
| 			barLength = len(repos) | 			barLength = len(repos) | ||||||
|  | 			logMsg = fmt.Sprintf("ensuring %v recipes are up-to-date", barLength) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if !internal.SkipUpdates { | 		if !internal.SkipUpdates { | ||||||
| 			logrus.Warnf("ensuring %v recipe(s) are up-to-date", barLength) | 			logrus.Warn(logMsg) | ||||||
| 			if err := updateRepositories(repos, recipeName); err != nil { | 			if err := updateRepositories(repos, recipeName); err != nil { | ||||||
| 				logrus.Fatal(err) | 				logrus.Fatal(err) | ||||||
| 			} | 			} | ||||||
| @ -154,6 +154,7 @@ A new catalogue copy can be published to the recipes repository by passing the | |||||||
| 				Category:      category, | 				Category:      category, | ||||||
| 				Features:      features, | 				Features:      features, | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			catlBar.Add(1) | 			catlBar.Add(1) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| @ -162,25 +163,25 @@ A new catalogue copy can be published to the recipes repository by passing the | |||||||
| 			logrus.Fatal(err) | 			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 { | 			if err := ioutil.WriteFile(config.RECIPES_JSON, recipesJSON, 0764); err != nil { | ||||||
| 				logrus.Fatal(err) | 				logrus.Fatal(err) | ||||||
| 			} | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			if recipeName != "" { | 			catlFS, err := catalogue.ReadRecipeCatalogue() | ||||||
| 				catlFS, err := catalogue.ReadRecipeCatalogue() | 			if err != nil { | ||||||
| 				if err != nil { | 				logrus.Fatal(err) | ||||||
| 					logrus.Fatal(err) | 			} | ||||||
| 				} |  | ||||||
| 				catlFS[recipeName] = catl[recipeName] |  | ||||||
|  |  | ||||||
| 				updatedRecipesJSON, err := json.MarshalIndent(catlFS, "", "    ") | 			catlFS[recipeName] = catl[recipeName] | ||||||
| 				if err != nil { |  | ||||||
| 					logrus.Fatal(err) | 			updatedRecipesJSON, err := json.MarshalIndent(catlFS, "", "    ") | ||||||
| 				} | 			if err != nil { | ||||||
| 				if err := ioutil.WriteFile(config.RECIPES_JSON, updatedRecipesJSON, 0764); err != nil { | 				logrus.Fatal(err) | ||||||
| 					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) | 					logrus.Fatal(err) | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		return nil | 		return nil | ||||||
| @ -270,7 +270,7 @@ func updateRepositories(repos catalogue.RepoCatalogue, recipeName string) error | |||||||
| 				logrus.Fatalf("%s has locally unstaged changes", rm.Name) | 				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) | 				logrus.Fatal(err) | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | |||||||
| @ -15,7 +15,6 @@ import ( | |||||||
| 	"coopcloud.tech/abra/cli/formatter" | 	"coopcloud.tech/abra/cli/formatter" | ||||||
| 	"coopcloud.tech/abra/pkg/client" | 	"coopcloud.tech/abra/pkg/client" | ||||||
| 	"coopcloud.tech/abra/pkg/config" | 	"coopcloud.tech/abra/pkg/config" | ||||||
| 	gitPkg "coopcloud.tech/abra/pkg/git" |  | ||||||
| 	"coopcloud.tech/abra/pkg/recipe" | 	"coopcloud.tech/abra/pkg/recipe" | ||||||
| 	"coopcloud.tech/abra/pkg/web" | 	"coopcloud.tech/abra/pkg/web" | ||||||
| 	"github.com/docker/distribution/reference" | 	"github.com/docker/distribution/reference" | ||||||
| @ -357,6 +356,8 @@ func ReadReposMetadata() (RepoCatalogue, error) { | |||||||
| 		bar.Add(1) | 		bar.Add(1) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	fmt.Println() // newline for spinner | ||||||
|  |  | ||||||
| 	return reposMeta, nil | 	return reposMeta, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -464,22 +465,11 @@ func GetRecipeVersions(recipeName string) (RecipeVersions, error) { | |||||||
| 		return versions, err | 		return versions, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	branch, err := gitPkg.GetCurrentBranch(repo) | 	_, err = recipe.CheckoutDefaultBranch(repo, recipeName) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return versions, err | 		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) | 	logrus.Debugf("collected %s for %s", versions, recipeName) | ||||||
|  |  | ||||||
| 	return versions, nil | 	return versions, nil | ||||||
|  | |||||||
| @ -310,72 +310,6 @@ func GetVersionLabelLocal(recipe Recipe) (string, error) { | |||||||
| 	return label, nil | 	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) { | func GetRecipeFeaturesAndCategory(recipeName string) (Features, string, error) { | ||||||
| 	feat := Features{} | 	feat := Features{} | ||||||
|  |  | ||||||
| @ -503,3 +437,102 @@ func GetStringInBetween(str, start, end string) (result string, err error) { | |||||||
|  |  | ||||||
| 	return str[s : s+e], nil | 	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