Skip to content

Commit

Permalink
Pass around prime prompt and output instead of using globals.
Browse files Browse the repository at this point in the history
Also renamed the package from the previous commit.
  • Loading branch information
mitchell-as committed Oct 19, 2023
1 parent 94c1b58 commit c05892c
Show file tree
Hide file tree
Showing 49 changed files with 252 additions and 164 deletions.
7 changes: 3 additions & 4 deletions cmd/state/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
_ "github.com/ActiveState/cli/internal/prompt" // Sets up survey defaults
"github.com/ActiveState/cli/internal/rollbar"
"github.com/ActiveState/cli/internal/runbits/panics"
"github.com/ActiveState/cli/internal/runbits/projectmigration"
"github.com/ActiveState/cli/internal/subshell"
"github.com/ActiveState/cli/internal/svcctl"
cmdletErrors "github.com/ActiveState/cli/pkg/cmdlets/errors"
Expand Down Expand Up @@ -212,13 +211,13 @@ func run(args []string, isInteractive bool, cfg *config.Instance, out output.Out
// Set up prompter
prompter := prompt.New(isInteractive, an)

projectmigration.Register(prompter, out)

// Set up conditional, which accesses a lot of primer data
sshell := subshell.New(cfg)

conditional := constraints.NewPrimeConditional(auth, pj, sshell.Shell())
conditional := constraints.NewPrimeConditional(auth, pj, sshell.Shell(), prompter, out)
project.RegisterConditional(conditional)
project.RegisterPrompt(prompter)
project.RegisterOutput(out)
project.RegisterExpander("mixin", project.NewMixin(auth).Expander)
project.RegisterExpander("secrets", project.NewSecretPromptingExpander(secretsapi.Get(), prompter, cfg, auth))

Expand Down
10 changes: 6 additions & 4 deletions internal/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/runbits/commitid"
"github.com/ActiveState/cli/internal/runbits/commitmediator"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/projectfile"
"github.com/ActiveState/cli/pkg/sysinfo"
Expand Down Expand Up @@ -81,10 +83,10 @@ type projectable interface {
Path() string
Dir() string
URL() string
LegacyCommitID() string // for commitid.GetCompatible
LegacyCommitID() string // for commitmediator.Get
}

func NewPrimeConditional(auth *authentication.Auth, pj projectable, subshellName string) *Conditional {
func NewPrimeConditional(auth *authentication.Auth, pj projectable, subshellName string, prompter prompt.Prompter, out output.Outputer) *Conditional {
var (
pjOwner string
pjName string
Expand All @@ -99,7 +101,7 @@ func NewPrimeConditional(auth *authentication.Auth, pj projectable, subshellName
pjName = pj.Name()
pjNamespace = pj.NamespaceString()
pjURL = pj.URL()
commitID, err := commitid.GetCompatible(pj)
commitID, err := commitmediator.Get(pj, prompter, out)
if err != nil {
multilog.Error("Unable to get local commit: %v", errs.JoinMessage(err))
}
Expand Down
3 changes: 2 additions & 1 deletion internal/events/cmdcall/cmdcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type primeable interface {
primer.Configurer
primer.Analyticer
primer.SvcModeler
primer.Prompter
}

// CmdCall manages dependencies for the handling of events triggered by command
Expand All @@ -41,7 +42,7 @@ func New(p primeable, cmdList string) *CmdCall {
subshell: p.Subshell(),
cmdList: cmdList,
p: p,
scriptrun: scriptrun.New(p.Auth(), p.Output(), p.Subshell(), p.Project(), p.Config(), p.Analytics(), p.SvcModel()),
scriptrun: scriptrun.New(p.Auth(), p.Output(), p.Subshell(), p.Project(), p.Config(), p.Analytics(), p.SvcModel(), p.Prompt()),
}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/globaldefault/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/subshell"
"github.com/ActiveState/cli/internal/subshell/sscommon"
"github.com/ActiveState/cli/internal/svcctl"
Expand Down Expand Up @@ -63,7 +65,7 @@ func Prepare(cfg DefaultConfigurer, shell subshell.SubShell) error {
}

// SetupDefaultActivation sets symlinks in the global bin directory to the currently activated runtime
func SetupDefaultActivation(subshell subshell.SubShell, cfg DefaultConfigurer, runtime *runtime.Runtime, proj *project.Project) error {
func SetupDefaultActivation(subshell subshell.SubShell, cfg DefaultConfigurer, runtime *runtime.Runtime, proj *project.Project, prompter prompt.Prompter, out output.Outputer) error {
logging.Debug("Setting up globaldefault")
if err := Prepare(cfg, subshell); err != nil {
return locale.WrapError(err, "err_globaldefault_prepare", "Could not prepare environment.")
Expand All @@ -79,7 +81,7 @@ func SetupDefaultActivation(subshell subshell.SubShell, cfg DefaultConfigurer, r
return locale.WrapError(err, "err_globaldefault_rtenv", "Could not construct runtime environment variables")
}

target := target.NewProjectTargetCache(proj, storage.GlobalBinDir(), nil, target.TriggerActivate)
target := target.NewProjectTargetCache(proj, storage.GlobalBinDir(), nil, target.TriggerActivate, prompter, out)
execInit := executors.New(BinDir())
if err := execInit.Apply(svcctl.NewIPCSockPathFromGlobals().String(), target, env, exes); err != nil {
return locale.WrapError(err, "err_globaldefault_fw", "Could not set up forwarders")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package commitid
package commitmediator

import (
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/runbits/projectmigration"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/go-openapi/strfmt"
Expand All @@ -17,11 +19,11 @@ type projecter interface {
// GetCompatible returns the given project's commit ID in either the new format (commit file), or
// the old format (activestate.yaml).
// If you require the commit file, use localcommit.Get().
func GetCompatible(proj projecter) (strfmt.UUID, error) {
func Get(proj projecter, prompter prompt.Prompter, out output.Outputer) (strfmt.UUID, error) {
if commitID, err := localcommit.Get(proj.Dir()); err == nil {
return commitID, nil
} else if localcommit.IsFileDoesNotExistError(err) {
if migrated, err := projectmigration.PromptAndMigrate(proj); err == nil && migrated {
if migrated, err := projectmigration.PromptAndMigrate(proj, prompter, out); err == nil && migrated {
return localcommit.Get(proj.Dir())
} else if err != nil {
return "", errs.Wrap(err, "Could not prompt and/or migrate project")
Expand Down
15 changes: 2 additions & 13 deletions internal/runbits/projectmigration/projectmigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,11 @@ type promptable interface {
Confirm(title, message string, defaultChoice *bool) (bool, error)
}

var prompt promptable
var out output.Outputer
var declined bool

func Register(prompt_ promptable, out_ output.Outputer) {
prompt = prompt_
out = out_
}

func PromptAndMigrate(proj projecter) (bool, error) {
if prompt == nil || out == nil {
return false, errs.New("projectmigration.Register() has not been called")
}

func PromptAndMigrate(proj projecter, prompt promptable, out output.Outputer) (bool, error) {
if declined {
return false, nil
return false, nil // already declined; do not prompt again
}

defaultChoice := false
Expand Down
4 changes: 3 additions & 1 deletion internal/runbits/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ActiveState/cli/internal/analytics"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/rtutils"
"github.com/ActiveState/cli/internal/runbits/buildscript"
"github.com/ActiveState/cli/pkg/platform/authentication"
Expand All @@ -24,12 +25,13 @@ func RefreshRuntime(
changed bool,
trigger target.Trigger,
svcm *model.SvcModel,
prompter prompt.Prompter,
) (rerr error) {
_, err := buildscript.Sync(proj, &commitID, out, auth)
if err != nil {
return locale.WrapError(err, "err_update_build_script")
}
target := target.NewProjectTarget(proj, nil, trigger)
target := target.NewProjectTarget(proj, nil, trigger, prompter, out)
isCached := true
rt, err := runtime.New(target, an, svcm, auth)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/runbits/requirements/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (r *RequirementOperation) ExecuteRequirementOperation(requirementName, requ
}

// refresh or install runtime
err = runbits.RefreshRuntime(r.Auth, r.Output, r.Analytics, pj, commitID, true, trigger, r.SvcModel)
err = runbits.RefreshRuntime(r.Auth, r.Output, r.Analytics, pj, commitID, true, trigger, r.SvcModel, r.Prompt)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions internal/runbits/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ActiveState/cli/internal/analytics"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/rtutils"
"github.com/ActiveState/cli/internal/runbits"
"github.com/ActiveState/cli/pkg/platform/authentication"
Expand All @@ -25,10 +26,11 @@ func NewFromProject(
an analytics.Dispatcher,
svcModel *model.SvcModel,
out output.Outputer,
auth *authentication.Auth) (_ *rt.Runtime, rerr error) {
auth *authentication.Auth,
prompter prompt.Prompter) (_ *rt.Runtime, rerr error) {
defer rationalizeError(auth, proj, &rerr)

rti, err := rt.New(target.NewProjectTarget(proj, nil, trigger), an, svcModel, auth)
rti, err := rt.New(target.NewProjectTarget(proj, nil, trigger, prompter, out), an, svcModel, auth)
if err == nil {
return rti, nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/runners/activate/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/ActiveState/cli/internal/process"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/runbits/activation"
"github.com/ActiveState/cli/internal/runbits/commitid"
"github.com/ActiveState/cli/internal/runbits/commitmediator"
"github.com/ActiveState/cli/internal/runbits/findproject"
"github.com/ActiveState/cli/internal/runbits/runtime"
"github.com/ActiveState/cli/internal/subshell"
Expand Down Expand Up @@ -175,15 +175,15 @@ func (r *Activate) Run(params *ActivateParams) (rerr error) {
}
}

rt, err := runtime.NewFromProject(proj, target.TriggerActivate, r.analytics, r.svcModel, r.out, r.auth)
rt, err := runtime.NewFromProject(proj, target.TriggerActivate, r.analytics, r.svcModel, r.out, r.auth, r.prompt)
if err != nil {
return locale.WrapError(err, "err_could_not_activate_venv", "Could not activate project")
}

venv := virtualenvironment.New(rt)

if setDefault {
err := globaldefault.SetupDefaultActivation(r.subshell, r.config, rt, proj)
err := globaldefault.SetupDefaultActivation(r.subshell, r.config, rt, proj, r.prompt, r.out)
if err != nil {
return locale.WrapError(err, "err_activate_default", "Could not make your project always available for use.")
}
Expand All @@ -195,7 +195,7 @@ func (r *Activate) Run(params *ActivateParams) (rerr error) {
}
}

commitID, err := commitid.GetCompatible(proj)
commitID, err := commitmediator.Get(proj, r.prompt, r.out)
if err != nil {
return errs.Wrap(err, "Unable to get local commit")
}
Expand Down
6 changes: 5 additions & 1 deletion internal/runners/checkout/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/primer"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/runbits/runtime"
"github.com/ActiveState/cli/internal/subshell"
"github.com/ActiveState/cli/pkg/cmdlets/checker"
Expand Down Expand Up @@ -36,6 +37,7 @@ type primeable interface {
primer.Configurer
primer.SvcModeler
primer.Analyticer
primer.Prompter
}

type Checkout struct {
Expand All @@ -46,6 +48,7 @@ type Checkout struct {
config *config.Instance
subshell subshell.SubShell
analytics analytics.Dispatcher
prompt prompt.Prompter
}

func NewCheckout(prime primeable) *Checkout {
Expand All @@ -57,6 +60,7 @@ func NewCheckout(prime primeable) *Checkout {
prime.Config(),
prime.Subshell(),
prime.Analytics(),
prime.Prompt(),
}
}

Expand All @@ -77,7 +81,7 @@ func (u *Checkout) Run(params *Params) (rerr error) {
return locale.WrapError(err, "err_project_frompath")
}

rti, err := runtime.NewFromProject(proj, target.TriggerCheckout, u.analytics, u.svcModel, u.out, u.auth)
rti, err := runtime.NewFromProject(proj, target.TriggerCheckout, u.analytics, u.svcModel, u.out, u.auth, u.prompt)
if err != nil {
return locale.WrapError(err, "err_checkout_runtime_new", "Could not checkout this project.")
}
Expand Down
6 changes: 5 additions & 1 deletion internal/runners/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/primer"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/runbits/buildscript"
"github.com/ActiveState/cli/internal/runbits/runtime"
"github.com/ActiveState/cli/pkg/platform/authentication"
Expand All @@ -20,6 +21,7 @@ type primeable interface {
primer.Auther
primer.Analyticer
primer.SvcModeler
primer.Prompter
}

type Commit struct {
Expand All @@ -28,6 +30,7 @@ type Commit struct {
auth *authentication.Auth
analytics analytics.Dispatcher
svcModel *model.SvcModel
prompt prompt.Prompter
}

func New(p primeable) *Commit {
Expand All @@ -37,6 +40,7 @@ func New(p primeable) *Commit {
auth: p.Auth(),
analytics: p.Analytics(),
svcModel: p.SvcModel(),
prompt: p.Prompt(),
}
}

Expand All @@ -54,7 +58,7 @@ func (c *Commit) Run() error {
}

trigger := target.TriggerCommit
rti, err := runtime.NewFromProject(c.proj, trigger, c.analytics, c.svcModel, c.out, c.auth)
rti, err := runtime.NewFromProject(c.proj, trigger, c.analytics, c.svcModel, c.out, c.auth, c.prompt)
if err != nil {
return locale.WrapInputError(
err, "err_commit_runtime_new",
Expand Down
15 changes: 9 additions & 6 deletions internal/runners/cve/cve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/primer"
"github.com/ActiveState/cli/internal/runbits/commitid"
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/runbits/commitmediator"
medmodel "github.com/ActiveState/cli/pkg/platform/api/mediator/model"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
"github.com/ActiveState/cli/pkg/project"
)

type Cve struct {
proj *project.Project
auth *authentication.Auth
out output.Outputer
proj *project.Project
auth *authentication.Auth
out output.Outputer
prompt prompt.Prompter
}

type outputData struct {
Expand Down Expand Up @@ -47,10 +49,11 @@ type primeable interface {
primer.Projecter
primer.Auther
primer.Outputer
primer.Prompter
}

func NewCve(prime *primer.Values) *Cve {
return &Cve{prime.Project(), prime.Auth(), prime.Output()}
return &Cve{prime.Project(), prime.Auth(), prime.Output(), prime.Prompt()}
}

func (c *Cve) Run() error {
Expand All @@ -66,7 +69,7 @@ func (c *Cve) Run() error {
)
}

commitID, err := commitid.GetCompatible(c.proj)
commitID, err := commitmediator.Get(c.proj, c.prompt, c.out)
if err != nil {
return errs.Wrap(err, "Could not get local commit")
}
Expand Down
Loading

0 comments on commit c05892c

Please sign in to comment.