github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/eval/eval.go (about)

     1  package eval
     2  
     3  import (
     4  	"github.com/ActiveState/cli/internal/constants"
     5  	"github.com/ActiveState/cli/internal/errs"
     6  	"github.com/ActiveState/cli/internal/locale"
     7  	"github.com/ActiveState/cli/internal/output"
     8  	"github.com/ActiveState/cli/internal/primer"
     9  	"github.com/ActiveState/cli/internal/runbits/rationalize"
    10  	"github.com/ActiveState/cli/pkg/localcommit"
    11  	"github.com/ActiveState/cli/pkg/platform/authentication"
    12  	"github.com/ActiveState/cli/pkg/platform/model/buildplanner"
    13  	"github.com/ActiveState/cli/pkg/project"
    14  )
    15  
    16  type primeable interface {
    17  	primer.Outputer
    18  	primer.Auther
    19  	primer.Projecter
    20  }
    21  
    22  type Params struct {
    23  	Target string
    24  }
    25  
    26  type Eval struct {
    27  	out     output.Outputer
    28  	project *project.Project
    29  	auth    *authentication.Auth
    30  }
    31  
    32  func New(p primeable) *Eval {
    33  	return &Eval{
    34  		out:     p.Output(),
    35  		project: p.Project(),
    36  		auth:    p.Auth(),
    37  	}
    38  }
    39  
    40  func (e *Eval) Run(params *Params) (rerr error) {
    41  	defer rationalizeError(&rerr)
    42  
    43  	e.out.Notice(output.Title(locale.Tl("title_eval", "Evaluating target: {{.V0}}", params.Target)))
    44  
    45  	if !e.auth.Authenticated() {
    46  		return rationalize.ErrNotAuthenticated
    47  	}
    48  
    49  	if e.project == nil {
    50  		return rationalize.ErrNoProject
    51  	}
    52  
    53  	commitID, err := localcommit.Get(e.project.Dir())
    54  	if err != nil {
    55  		return errs.Wrap(err, "Unable to get commit ID")
    56  	}
    57  
    58  	pg := output.StartSpinner(e.out, locale.Tl("progress_eval", "Evaluating ... "), constants.TerminalAnimationInterval)
    59  	defer func() {
    60  		if pg != nil {
    61  			pg.Stop(locale.T("progress_fail") + "\n")
    62  		}
    63  	}()
    64  
    65  	bp := buildplanner.NewBuildPlannerModel(e.auth)
    66  	if err := bp.WaitForBuild(commitID, e.project.Owner(), e.project.Name(), &params.Target); err != nil {
    67  		return locale.WrapError(err, "err_eval", "Failed to evaluate target '{{.V0}}'", params.Target)
    68  	}
    69  
    70  	pg.Stop("OK")
    71  	pg = nil
    72  
    73  	e.out.Notice(locale.Tl("notice_eval_success", "Target successfully evaluated"))
    74  
    75  	return nil
    76  }