forked from toolshed/abra
@ -13,13 +13,20 @@ import (
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
)
|
||||
|
||||
// Ensure makes sure the recipe exists, is up to date and has the latest version checked out.
|
||||
func (r Recipe) Ensure(chaos bool, offline bool) error {
|
||||
type EnsureContext struct {
|
||||
Chaos bool
|
||||
Offline bool
|
||||
IgnoreEnv bool
|
||||
}
|
||||
|
||||
// Ensure makes sure the recipe exists, is up to date and has the specific
|
||||
// version checked out.
|
||||
func (r Recipe) Ensure(ctx EnsureContext) error {
|
||||
if err := r.EnsureExists(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if chaos {
|
||||
if ctx.Chaos {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -27,15 +34,16 @@ func (r Recipe) Ensure(chaos bool, offline bool) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !offline {
|
||||
if !ctx.Offline {
|
||||
if err := r.EnsureUpToDate(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if r.Version != "" {
|
||||
log.Debugf("ensuring version %s", r.Version)
|
||||
if _, err := r.EnsureVersion(r.Version); err != nil {
|
||||
if r.EnvVersion != "" && !ctx.IgnoreEnv {
|
||||
log.Debugf("ensuring env version %s", r.EnvVersion)
|
||||
|
||||
if _, err := r.EnsureVersion(r.EnvVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -65,6 +73,41 @@ func (r Recipe) EnsureExists() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsChaosCommit determines if a version sttring is a chaos commit or not.
|
||||
func (r Recipe) IsChaosCommit(version string) (bool, error) {
|
||||
isChaosCommit := false
|
||||
|
||||
if err := gitPkg.EnsureGitRepo(r.Dir); err != nil {
|
||||
return isChaosCommit, err
|
||||
}
|
||||
|
||||
repo, err := git.PlainOpen(r.Dir)
|
||||
if err != nil {
|
||||
return isChaosCommit, err
|
||||
}
|
||||
|
||||
tags, err := repo.Tags()
|
||||
if err != nil {
|
||||
return isChaosCommit, err
|
||||
}
|
||||
|
||||
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 isChaosCommit, err
|
||||
}
|
||||
|
||||
if tagRef.String() == "" {
|
||||
isChaosCommit = true
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// EnsureVersion checks whether a specific version exists for a recipe.
|
||||
func (r Recipe) EnsureVersion(version string) (bool, error) {
|
||||
isChaosCommit := false
|
||||
|
Reference in New Issue
Block a user