-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrun_win.go
More file actions
197 lines (165 loc) · 6.66 KB
/
run_win.go
File metadata and controls
197 lines (165 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//go:build windows
// +build windows
package clean
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
svcApp "github.com/ActiveState/cli/cmd/state-svc/app"
"github.com/ActiveState/cli/internal/assets"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/installation"
"github.com/ActiveState/cli/internal/installation/storage"
"github.com/ActiveState/cli/internal/language"
"github.com/ActiveState/cli/internal/legacytray"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/scriptfile"
)
func (u *Uninstall) runUninstall(params *UninstallParams) error {
// we aggregate installation errors, such that we can display all installation problems in the end
// TODO: This behavior should be replaced with a proper rollback mechanism https://www.pivotaltracker.com/story/show/178134918
var aggErr error
logFile, err := ioutil.TempFile("", "state-clean-uninstall")
if err != nil {
logging.Error("Could not create temporary log file: %s", errs.JoinMessage(err))
aggErr = locale.WrapError(aggErr, "err_clean_logfile", "Could not create temporary log file")
}
stateExec, err := installation.StateExec()
if err != nil {
logging.Debug("Could not get State Tool executable: %s", errs.JoinMessage(err))
aggErr = locale.WrapError(aggErr, "err_state_exec")
}
err = removeInstall(logFile.Name(), params, u.cfg)
if err != nil {
logging.Debug("Could not remove installation: %s", errs.JoinMessage(err))
aggErr = locale.WrapError(aggErr, "uninstall_remove_executables_err", "Failed to remove all State Tool files in installation directory {{.V0}}", filepath.Dir(stateExec))
}
err = removeApp()
if err != nil {
logging.Debug("Could not remove app: %s", errs.JoinMessage(err))
aggErr = locale.WrapError(aggErr, "uninstall_remove_app_err", "Failed to remove service application")
}
if params.All {
err = removeCache(storage.CachePath())
if err != nil {
logging.Debug("Could not remove cache at %s: %s", storage.CachePath(), errs.JoinMessage(err))
aggErr = locale.WrapError(aggErr, "uninstall_remove_cache_err", "Failed to remove cache directory {{.V0}}.", storage.CachePath())
}
}
err = undoPrepare()
if err != nil {
logging.Debug("Could not undo prepare: %s", errs.JoinMessage(err))
aggErr = locale.WrapError(aggErr, "uninstall_prepare_err", "Failed to undo some installation steps.")
}
err = removeEnvPaths(u.cfg)
if err != nil {
logging.Debug("Could not remove environment paths: %s", errs.JoinMessage(err))
aggErr = locale.WrapError(aggErr, "uninstall_remove_paths_err", "Failed to remove PATH entries from environment")
}
if aggErr != nil {
return aggErr
}
u.out.Notice(locale.Tr("clean_message_windows", logFile.Name()))
if params.Prompt {
u.out.Print(locale.Tl("clean_uninstall_confirm_exit", "Press enter to exit."))
fmt.Scanln(ptr.To("")) // Wait for input from user
}
return nil
}
func removeConfig(configPath string, out output.Outputer) error {
logFile, err := ioutil.TempFile("", "state-clean-config")
if err != nil {
return locale.WrapError(err, "err_clean_logfile", "Could not create temporary log file")
}
out.Notice(locale.Tr("clean_config_message_windows", logFile.Name()))
return removePaths(logFile.Name(), configPath)
}
func removeInstall(logFile string, params *UninstallParams, cfg *config.Instance) error {
svcExec, err := installation.ServiceExec()
if err != nil {
return locale.WrapError(err, "err_service_exec")
}
err = legacytray.DetectAndRemove(filepath.Dir(svcExec), cfg)
if err != nil {
return locale.WrapError(err, "err_remove_legacy_tray", "Could not remove legacy tray application")
}
err = os.Remove(svcExec)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return locale.WrapError(err, "uninstall_rm_exec", "Could not remove executable: {{.V0}}. Error: {{.V1}}.", svcExec, err.Error())
}
stateExec, err := installation.StateExec()
if err != nil {
return locale.WrapError(err, "err_state_exec")
}
// Schedule removal of the entire branch name directory.
// This is because Windows often thinks the installation.InstallDirMarker and
// constants.StateInstallerCmd files are still in use.
branchDir := filepath.Dir(filepath.Dir(stateExec))
paths := []string{stateExec, branchDir}
if params.All {
paths = append(paths, cfg.ConfigPath()) // also remove the config directory
}
// If the transitional state tool path is known, we remove it. This is done in the background, because the transitional State Tool can be the initiator of the uninstall request
if transitionalStateTool := cfg.GetString(installation.CfgTransitionalStateToolPath); transitionalStateTool != "" {
paths = append(paths, transitionalStateTool)
}
return removePaths(logFile, paths...)
}
func removePaths(logFile string, paths ...string) error {
logging.Debug("Removing paths: %v", paths)
scriptName := "removePaths"
scriptBlock, err := assets.ReadFile(fmt.Sprintf("scripts/%s.bat", scriptName))
if err != nil {
return err
}
sf, err := scriptfile.New(language.Batch, scriptName, string(scriptBlock))
if err != nil {
return locale.WrapError(err, "err_clean_script", "Could not create new scriptfile")
}
exe, err := os.Executable()
if err != nil {
return locale.WrapError(err, "err_clean_executable", "Could not get executable name")
}
args := []string{"/C", sf.Filename(), logFile, fmt.Sprintf("%d", os.Getpid()), filepath.Base(exe)}
args = append(args, paths...)
_, err = exeutils.ExecuteAndForget("cmd.exe", args)
if err != nil {
return locale.WrapError(err, "err_clean_start", "Could not start remove direcotry script")
}
return nil
}
func removeApp() error {
svcApp, err := svcApp.New()
if err != nil {
return locale.WrapError(err, "err_autostart_app")
}
err = svcApp.Uninstall()
if err != nil {
return locale.WrapError(err, "err_uninstall_app", "Could not uninstall the State Tool service app.")
}
return nil
}
// verifyInstallation ensures that the State Tool was installed in a way
// that will allow us to properly uninstall
func verifyInstallation() error {
installationContext, err := installation.GetContext()
if err != nil {
return errs.Wrap(err, "Could not check if initial installation was run as admin")
}
isAdmin, err := osutils.IsAdmin()
if err != nil {
return errs.Wrap(err, "Could not check if current user is an administrator")
}
if installationContext.InstalledAsAdmin && !isAdmin {
return locale.NewInputError("err_uninstall_privilege_mismatch")
}
return nil
}