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

     1  package uninstall
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  
     7  	"github.com/ActiveState/cli/internal/analytics"
     8  	"github.com/ActiveState/cli/internal/analytics/constants"
     9  	"github.com/ActiveState/cli/internal/analytics/dimensions"
    10  	"github.com/ActiveState/cli/internal/config"
    11  	"github.com/ActiveState/cli/internal/errs"
    12  	"github.com/ActiveState/cli/internal/instanceid"
    13  	"github.com/ActiveState/cli/internal/locale"
    14  	"github.com/ActiveState/cli/internal/logging"
    15  	"github.com/ActiveState/cli/internal/multilog"
    16  	"github.com/ActiveState/cli/internal/osutils"
    17  	"github.com/ActiveState/cli/internal/output"
    18  	"github.com/ActiveState/cli/internal/primer"
    19  	"github.com/ActiveState/cli/internal/rtutils/ptr"
    20  	"github.com/ActiveState/cli/internal/subshell"
    21  	"github.com/ActiveState/cli/internal/subshell/sscommon"
    22  	"github.com/ActiveState/cli/pkg/platform/runtime/store"
    23  	"github.com/ActiveState/cli/pkg/platform/runtime/target"
    24  )
    25  
    26  type Params struct {
    27  	Path      string
    28  	UserScope bool
    29  }
    30  
    31  type Uninstall struct {
    32  	output    output.Outputer
    33  	subshell  subshell.SubShell
    34  	cfg       *config.Instance
    35  	analytics analytics.Dispatcher
    36  }
    37  
    38  type primeable interface {
    39  	primer.Outputer
    40  	primer.Subsheller
    41  	primer.Configurer
    42  	primer.Analyticer
    43  }
    44  
    45  func NewDeployUninstall(prime primeable) *Uninstall {
    46  	return &Uninstall{prime.Output(), prime.Subshell(), prime.Config(), prime.Analytics()}
    47  }
    48  
    49  func (u *Uninstall) Run(params *Params) error {
    50  	if runtime.GOOS == "windows" && !params.UserScope {
    51  		isAdmin, err := osutils.IsAdmin()
    52  		if err != nil {
    53  			multilog.Error("Could not check for windows administrator privileges: %v", err)
    54  		}
    55  		if !isAdmin {
    56  			return locale.NewError(
    57  				"err_deploy_uninstall_admin_privileges_required",
    58  				"Administrator rights are required for this command to modify the system PATH.  If you want to uninstall from the user environment, please adjust the command line flags.")
    59  		}
    60  	}
    61  
    62  	path := params.Path
    63  	var cwd string
    64  	if path == "" {
    65  		var err error
    66  		cwd, err = osutils.Getwd()
    67  		if err != nil {
    68  			return locale.WrapExternalError(
    69  				err,
    70  				"err_deploy_uninstall_cannot_get_cwd",
    71  				"Cannot determine current working directory. Please supply '[ACTIONABLE]--path[/RESET]' argument")
    72  		}
    73  		path = cwd
    74  	}
    75  
    76  	logging.Debug("Attempting to uninstall deployment at %s", path)
    77  	store := store.New(path)
    78  	if !store.HasMarker() {
    79  		return errs.AddTips(
    80  			locale.NewInputError("err_deploy_uninstall_not_deployed", "There is no deployed runtime at '{{.V0}}' to uninstall.", path),
    81  			locale.Tl("err_deploy_uninstall_not_deployed_tip", "Either change the current directory to a deployment or supply '--path <path>' arguments."))
    82  	}
    83  
    84  	if runtime.GOOS == "windows" && path == cwd {
    85  		return locale.NewInputError(
    86  			"err_deploy_uninstall_cannot_chdir",
    87  			"Cannot remove deployment in current working directory. Please cd elsewhere and run this command again with the '--path' flag.")
    88  	}
    89  
    90  	namespace, commitID := sourceAnalyticsInformation(store)
    91  
    92  	err := u.subshell.CleanUserEnv(u.cfg, sscommon.DeployID, params.UserScope)
    93  	if err != nil {
    94  		return locale.WrapError(err, "err_deploy_uninstall_env", "Failed to remove deploy directory from PATH")
    95  	}
    96  
    97  	err = os.RemoveAll(path)
    98  	if err != nil {
    99  		return locale.WrapError(err, "err_deploy_uninstall", "Unable to remove deployed runtime at '{{.V0}}'", path)
   100  	}
   101  
   102  	u.analytics.Event(constants.CatRuntimeUsage, constants.ActRuntimeDelete, &dimensions.Values{
   103  		Trigger:          ptr.To(target.TriggerDeploy.String()),
   104  		CommitID:         ptr.To(commitID),
   105  		ProjectNameSpace: ptr.To(namespace),
   106  		InstanceID:       ptr.To(instanceid.ID()),
   107  	})
   108  
   109  	u.output.Notice(locale.T("deploy_uninstall_success"))
   110  
   111  	return nil
   112  }
   113  
   114  func sourceAnalyticsInformation(store *store.Store) (string, string) {
   115  	namespace, err := store.Namespace()
   116  	if err != nil {
   117  		logging.Error("Could not read namespace from marker file: %v", err)
   118  	}
   119  
   120  	commitID, err := store.CommitID()
   121  	if err != nil {
   122  		logging.Error("Could not read commit ID from marker file: %v", err)
   123  	}
   124  
   125  	return namespace, commitID
   126  }