go-pathman/winpath/winpath_windows.go

115 lines
2.5 KiB
Go
Raw Permalink Normal View History

2019-07-22 05:21:47 +00:00
// +build windows
package winpath
// Needs to
// * use the registry editor directly to avoid possible PATH truncation
// ( https://stackoverflow.com/questions/9546324/adding-directory-to-path-environment-variable-in-windows )
// ( https://superuser.com/questions/387619/overcoming-the-1024-character-limit-with-setx )
// * explicitly send WM_SETTINGCHANGE
// ( https://github.com/golang/go/issues/18680#issuecomment-275582179 )
import (
"fmt"
"os"
"strings"
"golang.org/x/sys/windows/registry"
)
func add(p string) (bool, error) {
cur, err := paths()
if nil != err {
return false, err
}
index := IndexOf(cur, p)
// skip silently, successfully
if index >= 0 {
return false, nil
}
cur = append([]string{p}, cur...)
err = write(cur)
if nil != err {
return false, err
}
return true, nil
}
func remove(p string) (bool, error) {
cur, err := paths()
if nil != err {
return false, err
}
2019-07-28 10:04:53 +00:00
index := IndexOf(cur, p)
2019-07-22 05:21:47 +00:00
// skip silently, successfully
if index < 0 {
return false, nil
}
var newpaths []string
for i := range cur {
if i != index {
newpaths = append(newpaths, cur[i])
}
}
2019-07-28 11:03:44 +00:00
err = write(newpaths)
2019-07-22 05:21:47 +00:00
if nil != err {
return false, err
}
return true, nil
}
func write(cur []string) error {
// TODO --system to add to the system PATH rather than the user PATH
2019-07-28 11:03:44 +00:00
k, err := registry.OpenKey(registry.CURRENT_USER, `Environment`, registry.SET_VALUE)
2019-07-22 05:21:47 +00:00
if err != nil {
2019-07-28 11:03:44 +00:00
return fmt.Errorf("Can't open HKCU Environment for writes: %s", err)
2019-07-22 05:21:47 +00:00
}
defer k.Close()
err = k.SetStringValue(`Path`, strings.Join(cur, string(os.PathListSeparator)))
if nil != err {
2019-07-28 11:03:44 +00:00
return fmt.Errorf("Can't set HKCU Environment[Path]: %s", err)
2019-07-22 05:21:47 +00:00
}
err = k.Close()
if nil != err {
return err
}
if nil != sendmsg {
sendmsg()
} else {
fmt.Fprintf(os.Stderr, "Warning: added PATH, but you must reboot for changes to take effect\n")
}
return nil
}
func paths() ([]string, error) {
// This is the canonical reference, which is actually quite nice to have.
// TBH, it's a mess to do this on *nix systems.
k, err := registry.OpenKey(registry.CURRENT_USER, `Environment`, registry.QUERY_VALUE)
if err != nil {
2019-07-28 11:03:44 +00:00
return nil, fmt.Errorf("Can't open HKCU Environment for reads: %s", err)
2019-07-22 05:21:47 +00:00
}
defer k.Close()
// This is case insensitive on Windows.
// PATH, Path, path will all work.
s, _, err := k.GetStringValue("Path")
if err != nil {
2019-07-28 11:03:44 +00:00
return nil, fmt.Errorf("Can't query HKCU Environment[Path]: %s", err)
2019-07-22 05:21:47 +00:00
}
// ";" on Windows
return strings.Split(s, string(os.PathListSeparator)), nil
}