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

     1  package update
     2  
     3  import (
     4  	"github.com/ActiveState/cli/internal/constants"
     5  	"github.com/ActiveState/cli/internal/locale"
     6  	"github.com/ActiveState/cli/internal/multilog"
     7  	"github.com/ActiveState/cli/internal/output"
     8  	"github.com/ActiveState/cli/internal/prompt"
     9  	"github.com/ActiveState/cli/internal/runbits/rationalize"
    10  	"github.com/ActiveState/cli/internal/updater"
    11  	"github.com/ActiveState/cli/pkg/project"
    12  	"github.com/ActiveState/cli/pkg/projectfile"
    13  )
    14  
    15  type UnlockParams struct {
    16  	NonInteractive bool
    17  }
    18  
    19  type Unlock struct {
    20  	project *project.Project
    21  	out     output.Outputer
    22  	prompt  prompt.Prompter
    23  	cfg     updater.Configurable
    24  }
    25  
    26  func NewUnlock(prime primeable) *Unlock {
    27  	return &Unlock{
    28  		prime.Project(),
    29  		prime.Output(),
    30  		prime.Prompt(),
    31  		prime.Config(),
    32  	}
    33  }
    34  
    35  func (u *Unlock) Run(params *UnlockParams) error {
    36  	if u.project == nil {
    37  		return rationalize.ErrNoProject
    38  	}
    39  
    40  	if !u.project.IsLocked() {
    41  		u.out.Notice(locale.Tl("notice_not_locked", "The State Tool version is not locked for this project."))
    42  		return nil
    43  	}
    44  
    45  	u.out.Notice(locale.Tl("unlocking_version", "Unlocking State Tool version for current project."))
    46  
    47  	if !params.NonInteractive {
    48  		err := confirmUnlock(u.prompt)
    49  		if err != nil {
    50  			return locale.WrapError(err, "err_update_unlock_confirm", "Unlock cancelled by user.")
    51  		}
    52  	}
    53  
    54  	// Invalidate the installer version lock.
    55  	err := u.cfg.Set(updater.CfgKeyInstallVersion, "")
    56  	if err != nil {
    57  		multilog.Error("Failed to invalidate installer version lock on `state update lock` invocation: %v", err)
    58  	}
    59  
    60  	err = u.cfg.Set(constants.AutoUpdateConfigKey, "true")
    61  	if err != nil {
    62  		return locale.WrapError(err, "err_unlock_enable_autoupdate", "Unable to re-enable automatic updates prior to unlocking")
    63  	}
    64  
    65  	err = projectfile.RemoveLockInfo(u.project.Source().Path())
    66  	if err != nil {
    67  		return locale.WrapError(err, "err_update_projectfile", "Could not update projectfile")
    68  	}
    69  
    70  	u.out.Notice(locale.Tl("version_unlocked", "State Tool version unlocked"))
    71  	return nil
    72  }
    73  
    74  func confirmUnlock(prom prompt.Prompter) error {
    75  	msg := locale.T("confirm_update_unlocked_version_prompt")
    76  
    77  	defaultChoice := !prom.IsInteractive()
    78  	confirmed, err := prom.Confirm(locale.T("confirm"), msg, &defaultChoice)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	if !confirmed {
    84  		return locale.NewInputError("err_update_lock_noconfirm", "Cancelling by your request.")
    85  	}
    86  
    87  	return nil
    88  }