Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

targeted elevation #767 #793

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/nvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,20 @@ func use(version string, cpuarch string, reload ...bool) {
var ok bool
ok, err = runElevated(fmt.Sprintf(`"%s" cmd /C mklink /D "%s" "%s"`, filepath.Join(env.root, "elevate.cmd"), filepath.Clean(env.symlink), filepath.Join(env.root, "v"+version)))
if err != nil {
if strings.Contains(err.Error(), "file already exists") {
if strings.Contains(err.Error(), "not have sufficient privilege") {
cmd := exec.Command(filepath.Join(env.root, "elevate.cmd"), "cmd", "/C", "mklink", "/D", filepath.Clean(env.symlink), filepath.Join(env.root, "v"+version))
var output bytes.Buffer
var _stderr bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = &_stderr
perr := cmd.Run()
if perr != nil {
ok = false
fmt.Println(fmt.Sprint(perr) + ": " + _stderr.String())
} else {
ok = true
}
} else if strings.Contains(err.Error(), "file already exists") {
ok, err = runElevated(fmt.Sprintf(`"%s" cmd /C rmdir "%s"`, filepath.Join(env.root, "elevate.cmd"), filepath.Clean(env.symlink)))
reloadable := true
if len(reload) > 0 {
Expand Down Expand Up @@ -865,6 +878,10 @@ func runElevated(command string, forceUAC ...bool) (bool, error) {

if uac {
// Alternative elevation option at stackoverflow.com/questions/31558066/how-to-ask-for-administer-privileges-on-windows-with-go
path := filepath.Join(env.root, "elevate.cmd")
command = strings.ReplaceAll(command, path, ``)
//command = strings.ReplaceAll(command, `"`, ``)
command = strings.TrimSpace(command)
cmd := exec.Command(filepath.Join(env.root, "elevate.cmd"), command)
var output bytes.Buffer
var _stderr bytes.Buffer
Expand All @@ -890,8 +907,8 @@ func runElevated(command string, forceUAC ...bool) (bool, error) {
err := c.Run()
if err != nil {
msg := stderr.String()
if strings.Contains(msg, "not have sufficient privilege") && uac {
return runElevated(command, false)
if strings.Contains(msg, "not have sufficient privilege") && !uac {
return runElevated(command, true)
}
// fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return false, errors.New(fmt.Sprint(err) + ": " + msg)
Expand Down
2 changes: 1 addition & 1 deletion src/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func GetNodeJS(root string, v string, a string, append bool) bool {
}

zip := root + "\\v" + v + "\\" + strings.Replace(filepath.Base(url), ".zip", "", 1)
err = fs.Move(zip, root+"\\v"+v, true)
err = fs.Move(zip, root+"\\v"+v)
ojintoad marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Println("ERROR moving file: " + err.Error())
}
Expand Down