From b3810dca4eaf5b1d46e9fc1b74a793a7c5dd8b5d Mon Sep 17 00:00:00 2001 From: mitchell Date: Fri, 13 Sep 2024 21:54:34 -0400 Subject: [PATCH] Make checkoutinfo a primer property and use it to set commit IDs, and initialize and update build scripts. --- internal/events/cmdcall/cmdcall.go | 1 + internal/migrator/migrator.go | 7 +- internal/primer/primer.go | 34 ++-- internal/runbits/buildplanner/buildplanner.go | 13 +- internal/runbits/buildscript/commitid.go | 36 ---- internal/runbits/buildscript/file.go | 105 ---------- internal/runbits/checker/checker.go | 11 +- internal/runbits/checkout/checkout.go | 24 ++- internal/runbits/commits_runbit/time.go | 7 +- internal/runbits/runtime/rationalize.go | 2 +- .../runtime/requirements/requirements.go | 10 +- internal/runbits/runtime/runtime.go | 7 +- internal/runners/activate/activate.go | 6 +- internal/runners/artifacts/artifacts.go | 15 +- internal/runners/artifacts/download.go | 8 +- internal/runners/branch/add.go | 9 +- internal/runners/branch/list.go | 1 + internal/runners/checkout/checkout.go | 4 +- internal/runners/commit/commit.go | 5 +- internal/runners/cve/cve.go | 11 +- internal/runners/deploy/deploy.go | 11 +- .../runners/deploy/uninstall/uninstall.go | 5 +- internal/runners/eval/eval.go | 11 +- internal/runners/exec/exec.go | 1 + internal/runners/export/buildplan.go | 2 +- internal/runners/export/export.go | 1 + internal/runners/hello/hello_example.go | 15 +- internal/runners/history/history.go | 11 +- internal/runners/initialize/init.go | 12 +- internal/runners/languages/install.go | 1 + internal/runners/languages/languages.go | 9 +- internal/runners/manifest/manifest.go | 7 +- internal/runners/packages/import.go | 6 +- internal/runners/packages/info.go | 10 +- internal/runners/packages/install.go | 2 +- internal/runners/packages/list.go | 13 +- internal/runners/packages/search.go | 15 +- internal/runners/packages/uninstall.go | 2 +- internal/runners/platforms/add.go | 1 + internal/runners/platforms/list.go | 9 +- internal/runners/prepare/prepare.go | 6 +- internal/runners/pull/pull.go | 11 +- internal/runners/push/push.go | 18 +- internal/runners/refresh/refresh.go | 8 +- internal/runners/reset/reset.go | 13 +- internal/runners/revert/revert.go | 9 +- internal/runners/run/run.go | 3 +- internal/runners/shell/shell.go | 4 +- internal/runners/show/show.go | 17 +- internal/runners/swtch/switch.go | 16 +- internal/runners/upgrade/upgrade.go | 7 +- internal/runners/use/use.go | 4 +- internal/scriptrun/scriptrun.go | 1 + pkg/checkoutinfo/checkoutinfo.go | 181 ++++++++++++------ pkg/projectfile/projectfile.go | 1 + pkg/runtime_helpers/helpers.go | 11 +- 56 files changed, 347 insertions(+), 423 deletions(-) delete mode 100644 internal/runbits/buildscript/commitid.go diff --git a/internal/events/cmdcall/cmdcall.go b/internal/events/cmdcall/cmdcall.go index 7e8d068016..2e2e56e27f 100644 --- a/internal/events/cmdcall/cmdcall.go +++ b/internal/events/cmdcall/cmdcall.go @@ -21,6 +21,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } // CmdCall manages dependencies for the handling of events triggered by command diff --git a/internal/migrator/migrator.go b/internal/migrator/migrator.go index bf90cad3ec..e2723edb66 100644 --- a/internal/migrator/migrator.go +++ b/internal/migrator/migrator.go @@ -1,13 +1,13 @@ package migrator import ( - "path/filepath" + "github.com/go-openapi/strfmt" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" - "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/projectfile" ) @@ -26,7 +26,8 @@ func NewMigrator(auth *authentication.Auth, cfg *config.Instance) projectfile.Mi // have completed. Ensure you roll back any partial updates in the case of an error as they will need to be attempted again. case 0: logging.Debug("Attempting to create buildscript") - if err := buildscript_runbit.Initialize(filepath.Dir(project.Path()), project.Owner(), project.Name(), project.BranchName(), project.LegacyCommitID(), auth, cfg); err != nil { + info := checkoutinfo.New(auth, cfg, project) + if err := info.InitializeBuildScript(strfmt.UUID(project.LegacyCommitID())); err != nil { return v, errs.Wrap(err, "Failed to initialize buildscript") } } diff --git a/internal/primer/primer.go b/internal/primer/primer.go index 8ad3854415..4650b87528 100644 --- a/internal/primer/primer.go +++ b/internal/primer/primer.go @@ -12,6 +12,7 @@ import ( "github.com/ActiveState/cli/internal/prompt" "github.com/ActiveState/cli/internal/subshell" "github.com/ActiveState/cli/internal/svcctl" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" @@ -19,17 +20,18 @@ import ( ) type Values struct { - project *project.Project - projectfile *projectfile.Project - output output.Outputer - auth *authentication.Auth - prompt prompt.Prompter - subshell subshell.SubShell - conditional *constraints.Conditional - config *config.Instance - ipComm svcctl.IPCommunicator - svcModel *model.SvcModel - analytics analytics.Dispatcher + project *project.Project + projectfile *projectfile.Project + output output.Outputer + auth *authentication.Auth + prompt prompt.Prompter + subshell subshell.SubShell + conditional *constraints.Conditional + config *config.Instance + ipComm svcctl.IPCommunicator + svcModel *model.SvcModel + analytics analytics.Dispatcher + checkoutinfo *checkoutinfo.CheckoutInfo } func New(values ...any) *Values { @@ -65,12 +67,14 @@ func New(values ...any) *Values { } } } + result.checkoutinfo = checkoutinfo.New(result.auth, result.config, result.project) return result } func (v *Values) SetProject(p *project.Project) { v.project = p v.projectfile = p.Source() + v.checkoutinfo = checkoutinfo.New(v.auth, v.config, p) } type Projecter interface { @@ -118,6 +122,10 @@ type Conditioner interface { Conditional() *constraints.Conditional } +type CheckoutInfoer interface { + CheckoutInfo() *checkoutinfo.CheckoutInfo +} + func (v *Values) Project() *project.Project { return v.project } @@ -161,3 +169,7 @@ func (v *Values) Config() *config.Instance { func (v *Values) Analytics() analytics.Dispatcher { return v.analytics } + +func (v *Values) CheckoutInfo() *checkoutinfo.CheckoutInfo { + return v.checkoutinfo +} diff --git a/internal/runbits/buildplanner/buildplanner.go b/internal/runbits/buildplanner/buildplanner.go index e0ebceba2c..8c61ab1c5b 100644 --- a/internal/runbits/buildplanner/buildplanner.go +++ b/internal/runbits/buildplanner/buildplanner.go @@ -1,15 +1,14 @@ package buildplanner import ( - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/rtutils/ptr" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/pkg/buildplan" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/api/buildplanner/request" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" @@ -45,7 +44,7 @@ func GetCommit( target string, auth *authentication.Auth, out output.Outputer, - cfg *config.Instance) (commit *bpModel.Commit, rerr error) { + info *checkoutinfo.CheckoutInfo) (commit *bpModel.Commit, rerr error) { if pj == nil && !namespace.IsValid() { return nil, rationalize.ErrNoProject } @@ -78,7 +77,7 @@ func GetCommit( switch { // Return the buildplan from this runtime. case !namespaceProvided && !commitIdProvided: - localCommitID, err := buildscript_runbit.CommitID(pj.Path(), cfg) + localCommitID, err := info.CommitID() if err != nil { return nil, errs.Wrap(err, "Could not get commit ID") } @@ -155,7 +154,7 @@ func GetCommit( owner = pj.Owner() name = pj.Name() nsString = pj.NamespaceString() - commitID, err := buildscript_runbit.CommitID(pj.Path(), cfg) + commitID, err := info.CommitID() if err != nil { return nil, errs.Wrap(err, "Could not get commit ID") } @@ -183,8 +182,8 @@ func GetBuildPlan( target string, auth *authentication.Auth, out output.Outputer, - cfg *config.Instance) (bp *buildplan.BuildPlan, rerr error) { - commit, err := GetCommit(pj, namespace, commitID, target, auth, out, cfg) + info *checkoutinfo.CheckoutInfo) (bp *buildplan.BuildPlan, rerr error) { + commit, err := GetCommit(pj, namespace, commitID, target, auth, out, info) if err != nil { return nil, errs.Wrap(err, "Could not get commit") } diff --git a/internal/runbits/buildscript/commitid.go b/internal/runbits/buildscript/commitid.go deleted file mode 100644 index eb8426337f..0000000000 --- a/internal/runbits/buildscript/commitid.go +++ /dev/null @@ -1,36 +0,0 @@ -package buildscript_runbit - -import ( - "errors" - - "github.com/go-openapi/strfmt" - - "github.com/ActiveState/cli/internal/constants" - "github.com/ActiveState/cli/internal/errs" - "github.com/ActiveState/cli/pkg/buildscript" - "github.com/ActiveState/cli/pkg/checkoutinfo" -) - -func CommitID(path string, cfg configurer) (strfmt.UUID, error) { - script, err := buildscript.New() - if err != nil { - return "", errs.Wrap(err, "Could not create build script") - } - - project, err := checkoutinfo.GetProject(path) - if err != nil { - return "", errs.Wrap(err, "Could not get project") - } - script.SetProjectURL(project) // script.CommitID() is extracted from project URL - - if cfg.GetBool(constants.OptinBuildscriptsConfig) { - if script2, err := ScriptFromProject(path); err == nil { - script = script2 - } else if !errors.Is(err, ErrBuildscriptNotExist) { - return "", errs.Wrap(err, "Could not get build script") - } - // ErrBuildscriptNotExist will fall back on activestate.yaml - } - - return script.CommitID() -} diff --git a/internal/runbits/buildscript/file.go b/internal/runbits/buildscript/file.go index 53263d9a3b..644172c09d 100644 --- a/internal/runbits/buildscript/file.go +++ b/internal/runbits/buildscript/file.go @@ -8,18 +8,9 @@ import ( "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/fileutils" - "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/pkg/buildscript" - "github.com/ActiveState/cli/pkg/checkoutinfo" - "github.com/ActiveState/cli/pkg/platform/authentication" - "github.com/ActiveState/cli/pkg/platform/model/buildplanner" ) -// configurer is here until buildscripts are no longer walled behind an opt-in config option. -type configurer interface { - GetBool(string) bool -} - var ErrBuildscriptNotExist = errors.New("Build script does not exist") func ScriptFromProject(projectDir string) (*buildscript.BuildScript, error) { @@ -38,101 +29,5 @@ func ScriptFromProject(projectDir string) (*buildscript.BuildScript, error) { return nil, errs.Wrap(err, "Could not unmarshal build script") } - // Synchronize any changes with activestate.yaml. - err = checkoutinfo.UpdateProject(script, path) - if err != nil { - return nil, errs.Wrap(err, "Could not update project file") - } - return script, nil } - -func Initialize(path, owner, project, branch, commitID string, auth *authentication.Auth, cfg configurer) error { - if cfg.GetBool(constants.OptinBuildscriptsConfig) { - _, err := ScriptFromProject(path) - if err == nil { - return nil // nothing to do, buildscript already exists - } - if !errors.Is(err, os.ErrNotExist) { - return errs.Wrap(err, "Could not read project build script") - } - } - - buildplanner := buildplanner.NewBuildPlannerModel(auth) - script, err := buildplanner.GetBuildScript(owner, project, branch, commitID) - if err != nil { - return errs.Wrap(err, "Unable to get the remote build script") - } - - if !cfg.GetBool(constants.OptinBuildscriptsConfig) { - // Just update the project file with the new commit ID. - err = checkoutinfo.UpdateProject(script, path) - if err != nil { - return errs.Wrap(err, "Unable to update project file") - } - return nil - } - - scriptBytes, err := script.Marshal() - if err != nil { - return errs.Wrap(err, "Unable to marshal build script") - } - - scriptPath := filepath.Join(path, constants.BuildScriptFileName) - logging.Debug("Initializing build script at %s", scriptPath) - err = fileutils.WriteFile(scriptPath, scriptBytes) - if err != nil { - return errs.Wrap(err, "Unable to write build script") - } - - return nil -} - -func Update(path string, newScript *buildscript.BuildScript, cfg configurer) error { - if !cfg.GetBool(constants.OptinBuildscriptsConfig) { - // Just update the activestate.yaml file (e.g. with the new commit ID). - // Eventually the buildscript will be the one source of truth. - return checkoutinfo.UpdateProject(newScript, path) - } - - script, err := ScriptFromProject(path) - if err != nil { - return errs.Wrap(err, "Could not read build script") - } - - equals, err := script.Equals(newScript) - if err != nil { - return errs.Wrap(err, "Could not compare build script") - } - if script != nil && equals { - return nil // no changes to write - } - - sb, err := newScript.Marshal() - if err != nil { - return errs.Wrap(err, "Could not marshal build script") - } - - logging.Debug("Writing build script") - if err := fileutils.WriteFile(filepath.Join(path, constants.BuildScriptFileName), sb); err != nil { - return errs.Wrap(err, "Could not write build script to file") - } - - // Synchronize changes with activestate.yaml. - err = checkoutinfo.UpdateProject(newScript, path) - if err != nil { - return errs.Wrap(err, "Could not update project file") - } - - return nil -} - -// Remove removes an existing buildscript if it exists. -// This is primarily for updating an outdated buildscript. -func Remove(path string) error { - bsPath := filepath.Join(path, constants.BuildScriptFileName) - if !fileutils.TargetExists(bsPath) { - return nil - } - return os.Remove(bsPath) -} diff --git a/internal/runbits/checker/checker.go b/internal/runbits/checker/checker.go index 4235b2e321..433680dbba 100644 --- a/internal/runbits/checker/checker.go +++ b/internal/runbits/checker/checker.go @@ -4,12 +4,11 @@ import ( "errors" "strconv" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" @@ -17,8 +16,8 @@ import ( // RunCommitsBehindNotifier checks for the commits behind count based on the // provided project and displays the results to the user in a helpful manner. -func RunCommitsBehindNotifier(p *project.Project, out output.Outputer, auth *authentication.Auth, cfg *config.Instance) { - count, err := CommitsBehind(p, auth, cfg) +func RunCommitsBehindNotifier(p *project.Project, out output.Outputer, auth *authentication.Auth, info *checkoutinfo.CheckoutInfo) { + count, err := CommitsBehind(p, auth, info) if err != nil { if errors.Is(err, model.ErrCommitCountUnknowable) { out.Notice(output.Title(locale.Tr("runtime_update_notice_unknown_count"))) @@ -36,7 +35,7 @@ func RunCommitsBehindNotifier(p *project.Project, out output.Outputer, auth *aut } } -func CommitsBehind(p *project.Project, auth *authentication.Auth, cfg *config.Instance) (int, error) { +func CommitsBehind(p *project.Project, auth *authentication.Auth, info *checkoutinfo.CheckoutInfo) (int, error) { if p.IsHeadless() { return 0, nil } @@ -50,7 +49,7 @@ func CommitsBehind(p *project.Project, auth *authentication.Auth, cfg *config.In return 0, locale.NewError("err_latest_commit", "Latest commit ID is nil") } - commitID, err := buildscript_runbit.CommitID(p.Dir(), cfg) + commitID, err := info.CommitID() if err != nil { return 0, errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runbits/checkout/checkout.go b/internal/runbits/checkout/checkout.go index a974eea130..03fcbedbca 100644 --- a/internal/runbits/checkout/checkout.go +++ b/internal/runbits/checkout/checkout.go @@ -4,10 +4,8 @@ import ( "path/filepath" "github.com/ActiveState/cli/internal/analytics" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/primer" - "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/go-openapi/strfmt" "github.com/ActiveState/cli/internal/constants" @@ -27,17 +25,21 @@ import ( type primeable interface { primer.Outputer primer.Analyticer - primer.Configurer primer.Auther + primer.CheckoutInfoer + primer.Projecter } // Checkout will checkout the given platform project at the given path // This includes cloning an associated repository and creating the activestate.yaml // It does not activate any environment type Checkout struct { - repo git.Repository + repo git.Repository + prime primeable + // The remainder is redundant with the above. Refactoring this will follow in a later story so as not to blow + // up the one that necessitates adding the primer at this level. + // https://activestatef.atlassian.net/browse/DX-2869 output.Outputer - config *config.Instance analytics analytics.Dispatcher auth *authentication.Auth } @@ -53,7 +55,7 @@ func (e errCommitDoesNotBelong) Error() string { var errNoCommitID = errs.New("commitID is nil") func New(repo git.Repository, prime primeable) *Checkout { - return &Checkout{repo, prime.Output(), prime.Config(), prime.Analytics(), prime.Auth()} + return &Checkout{repo, prime, prime.Output(), prime.Analytics(), prime.Auth()} } func (r *Checkout) Run(ns *project.Namespaced, branchName, cachePath, targetPath string, noClone, bareCheckout bool) (_ string, rerr error) { @@ -102,8 +104,14 @@ func (r *Checkout) Run(ns *project.Namespaced, branchName, cachePath, targetPath return "", errs.Wrap(err, "Could not create project files") } - if err := buildscript_runbit.Initialize(path, owner, proj, branchName, commitID.String(), r.auth, r.config); err != nil { - return "", errs.Wrap(err, "Unable to initialize buildscript") + pj, err := project.FromPath(path) + if err != nil { + return "", errs.Wrap(err, "Could not read created project") + } + r.prime.SetProject(pj) + + if err := r.prime.CheckoutInfo().InitializeBuildScript(*commitID); err != nil { + return "", errs.Wrap(err, "Unable to initialize build script") } return path, nil diff --git a/internal/runbits/commits_runbit/time.go b/internal/runbits/commits_runbit/time.go index 2b7002673b..c28f0c9655 100644 --- a/internal/runbits/commits_runbit/time.go +++ b/internal/runbits/commits_runbit/time.go @@ -4,9 +4,8 @@ import ( "time" "github.com/ActiveState/cli/internal/captain" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" @@ -40,14 +39,14 @@ func ExpandTime(ts *captain.TimeValue, auth *authentication.Auth) (time.Time, er // ExpandTimeForProject is the same as ExpandTime except that it ensures the returned time is either the same or // later than that of the most recent commit. -func ExpandTimeForProject(ts *captain.TimeValue, auth *authentication.Auth, proj *project.Project, cfg *config.Instance) (time.Time, error) { +func ExpandTimeForProject(ts *captain.TimeValue, auth *authentication.Auth, proj *project.Project, info *checkoutinfo.CheckoutInfo) (time.Time, error) { timestamp, err := ExpandTime(ts, auth) if err != nil { return time.Time{}, errs.Wrap(err, "Unable to expand time") } if proj != nil { - commitID, err := buildscript_runbit.CommitID(proj.Dir(), cfg) + commitID, err := info.CommitID() if err != nil { return time.Time{}, errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runbits/runtime/rationalize.go b/internal/runbits/runtime/rationalize.go index 650b9cf0d0..4090a6c600 100644 --- a/internal/runbits/runtime/rationalize.go +++ b/internal/runbits/runtime/rationalize.go @@ -8,7 +8,7 @@ import ( "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/graph" "github.com/ActiveState/cli/internal/locale" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/pkg/platform/api" bpResp "github.com/ActiveState/cli/pkg/platform/api/buildplanner/response" diff --git a/internal/runbits/runtime/requirements/requirements.go b/internal/runbits/runtime/requirements/requirements.go index 95a2b7e17d..6206b87f8e 100644 --- a/internal/runbits/runtime/requirements/requirements.go +++ b/internal/runbits/runtime/requirements/requirements.go @@ -21,7 +21,6 @@ import ( "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/prompt" "github.com/ActiveState/cli/internal/rtutils/ptr" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/cves" "github.com/ActiveState/cli/internal/runbits/dependencies" "github.com/ActiveState/cli/internal/runbits/rationalize" @@ -75,6 +74,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } func NewRequirementOperation(prime primeable) *RequirementOperation { @@ -161,7 +161,7 @@ func (r *RequirementOperation) ExecuteRequirementOperation(ts *time.Time, requir return errs.Wrap(err, "Could not validate packages") } - parentCommitID, err := buildscript_runbit.CommitID(r.Project.Dir(), r.Config) + parentCommitID, err := r.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -351,7 +351,7 @@ func (r *RequirementOperation) resolveNamespace(ts *time.Time, requirement *Requ if requirement.NamespaceType != nil { switch *requirement.NamespaceType { case model.NamespacePackage, model.NamespaceBundle: - commitID, err := buildscript_runbit.CommitID(r.Project.Dir(), r.Config) + commitID, err := r.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -532,12 +532,12 @@ func (r *RequirementOperation) resolveRequirement(requirement *Requirement) erro } func (r *RequirementOperation) updateBuildScript(script *buildscript.BuildScript) error { - err := buildscript_runbit.Update(r.Project.Dir(), script, r.Config) + err := r.prime.CheckoutInfo().UpdateBuildScript(script) if err != nil { if r.Config.GetBool(constants.OptinBuildscriptsConfig) { return locale.WrapError(err, "err_update_build_script") } else { - // Update() only tried to update the commit ID if buildscripts are disabled. + // UpdateBuildScript() only tried to update the commit ID if buildscripts are disabled. return locale.WrapError(err, "err_package_update_commit_id") } } diff --git a/internal/runbits/runtime/runtime.go b/internal/runbits/runtime/runtime.go index 06a4014fd4..a60f52a3b0 100644 --- a/internal/runbits/runtime/runtime.go +++ b/internal/runbits/runtime/runtime.go @@ -17,7 +17,7 @@ import ( "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/rtutils" "github.com/ActiveState/cli/internal/rtutils/ptr" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/checkout" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/internal/runbits/runtime/progress" @@ -97,6 +97,7 @@ type primeable interface { primer.Configurer primer.SvcModeler primer.Analyticer + primer.CheckoutInfoer } func Update( @@ -139,7 +140,7 @@ func Update( commitID = opts.Commit.CommitID } if commitID == "" { - commitID, err = buildscript_runbit.CommitID(proj.Dir(), prime.Config()) + commitID, err = prime.CheckoutInfo().CommitID() if err != nil { return nil, errs.Wrap(err, "Failed to get commit ID") } @@ -161,7 +162,7 @@ func Update( } }() - rtHash, err := runtime_helpers.Hash(proj, &commitID, prime.Config()) + rtHash, err := runtime_helpers.Hash(proj, &commitID, prime.CheckoutInfo()) if err != nil { ah.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeCache, nil) return nil, errs.Wrap(err, "Failed to get runtime hash") diff --git a/internal/runners/activate/activate.go b/internal/runners/activate/activate.go index bf02e4e458..d5fe84ca3a 100644 --- a/internal/runners/activate/activate.go +++ b/internal/runners/activate/activate.go @@ -21,7 +21,6 @@ import ( "github.com/ActiveState/cli/internal/process" "github.com/ActiveState/cli/internal/prompt" "github.com/ActiveState/cli/internal/runbits/activation" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/checkout" "github.com/ActiveState/cli/internal/runbits/findproject" "github.com/ActiveState/cli/internal/runbits/git" @@ -66,6 +65,7 @@ type primeable interface { primer.Configurer primer.SvcModeler primer.Analyticer + primer.CheckoutInfoer } func NewActivate(prime primeable) *Activate { @@ -146,7 +146,7 @@ func (r *Activate) Run(params *ActivateParams) (rerr error) { } if proj != nil { - commitID, err := buildscript_runbit.CommitID(proj.Dir(), r.config) + commitID, err := r.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -196,7 +196,7 @@ func (r *Activate) Run(params *ActivateParams) (rerr error) { } } - commitID, err := buildscript_runbit.CommitID(proj.Dir(), r.config) + commitID, err := r.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/artifacts/artifacts.go b/internal/runners/artifacts/artifacts.go index 8885ef9c12..fdf7ab975c 100644 --- a/internal/runners/artifacts/artifacts.go +++ b/internal/runners/artifacts/artifacts.go @@ -8,13 +8,13 @@ import ( "strings" "github.com/ActiveState/cli/internal/analytics" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" buildplanner_runbit "github.com/ActiveState/cli/internal/runbits/buildplanner" "github.com/ActiveState/cli/pkg/buildplan" + "github.com/ActiveState/cli/pkg/checkoutinfo" bpResp "github.com/ActiveState/cli/pkg/platform/api/buildplanner/response" "github.com/ActiveState/cli/pkg/platform/api/buildplanner/types" "github.com/ActiveState/cli/pkg/platform/authentication" @@ -29,8 +29,8 @@ type primeable interface { primer.Auther primer.Projecter primer.SvcModeler - primer.Configurer primer.Analyticer + primer.CheckoutInfoer } type Params struct { @@ -41,18 +41,13 @@ type Params struct { Full bool } -type Configurable interface { - GetString(key string) string - GetBool(key string) bool -} - type Artifacts struct { out output.Outputer project *project.Project analytics analytics.Dispatcher svcModel *model.SvcModel auth *authentication.Auth - config *config.Instance + info *checkoutinfo.CheckoutInfo } type StructuredOutput struct { @@ -87,8 +82,8 @@ func New(p primeable) *Artifacts { project: p.Project(), auth: p.Auth(), svcModel: p.SvcModel(), - config: p.Config(), analytics: p.Analytics(), + info: p.CheckoutInfo(), } } @@ -116,7 +111,7 @@ func (b *Artifacts) Run(params *Params) (rerr error) { } bp, err := buildplanner_runbit.GetBuildPlan( - b.project, params.Namespace, params.CommitID, params.Target, b.auth, b.out, b.config) + b.project, params.Namespace, params.CommitID, params.Target, b.auth, b.out, b.info) if err != nil { return errs.Wrap(err, "Could not get buildplan") } diff --git a/internal/runners/artifacts/download.go b/internal/runners/artifacts/download.go index 4af6fd97c6..93d1aee5da 100644 --- a/internal/runners/artifacts/download.go +++ b/internal/runners/artifacts/download.go @@ -11,7 +11,6 @@ import ( "strings" "github.com/ActiveState/cli/internal/analytics" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/fileutils" "github.com/ActiveState/cli/internal/httputil" @@ -19,6 +18,7 @@ import ( "github.com/ActiveState/cli/internal/output" buildplanner_runbit "github.com/ActiveState/cli/internal/runbits/buildplanner" "github.com/ActiveState/cli/pkg/buildplan" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/api/buildplanner/request" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" @@ -40,7 +40,7 @@ type Download struct { analytics analytics.Dispatcher svcModel *model.SvcModel auth *authentication.Auth - config *config.Instance + info *checkoutinfo.CheckoutInfo } func NewDownload(prime primeable) *Download { @@ -50,7 +50,7 @@ func NewDownload(prime primeable) *Download { analytics: prime.Analytics(), svcModel: prime.SvcModel(), auth: prime.Auth(), - config: prime.Config(), + info: prime.CheckoutInfo(), } } @@ -92,7 +92,7 @@ func (d *Download) Run(params *DownloadParams) (rerr error) { } bp, err := buildplanner_runbit.GetBuildPlan( - d.project, params.Namespace, params.CommitID, target, d.auth, d.out, d.config) + d.project, params.Namespace, params.CommitID, target, d.auth, d.out, d.info) if err != nil { return errs.Wrap(err, "Could not get build plan map") } diff --git a/internal/runners/branch/add.go b/internal/runners/branch/add.go index 2410f96bc6..b46b510196 100644 --- a/internal/runners/branch/add.go +++ b/internal/runners/branch/add.go @@ -1,13 +1,12 @@ package branch import ( - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" @@ -17,7 +16,7 @@ type Add struct { out output.Outputer project *project.Project auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } type AddParams struct { @@ -29,7 +28,7 @@ func NewAdd(prime primeable) *Add { out: prime.Output(), project: prime.Project(), auth: prime.Auth(), - cfg: prime.Config(), + info: prime.CheckoutInfo(), } } @@ -56,7 +55,7 @@ func (a *Add) Run(params AddParams) error { return locale.WrapError(err, "err_fetch_branch", "", localBranch) } - commitID, err := buildscript_runbit.CommitID(a.project.Dir(), a.cfg) + commitID, err := a.info.CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/branch/list.go b/internal/runners/branch/list.go index e60fdad106..b02cb00b3f 100644 --- a/internal/runners/branch/list.go +++ b/internal/runners/branch/list.go @@ -17,6 +17,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } type List struct { diff --git a/internal/runners/checkout/checkout.go b/internal/runners/checkout/checkout.go index b156aea560..2f1b637d84 100644 --- a/internal/runners/checkout/checkout.go +++ b/internal/runners/checkout/checkout.go @@ -15,7 +15,6 @@ import ( "github.com/ActiveState/cli/internal/osutils" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/checkout" "github.com/ActiveState/cli/internal/runbits/cves" "github.com/ActiveState/cli/internal/runbits/dependencies" @@ -48,6 +47,7 @@ type primeable interface { primer.SvcModeler primer.Analyticer primer.Projecter + primer.CheckoutInfoer } type Checkout struct { @@ -142,7 +142,7 @@ func (u *Checkout) Run(params *Params) (rerr error) { var buildPlan *buildplan.BuildPlan rtOpts := []runtime_runbit.SetOpt{} if archive == nil { - commitID, err := buildscript_runbit.CommitID(proj.Path(), u.config) + commitID, err := u.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Could not get commit ID") } diff --git a/internal/runners/commit/commit.go b/internal/runners/commit/commit.go index edbe5fc698..cef5c2883c 100644 --- a/internal/runners/commit/commit.go +++ b/internal/runners/commit/commit.go @@ -8,7 +8,7 @@ import ( "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/cves" "github.com/ActiveState/cli/internal/runbits/dependencies" "github.com/ActiveState/cli/internal/runbits/rationalize" @@ -24,6 +24,7 @@ type primeable interface { primer.SvcModeler primer.Configurer primer.Prompter + primer.CheckoutInfoer } type Commit struct { @@ -117,7 +118,7 @@ func (c *Commit) Run() (rerr error) { } // Update the local build script. Its build expression should match the committed expression. - if err := buildscript_runbit.Update(proj.Dir(), stagedCommit.BuildScript(), c.prime.Config()); err != nil { + if err := c.prime.CheckoutInfo().UpdateBuildScript(stagedCommit.BuildScript()); err != nil { return errs.Wrap(err, "Could not update build script") } diff --git a/internal/runners/cve/cve.go b/internal/runners/cve/cve.go index 9995a66030..6f10685c5b 100644 --- a/internal/runners/cve/cve.go +++ b/internal/runners/cve/cve.go @@ -7,13 +7,12 @@ import ( "strconv" "time" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" + "github.com/ActiveState/cli/pkg/checkoutinfo" medmodel "github.com/ActiveState/cli/pkg/platform/api/mediator/model" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" @@ -24,14 +23,14 @@ type primeable interface { primer.Projecter primer.Auther primer.Outputer - primer.Configurer + primer.CheckoutInfoer } type Cve struct { proj *project.Project auth *authentication.Auth out output.Outputer - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } type CveInfo struct { @@ -41,7 +40,7 @@ type CveInfo struct { } func NewCve(prime primeable) *Cve { - return &Cve{prime.Project(), prime.Auth(), prime.Output(), prime.Config()} + return &Cve{prime.Project(), prime.Auth(), prime.Output(), prime.CheckoutInfo()} } type Params struct { @@ -118,7 +117,7 @@ func (r *Cve) fetchVulnerabilities(namespaceOverride project.Namespaced) (*medmo commitID = namespaceOverride.CommitID.String() } else { var err error - commitUUID, err := buildscript_runbit.CommitID(r.proj.Dir(), r.cfg) + commitUUID, err := r.info.CommitID() if err != nil { return nil, errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/deploy/deploy.go b/internal/runners/deploy/deploy.go index 9c366c8daf..b40b44cd02 100644 --- a/internal/runners/deploy/deploy.go +++ b/internal/runners/deploy/deploy.go @@ -23,7 +23,6 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/rtutils" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/checkout" runtime_runbit "github.com/ActiveState/cli/internal/runbits/runtime" "github.com/ActiveState/cli/internal/runbits/runtime/progress" @@ -72,6 +71,7 @@ type primeable interface { primer.Analyticer primer.SvcModeler primer.Projecter + primer.CheckoutInfoer } func NewDeploy(step Step, prime primeable) *Deploy { @@ -174,16 +174,17 @@ func (d *Deploy) install(params *Params, commitID strfmt.UUID) (rerr error) { return errs.Wrap(err, "Could not create project files") } - if err := buildscript_runbit.Initialize(params.Path, params.Namespace.Owner, params.Namespace.Project, constants.DefaultBranchName, commitID.String(), d.auth, d.cfg); err != nil { - return errs.Wrap(err, "Unable to initialize buildscript") - } - proj, err := project.FromPath(params.Path) if err != nil { return locale.WrapError(err, "err_project_frompath") } d.prime.SetProject(proj) + err = d.prime.CheckoutInfo().InitializeBuildScript(commitID) + if err != nil { + return errs.Wrap(err, "Could not initialize build script") + } + pg := progress.NewRuntimeProgressIndicator(d.output) defer rtutils.Closer(pg.Close, &rerr) diff --git a/internal/runners/deploy/uninstall/uninstall.go b/internal/runners/deploy/uninstall/uninstall.go index d9a0624e5e..6a8d82a62b 100644 --- a/internal/runners/deploy/uninstall/uninstall.go +++ b/internal/runners/deploy/uninstall/uninstall.go @@ -17,10 +17,10 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/rtutils/ptr" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/runtime/trigger" "github.com/ActiveState/cli/internal/subshell" "github.com/ActiveState/cli/internal/subshell/sscommon" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/project" "github.com/ActiveState/cli/pkg/projectfile" ) @@ -85,7 +85,8 @@ func (u *Uninstall) Run(params *Params) error { return locale.WrapError(err, "err_deploy_uninstall_cannot_read_project", "Cannot read project at '{{.V0}}'", path) } - commitID, err := buildscript_runbit.CommitID(path, u.cfg) + info := checkoutinfo.New(nil, u.cfg, proj) + commitID, err := info.CommitID() if err != nil { return locale.WrapError(err, "err_deploy_uninstall_cannot_read_commit", "Cannot read commit ID from project at '{{.V0}}'", path) } diff --git a/internal/runners/eval/eval.go b/internal/runners/eval/eval.go index f52ef81d88..2e8a05a7da 100644 --- a/internal/runners/eval/eval.go +++ b/internal/runners/eval/eval.go @@ -1,14 +1,13 @@ package eval import ( - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model/buildplanner" "github.com/ActiveState/cli/pkg/project" @@ -18,7 +17,7 @@ type primeable interface { primer.Outputer primer.Auther primer.Projecter - primer.Configurer + primer.CheckoutInfoer } type Params struct { @@ -29,7 +28,7 @@ type Eval struct { out output.Outputer project *project.Project auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } func New(p primeable) *Eval { @@ -37,7 +36,7 @@ func New(p primeable) *Eval { out: p.Output(), project: p.Project(), auth: p.Auth(), - cfg: p.Config(), + info: p.CheckoutInfo(), } } @@ -54,7 +53,7 @@ func (e *Eval) Run(params *Params) (rerr error) { return rationalize.ErrNoProject } - commitID, err := buildscript_runbit.CommitID(e.project.Dir(), e.cfg) + commitID, err := e.info.CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/exec/exec.go b/internal/runners/exec/exec.go index 6ddca60d0a..932f54c9dd 100644 --- a/internal/runners/exec/exec.go +++ b/internal/runners/exec/exec.go @@ -62,6 +62,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } type Params struct { diff --git a/internal/runners/export/buildplan.go b/internal/runners/export/buildplan.go index ff23a41151..ba4371f833 100644 --- a/internal/runners/export/buildplan.go +++ b/internal/runners/export/buildplan.go @@ -34,7 +34,7 @@ func (b *BuildPlan) Run(params *BuildPlanParams) (rerr error) { } commit, err := buildplanner.GetCommit( - proj, params.Namespace, params.CommitID, params.Target, b.prime.Auth(), out, b.prime.Config()) + proj, params.Namespace, params.CommitID, params.Target, b.prime.Auth(), out, b.prime.CheckoutInfo()) if err != nil { return errs.Wrap(err, "Could not get commit") } diff --git a/internal/runners/export/export.go b/internal/runners/export/export.go index 34e425b46a..651383247e 100644 --- a/internal/runners/export/export.go +++ b/internal/runners/export/export.go @@ -13,6 +13,7 @@ type primeable interface { primer.Projecter primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } type Export struct{} diff --git a/internal/runners/hello/hello_example.go b/internal/runners/hello/hello_example.go index bafa54ccaf..a3efc3e0bc 100644 --- a/internal/runners/hello/hello_example.go +++ b/internal/runners/hello/hello_example.go @@ -10,14 +10,13 @@ package hello import ( "errors" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/example" "github.com/ActiveState/cli/internal/runbits/rationalize" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" @@ -28,7 +27,7 @@ type primeable interface { primer.Outputer primer.Auther primer.Projecter - primer.Configurer + primer.CheckoutInfoer } // Params defines the parameters needed to execute a given runner. These @@ -53,7 +52,7 @@ type Hello struct { out output.Outputer project *project.Project auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } // New contains the scope in which an instance of Hello is constructed from an @@ -63,7 +62,7 @@ func New(p primeable) *Hello { out: p.Output(), project: p.Project(), auth: p.Auth(), - cfg: p.Config(), + info: p.CheckoutInfo(), } } @@ -125,7 +124,7 @@ func (h *Hello) Run(params *Params) (rerr error) { } // Grab data from the platform. - commitMsg, err := currentCommitMessage(h.project, h.auth, h.cfg) + commitMsg, err := currentCommitMessage(h.project, h.auth, h.info) if err != nil { err = errs.Wrap( err, "Cannot get commit message", @@ -149,12 +148,12 @@ func (h *Hello) Run(params *Params) (rerr error) { // is obtained. Since it is a sort of construction function that has some // complexity, it is helpful to provide localized error context. Secluding this // sort of logic is helpful to keep the subhandlers clean. -func currentCommitMessage(proj *project.Project, auth *authentication.Auth, cfg *config.Instance) (string, error) { +func currentCommitMessage(proj *project.Project, auth *authentication.Auth, info *checkoutinfo.CheckoutInfo) (string, error) { if proj == nil { return "", errs.New("Cannot determine which project to use") } - commitId, err := buildscript_runbit.CommitID(proj.Dir(), cfg) + commitId, err := info.CommitID() if err != nil { return "", errs.Wrap(err, "Cannot determine which commit to use") } diff --git a/internal/runners/history/history.go b/internal/runners/history/history.go index c6ef6b8467..1bef0ddcd9 100644 --- a/internal/runners/history/history.go +++ b/internal/runners/history/history.go @@ -3,13 +3,12 @@ package history import ( "github.com/go-openapi/strfmt" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/commit" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/api/mono/mono_models" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" @@ -20,14 +19,14 @@ type primeable interface { primer.Projecter primer.Outputer primer.Auther - primer.Configurer + primer.CheckoutInfoer } type History struct { project *project.Project out output.Outputer auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } func NewHistory(prime primeable) *History { @@ -35,7 +34,7 @@ func NewHistory(prime primeable) *History { prime.Project(), prime.Output(), prime.Auth(), - prime.Config(), + prime.CheckoutInfo(), } } @@ -48,7 +47,7 @@ func (h *History) Run(params *HistoryParams) error { } h.out.Notice(locale.Tr("operating_message", h.project.NamespaceString(), h.project.Dir())) - localCommitID, err := buildscript_runbit.CommitID(h.project.Dir(), h.cfg) + localCommitID, err := h.info.CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/initialize/init.go b/internal/runners/initialize/init.go index 33688aef3d..2d4471f85a 100644 --- a/internal/runners/initialize/init.go +++ b/internal/runners/initialize/init.go @@ -18,13 +18,13 @@ import ( "github.com/ActiveState/cli/internal/osutils" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/dependencies" "github.com/ActiveState/cli/internal/runbits/errors" "github.com/ActiveState/cli/internal/runbits/org" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/internal/runbits/runtime" "github.com/ActiveState/cli/internal/runbits/runtime/trigger" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner" @@ -69,6 +69,7 @@ type primeable interface { primer.Analyticer primer.SvcModeler primer.Projecter + primer.CheckoutInfoer } type errProjectExists struct { @@ -98,7 +99,7 @@ func New(prime primeable) *Initialize { // (i.e. `state use show`). // Error handling is not necessary because it's an input error to not include a language to // `state init`. We're just trying to infer one as a convenience to the user. -func inferLanguage(config projectfile.ConfigGetter, auth *authentication.Auth, cfg Configurable) (string, string, bool) { +func inferLanguage(config projectfile.ConfigGetter, auth *authentication.Auth) (string, string, bool) { defaultProjectDir := config.GetString(constants.GlobalDefaultPrefname) if defaultProjectDir == "" { return "", "", false @@ -107,7 +108,8 @@ func inferLanguage(config projectfile.ConfigGetter, auth *authentication.Auth, c if err != nil { return "", "", false } - commitID, err := buildscript_runbit.CommitID(defaultProj.Dir(), cfg) + info := checkoutinfo.New(auth, config, defaultProj) + commitID, err := info.CommitID() if err != nil { multilog.Error("Unable to get local commit: %v", errs.JoinMessage(err)) return "", "", false @@ -180,7 +182,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) { languageVersion = langParts[1] } } else { - languageName, languageVersion, inferred = inferLanguage(r.config, r.auth, r.config) + languageName, languageVersion, inferred = inferLanguage(r.config, r.auth) } if languageName == "" { @@ -275,7 +277,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) { return errs.Wrap(err, "Could not create project") } - if err := buildscript_runbit.Initialize(proj.Dir(), proj.Owner(), proj.Name(), proj.BranchName(), commitID.String(), r.auth, r.config); err != nil { + if err := r.prime.CheckoutInfo().InitializeBuildScript(commitID); err != nil { return errs.Wrap(err, "Unable to initialize buildscript") } diff --git a/internal/runners/languages/install.go b/internal/runners/languages/install.go index d2f208dae6..578fa5cd8d 100644 --- a/internal/runners/languages/install.go +++ b/internal/runners/languages/install.go @@ -26,6 +26,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } func NewUpdate(prime primeable) *Update { diff --git a/internal/runners/languages/languages.go b/internal/runners/languages/languages.go index d106752e01..c20b8610ce 100644 --- a/internal/runners/languages/languages.go +++ b/internal/runners/languages/languages.go @@ -2,14 +2,13 @@ package languages import ( "github.com/ActiveState/cli/internal/analytics" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/pkg/buildplan" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner" @@ -23,7 +22,7 @@ type Languages struct { analytics analytics.Dispatcher svcModel *model.SvcModel auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } // NewLanguages prepares a list execution context for use. @@ -34,7 +33,7 @@ func NewLanguages(prime primeable) *Languages { prime.Analytics(), prime.SvcModel(), prime.Auth(), - prime.Config(), + prime.CheckoutInfo(), } } @@ -57,7 +56,7 @@ func (l *Languages) Run() error { return rationalize.ErrNoProject } - commitID, err := buildscript_runbit.CommitID(l.project.Dir(), l.cfg) + commitID, err := l.info.CommitID() if err != nil { return errs.AddTips( locale.WrapError( diff --git a/internal/runners/manifest/manifest.go b/internal/runners/manifest/manifest.go index 7e3eaf88f7..dd28c97a90 100644 --- a/internal/runners/manifest/manifest.go +++ b/internal/runners/manifest/manifest.go @@ -8,7 +8,7 @@ import ( "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/pkg/buildplan" "github.com/ActiveState/cli/pkg/buildscript" @@ -26,6 +26,7 @@ type primeable interface { primer.Analyticer primer.SvcModeler primer.Configurer + primer.CheckoutInfoer } type Manifest struct { @@ -100,7 +101,7 @@ func (m *Manifest) fetchRequirements() ([]buildscript.Requirement, error) { return nil, errs.Wrap(err, "Could not get buildscript") } } else { - commitID, err := buildscript_runbit.CommitID(m.project.Dir(), m.cfg) + commitID, err := m.prime.CheckoutInfo().CommitID() if err != nil { return nil, errs.Wrap(err, "Could not get commit ID") } @@ -121,7 +122,7 @@ func (m *Manifest) fetchRequirements() ([]buildscript.Requirement, error) { } func (m *Manifest) fetchBuildplanRequirements() (buildplan.Ingredients, error) { - commitID, err := buildscript_runbit.CommitID(m.project.Dir(), m.cfg) + commitID, err := m.prime.CheckoutInfo().CommitID() if err != nil { return nil, errs.Wrap(err, "Failed to get commit ID") } diff --git a/internal/runners/packages/import.go b/internal/runners/packages/import.go index 2053bb1f98..1186fd019e 100644 --- a/internal/runners/packages/import.go +++ b/internal/runners/packages/import.go @@ -10,7 +10,6 @@ import ( "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/cves" "github.com/ActiveState/cli/internal/runbits/dependencies" "github.com/ActiveState/cli/internal/runbits/org" @@ -69,6 +68,7 @@ type primeable interface { primer.Analyticer primer.SvcModeler primer.Configurer + primer.CheckoutInfoer } // NewImport prepares an importation execution context for use. @@ -93,7 +93,7 @@ func (i *Import) Run(params *ImportRunParams) (rerr error) { params.FileName = defaultImportFile } - localCommitId, err := buildscript_runbit.CommitID(proj.Dir(), i.prime.Config()) + localCommitId, err := i.prime.CheckoutInfo().CommitID() if err != nil { return locale.WrapError(err, "package_err_cannot_obtain_commit") } @@ -141,7 +141,7 @@ func (i *Import) Run(params *ImportRunParams) (rerr error) { // Always update the build script, even if the commit fails to build if stagedCommit != nil && stagedCommit.Commit != nil && stagedCommit.Commit.CommitID != "" { - err = buildscript_runbit.Update(proj.Dir(), stagedCommit.BuildScript(), i.prime.Config()) + err = i.prime.CheckoutInfo().UpdateBuildScript(stagedCommit.BuildScript()) if err != nil { if i.prime.Config().GetBool(constants.OptinBuildscriptsConfig) { return locale.WrapError(err, "err_update_build_script") diff --git a/internal/runners/packages/info.go b/internal/runners/packages/info.go index 40fe39ccea..5e71438a16 100644 --- a/internal/runners/packages/info.go +++ b/internal/runners/packages/info.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/ActiveState/cli/internal/captain" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" @@ -14,6 +13,7 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/rtutils/ptr" "github.com/ActiveState/cli/internal/runbits/commits_runbit" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/api/inventory/inventory_models" "github.com/ActiveState/cli/pkg/platform/api/vulnerabilities/request" "github.com/ActiveState/cli/pkg/platform/authentication" @@ -34,7 +34,7 @@ type Info struct { out output.Outputer proj *project.Project auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } // NewInfo prepares an information execution context for use. @@ -43,7 +43,7 @@ func NewInfo(prime primeable) *Info { out: prime.Output(), proj: prime.Project(), auth: prime.Auth(), - cfg: prime.Config(), + info: prime.CheckoutInfo(), } } @@ -61,7 +61,7 @@ func (i *Info) Run(params InfoRunParams, nstype model.NamespaceType) error { } if nsTypeV != nil { - language, err := targetedLanguage(params.Language, i.proj, i.auth, i.cfg) + language, err := targetedLanguage(params.Language, i.proj, i.auth, i.info) if err != nil { return locale.WrapError(err, fmt.Sprintf("%s_err_cannot_obtain_language", *nsTypeV)) } @@ -74,7 +74,7 @@ func (i *Info) Run(params InfoRunParams, nstype model.NamespaceType) error { normalized = params.Package.Name } - ts, err := commits_runbit.ExpandTimeForProject(¶ms.Timestamp, i.auth, i.proj, i.cfg) + ts, err := commits_runbit.ExpandTimeForProject(¶ms.Timestamp, i.auth, i.proj, i.info) if err != nil { return errs.Wrap(err, "Unable to get timestamp from params") } diff --git a/internal/runners/packages/install.go b/internal/runners/packages/install.go index 17a97373b6..74fe9a11ac 100644 --- a/internal/runners/packages/install.go +++ b/internal/runners/packages/install.go @@ -52,7 +52,7 @@ func (a *Install) Run(params InstallRunParams, nsType model.NamespaceType) (rerr reqs = append(reqs, req) } - ts, err := commits_runbit.ExpandTimeForProject(¶ms.Timestamp, a.prime.Auth(), a.prime.Project(), a.prime.Config()) + ts, err := commits_runbit.ExpandTimeForProject(¶ms.Timestamp, a.prime.Auth(), a.prime.Project(), a.prime.CheckoutInfo()) if err != nil { return errs.Wrap(err, "Unable to get timestamp from params") } diff --git a/internal/runners/packages/list.go b/internal/runners/packages/list.go index 0ebb24a1ea..09941117c3 100644 --- a/internal/runners/packages/list.go +++ b/internal/runners/packages/list.go @@ -11,14 +11,13 @@ import ( "github.com/go-openapi/strfmt" "github.com/ActiveState/cli/internal/analytics" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/rtutils/ptr" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" + "github.com/ActiveState/cli/pkg/checkoutinfo" gqlModel "github.com/ActiveState/cli/pkg/platform/api/graphql/model" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" @@ -39,7 +38,7 @@ type List struct { analytics analytics.Dispatcher svcModel *model.SvcModel auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } // NewList prepares a list execution context for use. @@ -50,7 +49,7 @@ func NewList(prime primeable) *List { analytics: prime.Analytics(), svcModel: prime.SvcModel(), auth: prime.Auth(), - cfg: prime.Config(), + info: prime.CheckoutInfo(), } } @@ -89,7 +88,7 @@ func (l *List) Run(params ListRunParams, nstype model.NamespaceType) error { return locale.WrapError(err, fmt.Sprintf("%s_err_cannot_obtain_commit", nstype)) } default: - commitID, err = targetFromProjectFile(l.project, l.cfg) + commitID, err = targetFromProjectFile(l.project, l.info) if err != nil { return locale.WrapError(err, fmt.Sprintf("%s_err_cannot_obtain_commit", nstype)) } @@ -205,12 +204,12 @@ func targetFromProject(projectString string) (*strfmt.UUID, error) { return branch.CommitID, nil } -func targetFromProjectFile(proj *project.Project, cfg *config.Instance) (*strfmt.UUID, error) { +func targetFromProjectFile(proj *project.Project, info *checkoutinfo.CheckoutInfo) (*strfmt.UUID, error) { logging.Debug("commit from project file") if proj == nil { return nil, rationalize.ErrNoProject } - commit, err := buildscript_runbit.CommitID(proj.Dir(), cfg) + commit, err := info.CommitID() if err != nil { return nil, errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/packages/search.go b/internal/runners/packages/search.go index cfd76cdfc1..f916460b80 100644 --- a/internal/runners/packages/search.go +++ b/internal/runners/packages/search.go @@ -4,13 +4,12 @@ import ( "fmt" "github.com/ActiveState/cli/internal/captain" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/commits_runbit" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/api/vulnerabilities/request" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" @@ -31,7 +30,7 @@ type Search struct { out output.Outputer proj *project.Project auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } // NewSearch prepares a searching execution context for use. @@ -40,7 +39,7 @@ func NewSearch(prime primeable) *Search { out: prime.Output(), proj: prime.Project(), auth: prime.Auth(), - cfg: prime.Config(), + info: prime.CheckoutInfo(), } } @@ -52,7 +51,7 @@ func (s *Search) Run(params SearchRunParams, nstype model.NamespaceType) error { var ns model.Namespace if params.Ingredient.Namespace == "" { - language, err := targetedLanguage(params.Language, s.proj, s.auth, s.cfg) + language, err := targetedLanguage(params.Language, s.proj, s.auth, s.info) if err != nil { return locale.WrapError(err, fmt.Sprintf("%s_err_cannot_obtain_language", nstype)) } @@ -62,7 +61,7 @@ func (s *Search) Run(params SearchRunParams, nstype model.NamespaceType) error { ns = model.NewRawNamespace(params.Ingredient.Namespace) } - ts, err := commits_runbit.ExpandTimeForProject(¶ms.Timestamp, s.auth, s.proj, s.cfg) + ts, err := commits_runbit.ExpandTimeForProject(¶ms.Timestamp, s.auth, s.proj, s.info) if err != nil { return errs.Wrap(err, "Unable to get timestamp from params") } @@ -116,7 +115,7 @@ func (s *Search) Run(params SearchRunParams, nstype model.NamespaceType) error { return nil } -func targetedLanguage(languageOpt string, proj *project.Project, auth *authentication.Auth, cfg *config.Instance) (string, error) { +func targetedLanguage(languageOpt string, proj *project.Project, auth *authentication.Auth, info *checkoutinfo.CheckoutInfo) (string, error) { if languageOpt != "" { return languageOpt, nil } @@ -127,7 +126,7 @@ func targetedLanguage(languageOpt string, proj *project.Project, auth *authentic ) } - commitID, err := buildscript_runbit.CommitID(proj.Dir(), cfg) + commitID, err := info.CommitID() if err != nil { return "", errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/packages/uninstall.go b/internal/runners/packages/uninstall.go index daba4e8ce6..399f1ddec4 100644 --- a/internal/runners/packages/uninstall.go +++ b/internal/runners/packages/uninstall.go @@ -51,7 +51,7 @@ func (u *Uninstall) Run(params UninstallRunParams, nsType model.NamespaceType) ( reqs = append(reqs, req) } - ts, err := commits_runbit.ExpandTimeForProject(&captain.TimeValue{}, u.prime.Auth(), u.prime.Project(), u.prime.Config()) + ts, err := commits_runbit.ExpandTimeForProject(&captain.TimeValue{}, u.prime.Auth(), u.prime.Project(), u.prime.CheckoutInfo()) if err != nil { return errs.Wrap(err, "Unable to get timestamp from params") } diff --git a/internal/runners/platforms/add.go b/internal/runners/platforms/add.go index 767c569f2a..02c1eb484e 100644 --- a/internal/runners/platforms/add.go +++ b/internal/runners/platforms/add.go @@ -28,6 +28,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } // NewAdd prepares an add execution context for use. diff --git a/internal/runners/platforms/list.go b/internal/runners/platforms/list.go index 80bd71ca65..2b9a1e6fb7 100644 --- a/internal/runners/platforms/list.go +++ b/internal/runners/platforms/list.go @@ -1,13 +1,12 @@ package platforms import ( - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/authentication" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" @@ -19,7 +18,7 @@ type List struct { out output.Outputer proj *project.Project auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } // NewList prepares a list execution context for use. @@ -28,7 +27,7 @@ func NewList(prime primeable) *List { out: prime.Output(), proj: prime.Project(), auth: prime.Auth(), - cfg: prime.Config(), + info: prime.CheckoutInfo(), } } @@ -40,7 +39,7 @@ func (l *List) Run() error { return rationalize.ErrNoProject } - commitID, err := buildscript_runbit.CommitID(l.proj.Dir(), l.cfg) + commitID, err := l.info.CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/prepare/prepare.go b/internal/runners/prepare/prepare.go index 2b43463dd5..3b9b0ab9c1 100644 --- a/internal/runners/prepare/prepare.go +++ b/internal/runners/prepare/prepare.go @@ -23,6 +23,7 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/subshell" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" "github.com/ActiveState/cli/pkg/runtime_helpers" @@ -37,6 +38,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } // Prepare manages the prepare execution context. @@ -46,6 +48,7 @@ type Prepare struct { cfg *config.Instance analytics analytics.Dispatcher svcModel *model.SvcModel + info *checkoutinfo.CheckoutInfo } // New prepares a prepare execution context for use. @@ -55,6 +58,7 @@ func New(prime primeable) *Prepare { subshell: prime.Subshell(), cfg: prime.Config(), analytics: prime.Analytics(), + info: prime.CheckoutInfo(), } } @@ -77,7 +81,7 @@ func (r *Prepare) resetExecutors() error { return errs.Wrap(err, "Could not initialize runtime for project.") } - rtHash, err := runtime_helpers.Hash(proj, nil, r.cfg) + rtHash, err := runtime_helpers.Hash(proj, nil, r.info) if err != nil { return errs.Wrap(err, "Could not get runtime hash") } diff --git a/internal/runners/pull/pull.go b/internal/runners/pull/pull.go index 83931d15e7..802b409e85 100644 --- a/internal/runners/pull/pull.go +++ b/internal/runners/pull/pull.go @@ -64,6 +64,7 @@ type primeable interface { primer.Analyticer primer.Configurer primer.SvcModeler + primer.CheckoutInfoer } func New(prime primeable) *Pull { @@ -123,7 +124,7 @@ func (p *Pull) Run(params *PullParams) (rerr error) { } var localCommit *strfmt.UUID - localCommitID, err := buildscript_runbit.CommitID(p.project.Dir(), p.cfg) + localCommitID, err := p.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -181,7 +182,7 @@ func (p *Pull) Run(params *PullParams) (rerr error) { p.notifyMergeStrategy(string(strategy), *localCommit, remoteProject) } - commitID, err := buildscript_runbit.CommitID(p.project.Dir(), p.cfg) + commitID, err := p.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -197,7 +198,7 @@ func (p *Pull) Run(params *PullParams) (rerr error) { if err != nil { return errs.Wrap(err, "Could not get build script") } - err = buildscript_runbit.Update(p.project.Dir(), script, p.cfg) + err = p.prime.CheckoutInfo().UpdateBuildScript(script) if err != nil { return errs.Wrap(err, "Unable to update build script") } @@ -280,7 +281,7 @@ func (p *Pull) mergeBuildScript(remoteCommit, localCommit strfmt.UUID) error { switch { case errors.Is(err, model.ErrMergeFastForward): // Fast forward to the remote commit ID. - return buildscript_runbit.Update(p.project.Dir(), scriptB, p.cfg) + return p.prime.CheckoutInfo().UpdateBuildScript(scriptB) case !errors.Is(err, model.ErrMergeCommitInHistory): return locale.WrapError(err, "err_mergecommit", "Could not detect if merge is necessary.") } @@ -297,7 +298,7 @@ func (p *Pull) mergeBuildScript(remoteCommit, localCommit strfmt.UUID) error { } // Write the merged build expression as a local build script. - return buildscript_runbit.Update(p.project.Dir(), scriptA, p.cfg) + return p.prime.CheckoutInfo().UpdateBuildScript(scriptA) } func resolveRemoteProject(prj *project.Project) (*project.Namespaced, error) { diff --git a/internal/runners/push/push.go b/internal/runners/push/push.go index 65b0249a64..1f3597c70c 100644 --- a/internal/runners/push/push.go +++ b/internal/runners/push/push.go @@ -11,8 +11,8 @@ import ( "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/prompt" "github.com/ActiveState/cli/internal/rtutils/ptr" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/api/buildplanner/types" "github.com/ActiveState/cli/pkg/platform/api/mono/mono_models" "github.com/ActiveState/cli/pkg/platform/authentication" @@ -35,6 +35,7 @@ type Push struct { project *project.Project prompt prompt.Prompter auth *authentication.Auth + info *checkoutinfo.CheckoutInfo } type PushParams struct { @@ -47,10 +48,11 @@ type primeable interface { primer.Configurer primer.Prompter primer.Auther + primer.CheckoutInfoer } func NewPush(prime primeable) *Push { - return &Push{prime.Config(), prime.Output(), prime.Project(), prime.Prompt(), prime.Auth()} + return &Push{prime.Config(), prime.Output(), prime.Project(), prime.Prompt(), prime.Auth(), prime.CheckoutInfo()} } type intention uint16 @@ -92,7 +94,7 @@ func (r *Push) Run(params PushParams) (rerr error) { } r.out.Notice(locale.Tr("operating_message", r.project.NamespaceString(), r.project.Dir())) - commitID, err := buildscript_runbit.CommitID(r.project.Dir(), r.config) // The commit we want to push + commitID, err := r.info.CommitID() // The commit we want to push if err != nil { // Note: should not get here, as verifyInput() ensures there is a local commit return errs.Wrap(err, "Unable to get commit ID") @@ -207,11 +209,7 @@ func (r *Push) Run(params PushParams) (rerr error) { } // Update the project's commitID with the create project or push result. - script, err = bp.GetBuildScript(targetNamespace.Owner, targetNamespace.Project, branch.Label, commitID.String()) - if err != nil { - return errs.Wrap(err, "Could not get build script") - } - err = buildscript_runbit.Update(r.project.Dir(), script, r.config) + err = r.info.SetCommitID(commitID) if err != nil { return errs.Wrap(err, "Unable to update build script") } @@ -289,7 +287,7 @@ func (r *Push) verifyInput() error { return rationalize.ErrNoProject } - commitID, err := buildscript_runbit.CommitID(r.project.Dir(), r.config) + commitID, err := r.info.CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -327,7 +325,7 @@ func (r *Push) promptNamespace() (*project.Namespaced, error) { } var name string - commitID, err := buildscript_runbit.CommitID(r.project.Dir(), r.config) + commitID, err := r.info.CommitID() if err != nil { return nil, errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/refresh/refresh.go b/internal/runners/refresh/refresh.go index 48eb16d95c..a36922a3f5 100644 --- a/internal/runners/refresh/refresh.go +++ b/internal/runners/refresh/refresh.go @@ -11,7 +11,6 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/prompt" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/findproject" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/internal/runbits/runtime" @@ -35,6 +34,7 @@ type primeable interface { primer.SvcModeler primer.Analyticer primer.Projecter + primer.CheckoutInfoer } type Refresh struct { @@ -78,12 +78,8 @@ func (r *Refresh) Run(params *Params) error { r.out.Notice(locale.Tr("operating_message", proj.NamespaceString(), proj.Dir())) - needsUpdate, err := runtime_helpers.NeedsUpdate(proj, nil, r.config) + needsUpdate, err := runtime_helpers.NeedsUpdate(proj, nil, r.prime.CheckoutInfo()) if err != nil { - if errors.Is(err, buildscript_runbit.ErrBuildscriptNotExist) { - return locale.WrapInputError(err, locale.T("notice_needs_buildscript_reset")) - - } return errs.Wrap(err, "could not determine if runtime needs update") } diff --git a/internal/runners/reset/reset.go b/internal/runners/reset/reset.go index 41573e1588..e6e55264b7 100644 --- a/internal/runners/reset/reset.go +++ b/internal/runners/reset/reset.go @@ -11,7 +11,6 @@ import ( "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/rationalize" "github.com/ActiveState/cli/internal/runbits/runtime" "github.com/ActiveState/cli/internal/runbits/runtime/trigger" @@ -51,6 +50,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } func New(prime primeable) *Reset { @@ -79,7 +79,7 @@ func (r *Reset) Run(params *Params) error { if err != nil { return locale.WrapError(err, "err_reset_latest_commit", "Could not get latest commit ID") } - localCommitID, err := buildscript_runbit.CommitID(r.project.Dir(), r.cfg) + localCommitID, err := r.prime.CheckoutInfo().CommitID() var errInvalidCommitID *checkoutinfo.ErrInvalidCommitID if err != nil && !errors.As(err, &errInvalidCommitID) { return errs.Wrap(err, "Unable to get commit ID") @@ -90,7 +90,7 @@ func (r *Reset) Run(params *Params) error { commitID = *latestCommit case strings.EqualFold(params.CommitID, local): - localCommitID, err := buildscript_runbit.CommitID(r.project.Dir(), r.cfg) + localCommitID, err := r.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -108,7 +108,7 @@ func (r *Reset) Run(params *Params) error { } } - localCommitID, err := buildscript_runbit.CommitID(r.project.Dir(), r.cfg) + localCommitID, err := r.prime.CheckoutInfo().CommitID() var errInvalidCommitID *checkoutinfo.ErrInvalidCommitID if err != nil && !errors.As(err, &errInvalidCommitID) { return errs.Wrap(err, "Unable to get commit ID") @@ -126,10 +126,7 @@ func (r *Reset) Run(params *Params) error { } // Reset the build script. - if err := buildscript_runbit.Remove(r.project.Dir()); err != nil { - return errs.Wrap(err, "Unable to remove existing build script") - } - if err := buildscript_runbit.Initialize(r.project.Dir(), r.project.Owner(), r.project.Name(), r.project.BranchName(), commitID.String(), r.auth, r.cfg); err != nil { + if err := r.prime.CheckoutInfo().InitializeBuildScript(commitID); err != nil { return errs.Wrap(err, "Unable to initialize buildscript") } diff --git a/internal/runners/revert/revert.go b/internal/runners/revert/revert.go index baf8c6d819..e2c5191680 100644 --- a/internal/runners/revert/revert.go +++ b/internal/runners/revert/revert.go @@ -4,13 +4,11 @@ import ( "strings" "github.com/ActiveState/cli/internal/analytics" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/prompt" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/commit" "github.com/ActiveState/cli/internal/runbits/rationalize" runtime_runbit "github.com/ActiveState/cli/internal/runbits/runtime" @@ -35,7 +33,6 @@ type Revert struct { auth *authentication.Auth analytics analytics.Dispatcher svcModel *model.SvcModel - cfg *config.Instance } type Params struct { @@ -52,6 +49,7 @@ type primeable interface { primer.Analyticer primer.SvcModeler primer.Configurer + primer.CheckoutInfoer } func New(prime primeable) *Revert { @@ -63,7 +61,6 @@ func New(prime primeable) *Revert { prime.Auth(), prime.Analytics(), prime.SvcModel(), - prime.Config(), } } @@ -85,7 +82,7 @@ func (r *Revert) Run(params *Params) (rerr error) { if !strfmt.IsUUID(commitID) && !strings.EqualFold(commitID, remoteCommitID) { return locale.NewInputError("err_revert_invalid_commit_id", "Invalid commit ID") } - latestCommit, err := buildscript_runbit.CommitID(r.project.Dir(), r.cfg) + latestCommit, err := r.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -158,7 +155,7 @@ func (r *Revert) Run(params *Params) (rerr error) { locale.T("tip_private_project_auth")) } - err = buildscript_runbit.Update(r.project.Dir(), revertScript, r.cfg) + err = r.prime.CheckoutInfo().UpdateBuildScript(revertScript) if err != nil { return errs.Wrap(err, "Unable to update build script") } diff --git a/internal/runners/run/run.go b/internal/runners/run/run.go index f19185edf3..de37f90ef1 100644 --- a/internal/runners/run/run.go +++ b/internal/runners/run/run.go @@ -43,6 +43,7 @@ type primeable interface { primer.Configurer primer.SvcModeler primer.Analyticer + primer.CheckoutInfoer } // New constructs a new instance of Run. @@ -76,7 +77,7 @@ func (r *Run) Run(name string, args []string) error { r.out.Notice(output.Title(locale.Tl("run_script_title", "Running Script: [ACTIONABLE]{{.V0}}[/RESET]", name))) if r.auth.Authenticated() { - checker.RunCommitsBehindNotifier(r.proj, r.out, r.auth, r.cfg) + checker.RunCommitsBehindNotifier(r.proj, r.out, r.auth, r.prime.CheckoutInfo()) } script, err := r.proj.ScriptByName(name) diff --git a/internal/runners/shell/shell.go b/internal/runners/shell/shell.go index 0540d6e5ac..90c50a5203 100644 --- a/internal/runners/shell/shell.go +++ b/internal/runners/shell/shell.go @@ -15,7 +15,6 @@ import ( "github.com/ActiveState/cli/internal/process" "github.com/ActiveState/cli/internal/prompt" "github.com/ActiveState/cli/internal/runbits/activation" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/findproject" "github.com/ActiveState/cli/internal/runbits/runtime" "github.com/ActiveState/cli/internal/runbits/runtime/trigger" @@ -41,6 +40,7 @@ type primeable interface { primer.SvcModeler primer.Analyticer primer.Projecter + primer.CheckoutInfoer } type Shell struct { @@ -84,7 +84,7 @@ func (u *Shell) Run(params *Params) error { u.prime.SetProject(proj) - commitID, err := buildscript_runbit.CommitID(proj.Dir(), u.config) + commitID, err := u.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/show/show.go b/internal/runners/show/show.go index 71c207debf..79ca0e21a7 100644 --- a/internal/runners/show/show.go +++ b/internal/runners/show/show.go @@ -9,7 +9,6 @@ import ( "github.com/ActiveState/cli/pkg/runtime_helpers" "github.com/go-openapi/strfmt" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constraints" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/fileutils" @@ -17,9 +16,9 @@ import ( "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/internal/secrets" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/platform/api/mono/mono_models" secretsapi "github.com/ActiveState/cli/pkg/platform/api/secrets" "github.com/ActiveState/cli/pkg/platform/authentication" @@ -39,7 +38,7 @@ type Show struct { out output.Outputer conditional *constraints.Conditional auth *authentication.Auth - cfg *config.Instance + info *checkoutinfo.CheckoutInfo } type primeable interface { @@ -47,7 +46,7 @@ type primeable interface { primer.Outputer primer.Conditioner primer.Auther - primer.Configurer + primer.CheckoutInfoer } type RuntimeDetails struct { @@ -131,7 +130,7 @@ func New(prime primeable) *Show { prime.Output(), prime.Conditional(), prime.Auth(), - prime.Config(), + prime.CheckoutInfo(), } } @@ -193,7 +192,7 @@ func (s *Show) Run(params Params) error { return locale.WrapError(err, "err_show_scripts", "Could not parse scripts") } - commitID, err = buildscript_runbit.CommitID(s.project.Dir(), s.cfg) + commitID, err = s.info.CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } @@ -229,7 +228,7 @@ func (s *Show) Run(params Params) error { return locale.WrapError(err, "err_show_langauges", "Could not retrieve language information") } - commit, err := commitsData(owner, projectName, branchName, commitID, s.project, s.auth, s.cfg) + commit, err := commitsData(owner, projectName, branchName, commitID, s.project, s.auth, s.info) if err != nil { return locale.WrapError(err, "err_show_commit", "Could not get commit information") } @@ -360,7 +359,7 @@ func visibilityData(owner, project string, remoteProject *mono_models.Project) s return locale.T("public") } -func commitsData(owner, project, branchName string, commitID strfmt.UUID, localProject *project.Project, auth *authentication.Auth, cfg *config.Instance) (string, error) { +func commitsData(owner, project, branchName string, commitID strfmt.UUID, localProject *project.Project, auth *authentication.Auth, info *checkoutinfo.CheckoutInfo) (string, error) { latestCommit, err := model.BranchCommitID(owner, project, branchName) if err != nil { return "", locale.WrapError(err, "err_show_get_latest_commit", "Could not get latest commit ID") @@ -384,7 +383,7 @@ func commitsData(owner, project, branchName string, commitID strfmt.UUID, localP if err != nil { return "", locale.WrapError(err, "err_show_commits_behind", "Could not determine number of commits behind latest") } - localCommitID, err := buildscript_runbit.CommitID(localProject.Dir(), cfg) + localCommitID, err := info.CommitID() if err != nil { return "", errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/runners/swtch/switch.go b/internal/runners/swtch/switch.go index 29473d747e..86b2b7bdc3 100644 --- a/internal/runners/swtch/switch.go +++ b/internal/runners/swtch/switch.go @@ -8,7 +8,6 @@ import ( "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/internal/runbits/runtime" "github.com/ActiveState/cli/internal/runbits/runtime/trigger" @@ -44,6 +43,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } type identifier interface { @@ -99,7 +99,6 @@ func (s *Switch) Run(params SwitchParams) error { if err != nil { return locale.WrapError(err, "err_fetch_project", "", s.project.Namespace().String()) } - branch := s.project.BranchName() identifier, err := resolveIdentifier(project, params.Identifier) if err != nil { @@ -107,12 +106,13 @@ func (s *Switch) Run(params SwitchParams) error { } if id, ok := identifier.(branchIdentifier); ok { - branch = id.branch.Label + err = s.project.Source().SetBranch(id.branch.Label) + if err != nil { + return locale.WrapError(err, "err_switch_set_branch", "Could not update branch") + } } - commitID := identifier.CommitID() - - belongs, err := model.CommitBelongsToBranch(s.project.Owner(), s.project.Name(), branch, commitID, s.auth) + belongs, err := model.CommitBelongsToBranch(s.project.Owner(), s.project.Name(), s.project.BranchName(), identifier.CommitID(), s.auth) if err != nil { return locale.WrapError(err, "err_identifier_branch", "Could not determine if commit belongs to branch") } @@ -121,11 +121,11 @@ func (s *Switch) Run(params SwitchParams) error { } bp := buildplanner.NewBuildPlannerModel(s.auth) - script, err := bp.GetBuildScript(s.project.Owner(), s.project.Name(), branch, commitID.String()) + script, err := bp.GetBuildScript(s.project.Owner(), s.project.Name(), s.project.BranchName(), identifier.CommitID().String()) if err != nil { return errs.Wrap(err, "Could not get build script") } - err = buildscript_runbit.Update(s.project.Dir(), script, s.cfg) + err = s.prime.CheckoutInfo().UpdateBuildScript(script) if err != nil { return errs.Wrap(err, "Unable to update build script") } diff --git a/internal/runners/upgrade/upgrade.go b/internal/runners/upgrade/upgrade.go index ae9473b894..4e94bb5c10 100644 --- a/internal/runners/upgrade/upgrade.go +++ b/internal/runners/upgrade/upgrade.go @@ -13,7 +13,6 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/rtutils/ptr" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/commits_runbit" "github.com/ActiveState/cli/internal/runbits/rationalize" "github.com/ActiveState/cli/internal/sliceutils" @@ -31,7 +30,7 @@ type primeable interface { primer.Auther primer.Projecter primer.Prompter - primer.Configurer + primer.CheckoutInfoer } type Params struct { @@ -91,7 +90,7 @@ func (u *Upgrade) Run(params *Params) (rerr error) { }() // Collect "before" buildplan - localCommitID, err := buildscript_runbit.CommitID(proj.Dir(), u.prime.Config()) + localCommitID, err := u.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Failed to get commit ID") } @@ -158,7 +157,7 @@ func (u *Upgrade) Run(params *Params) (rerr error) { } } - err = buildscript_runbit.Update(proj.Dir(), bumpedCommit.BuildScript(), u.prime.Config()) + err = u.prime.CheckoutInfo().UpdateBuildScript(bumpedCommit.BuildScript()) if err != nil { return errs.Wrap(err, "Could not update build script") } diff --git a/internal/runners/use/use.go b/internal/runners/use/use.go index 3ed2167138..2039e4fa8d 100644 --- a/internal/runners/use/use.go +++ b/internal/runners/use/use.go @@ -12,7 +12,6 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/prompt" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" "github.com/ActiveState/cli/internal/runbits/checkout" "github.com/ActiveState/cli/internal/runbits/findproject" "github.com/ActiveState/cli/internal/runbits/git" @@ -37,6 +36,7 @@ type primeable interface { primer.SvcModeler primer.Analyticer primer.Projecter + primer.CheckoutInfoer } type Use struct { @@ -81,7 +81,7 @@ func (u *Use) Run(params *Params) error { u.prime.SetProject(proj) - commitID, err := buildscript_runbit.CommitID(proj.Dir(), u.config) + commitID, err := u.prime.CheckoutInfo().CommitID() if err != nil { return errs.Wrap(err, "Unable to get commit ID") } diff --git a/internal/scriptrun/scriptrun.go b/internal/scriptrun/scriptrun.go index 12f94c17c9..29776e3c7a 100644 --- a/internal/scriptrun/scriptrun.go +++ b/internal/scriptrun/scriptrun.go @@ -33,6 +33,7 @@ type primeable interface { primer.Configurer primer.Analyticer primer.SvcModeler + primer.CheckoutInfoer } // ScriptRun manages the context required to run a script. diff --git a/pkg/checkoutinfo/checkoutinfo.go b/pkg/checkoutinfo/checkoutinfo.go index 0a026d7186..a76c3cc6f5 100644 --- a/pkg/checkoutinfo/checkoutinfo.go +++ b/pkg/checkoutinfo/checkoutinfo.go @@ -5,21 +5,38 @@ import ( "os" "path/filepath" + "github.com/go-openapi/strfmt" + "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/fileutils" - "github.com/ActiveState/cli/internal/locale" + "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/pkg/buildscript" - "github.com/ActiveState/cli/pkg/project" - "github.com/go-openapi/strfmt" + "github.com/ActiveState/cli/pkg/platform/authentication" + "github.com/ActiveState/cli/pkg/platform/model/buildplanner" ) -// proj holds the project instance most recently accessed, if any. -// Using globals in this way is an anti-pattern, but because the commit mechanic is going through a lot of changes -// we're currently handling it this way to help further refactors. Once we've landed the go-forward mechanic we should -// remove this anti-pattern. -// https://activestatef.atlassian.net/browse/DX-2524 -var proj *project.Project +type configurer interface { + GetBool(string) bool +} + +type projecter interface { + Owner() string + Name() string + BranchName() string + LegacyCommitID() string + SetLegacyCommit(string) error + Dir() string + URL() string +} + +type CheckoutInfo struct { + auth *authentication.Auth + cfg configurer + project projecter +} + +var ErrBuildscriptNotExist = errors.New("Build script does not exist") type ErrInvalidCommitID struct { CommitID string @@ -29,32 +46,24 @@ func (e ErrInvalidCommitID) Error() string { return "invalid commit ID" } -func setupProject(pjpath string) error { - if proj != nil && proj.Dir() == pjpath { - return nil - } - var err error - proj, err = project.FromPath(pjpath) - if err != nil { - return errs.Wrap(err, "Could not get project info to set up project") - } - return nil +func New(auth *authentication.Auth, cfg configurer, project projecter) *CheckoutInfo { + return &CheckoutInfo{auth, cfg, project} } -func GetProject(pjpath string) (string, error) { - if err := setupProject(pjpath); err != nil { - return "", errs.Wrap(err, "Could not setup project") - } +func (c *CheckoutInfo) Owner() string { + return c.project.Owner() +} - return proj.Source().Project, nil +func (c *CheckoutInfo) Name() string { + return c.project.Name() } -func GetCommitID(pjpath string) (strfmt.UUID, error) { - if err := setupProject(pjpath); err != nil { - return "", errs.Wrap(err, "Could not setup project") - } +func (c *CheckoutInfo) Branch() string { + return c.project.BranchName() +} - commitID := proj.LegacyCommitID() +func (c *CheckoutInfo) CommitID() (strfmt.UUID, error) { + commitID := c.project.LegacyCommitID() if !strfmt.IsUUID(commitID) { return "", &ErrInvalidCommitID{commitID} } @@ -62,39 +71,27 @@ func GetCommitID(pjpath string) (strfmt.UUID, error) { return strfmt.UUID(commitID), nil } -func SetCommitID(pjpath, commitID string) error { - if !strfmt.IsUUID(commitID) { - return locale.NewInputError("err_commit_id_invalid", commitID) - } - - if err := setupProject(pjpath); err != nil { - return errs.Wrap(err, "Could not setup project") - } - - if err := proj.SetLegacyCommit(commitID); err != nil { +func (c *CheckoutInfo) SetCommitID(commitID strfmt.UUID) error { + // Update commitID in activestate.yaml. + logging.Debug("Updating commitID in activestate.yaml") + if err := c.project.SetLegacyCommit(commitID.String()); err != nil { return errs.Wrap(err, "Could not set commit ID") } - if err := updateBuildScript(); err != nil { - return errs.Wrap(err, "Could not update build script") + if !c.cfg.GetBool(constants.OptinBuildscriptsConfig) { + return nil // buildscripts are not enabled, so nothing more to do } - return nil -} + // Update commitID in Project field of build script. + logging.Debug("Updating commitID in buildscript") + buildscriptPath := filepath.Join(c.project.Dir(), constants.BuildScriptFileName) -// updateBuildScript updates the build script's Project info field. -// Note: cannot use runbits.buildscript.ScriptFromProject() and Update() due to import cycle. -func updateBuildScript() error { - buildscriptPath := filepath.Join(proj.ProjectDir(), constants.BuildScriptFileName) + if !fileutils.FileExists(buildscriptPath) { + return c.InitializeBuildScript(commitID) + } data, err := fileutils.ReadFile(buildscriptPath) if err != nil { - if errors.Is(err, os.ErrNotExist) { - // There is no build script to update, so just exit. - // Normally we would put this behind a optin.buildscripts config test, but that would require - // another config global anti-pattern for this package. - return nil // no build script to update - } return errs.Wrap(err, "Could not read build script for updating") } @@ -106,7 +103,7 @@ func updateBuildScript() error { return errs.Wrap(err, "Could not unmarshal build script") } - script.SetProjectURL(proj.URL()) + script.SetProjectURL(c.project.URL()) data, err = script.Marshal() if err != nil { @@ -121,20 +118,78 @@ func updateBuildScript() error { return nil } -func UpdateProject(script *buildscript.BuildScript, dir string) error { - err := setupProject(dir) - if err != nil { - return errs.Wrap(err, "Could not setup project") +func (c *CheckoutInfo) InitializeBuildScript(commitID strfmt.UUID) error { + if c.cfg.GetBool(constants.OptinBuildscriptsConfig) { + buildplanner := buildplanner.NewBuildPlannerModel(c.auth) + script, err := buildplanner.GetBuildScript(c.Owner(), c.Name(), c.Branch(), commitID.String()) + if err != nil { + return errs.Wrap(err, "Unable to get the remote build script") + } + + scriptBytes, err := script.Marshal() + if err != nil { + return errs.Wrap(err, "Unable to marshal build script") + } + + scriptPath := filepath.Join(c.project.Dir(), constants.BuildScriptFileName) + logging.Debug("Initializing build script at %s", scriptPath) + err = fileutils.WriteFile(scriptPath, scriptBytes) + if err != nil { + return errs.Wrap(err, "Unable to write build script") + } } - commitID, err := script.CommitID() - if err != nil { - return errs.Wrap(err, "Could not get commit ID") + // Update activestate.yaml. + if err := c.project.SetLegacyCommit(commitID.String()); err != nil { + return errs.Wrap(err, "Could not set commit ID") + } + + return nil +} + +func (c *CheckoutInfo) UpdateBuildScript(newScript *buildscript.BuildScript) error { + if c.cfg.GetBool(constants.OptinBuildscriptsConfig) { + path := filepath.Join(c.project.Dir(), constants.BuildScriptFileName) + + data, err := fileutils.ReadFile(path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return errs.Pack(err, ErrBuildscriptNotExist) + } + return errs.Wrap(err, "Could not read build script from file") + } + + script, err := buildscript.Unmarshal(data) + if err != nil { + return errs.Wrap(err, "Could not unmarshal build script") + } + + equals, err := script.Equals(newScript) + if err != nil { + return errs.Wrap(err, "Could not compare build script") + } + if script != nil && equals { + return nil // no changes to write + } + + sb, err := newScript.Marshal() + if err != nil { + return errs.Wrap(err, "Could not marshal build script") + } + + logging.Debug("Writing build script") + if err := fileutils.WriteFile(filepath.Join(path, constants.BuildScriptFileName), sb); err != nil { + return errs.Wrap(err, "Could not write build script to file") + } } - err = proj.SetLegacyCommit(commitID.String()) + // Update activestate.yaml. + commitID, err := newScript.CommitID() if err != nil { - return errs.Wrap(err, "Could not update commit ID") + return errs.Wrap(err, "Could not get script commit ID") + } + if err := c.project.SetLegacyCommit(commitID.String()); err != nil { + return errs.Wrap(err, "Could not set commit ID") } return nil diff --git a/pkg/projectfile/projectfile.go b/pkg/projectfile/projectfile.go index a524d631b8..a1d8929faf 100644 --- a/pkg/projectfile/projectfile.go +++ b/pkg/projectfile/projectfile.go @@ -1195,6 +1195,7 @@ type ConfigGetter interface { AllKeys() []string GetStringSlice(string) []string GetString(string) string + GetBool(string) bool Set(string, interface{}) error GetThenSet(string, func(interface{}) (interface{}, error)) error Close() error diff --git a/pkg/runtime_helpers/helpers.go b/pkg/runtime_helpers/helpers.go index 0381ad3e5f..8fe292e164 100644 --- a/pkg/runtime_helpers/helpers.go +++ b/pkg/runtime_helpers/helpers.go @@ -4,14 +4,13 @@ import ( "path/filepath" "strings" - "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/fileutils" "github.com/ActiveState/cli/internal/hash" "github.com/ActiveState/cli/internal/installation/storage" "github.com/ActiveState/cli/internal/multilog" - buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript" + "github.com/ActiveState/cli/pkg/checkoutinfo" "github.com/ActiveState/cli/pkg/project" "github.com/ActiveState/cli/pkg/runtime" "github.com/go-openapi/strfmt" @@ -32,13 +31,13 @@ func FromProject(proj *project.Project) (*runtime.Runtime, error) { return rt, nil } -func NeedsUpdate(proj *project.Project, overrideCommitID *strfmt.UUID, cfg *config.Instance) (bool, error) { +func NeedsUpdate(proj *project.Project, overrideCommitID *strfmt.UUID, info *checkoutinfo.CheckoutInfo) (bool, error) { rt, err := FromProject(proj) if err != nil { return false, errs.Wrap(err, "Could not obtain runtime") } - hash, err := Hash(proj, overrideCommitID, cfg) + hash, err := Hash(proj, overrideCommitID, info) if err != nil { return false, errs.Wrap(err, "Could not get hash") } @@ -46,11 +45,11 @@ func NeedsUpdate(proj *project.Project, overrideCommitID *strfmt.UUID, cfg *conf return hash != rt.Hash(), nil } -func Hash(proj *project.Project, overrideCommitID *strfmt.UUID, cfg *config.Instance) (string, error) { +func Hash(proj *project.Project, overrideCommitID *strfmt.UUID, info *checkoutinfo.CheckoutInfo) (string, error) { var err error var commitID strfmt.UUID if overrideCommitID == nil { - commitID, err = buildscript_runbit.CommitID(proj.Dir(), cfg) + commitID, err = info.CommitID() if err != nil { return "", errs.Wrap(err, "Failed to get commit ID") }