Files
windows-pathifier/main.go

68 lines
1.4 KiB
Go

package main
import (
"flag"
"os"
"strings"
logger "git.site.quack-lab.dev/dave/cylogger"
"golang.org/x/sys/windows/registry"
)
func main() {
flag.Parse()
logger.InitFlag()
currentDir, err := os.Getwd()
if err != nil {
logger.Error("Failed to get current directory: %v", err)
os.Exit(1)
}
logger.Info("Current directory: %s", currentDir)
// Get current PATH from registry
key, err := registry.OpenKey(registry.CURRENT_USER, `Environment`, registry.QUERY_VALUE|registry.SET_VALUE)
if err != nil {
logger.Error("Failed to open registry key: %v", err)
os.Exit(1)
}
defer key.Close()
pathValue, _, err := key.GetStringValue("PATH")
if err != nil {
logger.Error("Failed to read PATH from registry: %v", err)
os.Exit(1)
}
// Check if current directory is already in PATH
pathDirs := strings.Split(pathValue, ";")
currentDirInPath := false
for _, dir := range pathDirs {
if strings.EqualFold(strings.TrimSpace(dir), currentDir) {
currentDirInPath = true
break
}
}
if currentDirInPath {
logger.Info("Current directory is already in PATH")
return
}
// Add current directory to PATH
newPath := pathValue
if newPath != "" && !strings.HasSuffix(newPath, ";") {
newPath += ";"
}
newPath += currentDir
err = key.SetStringValue("PATH", newPath)
if err != nil {
logger.Error("Failed to update PATH in registry: %v", err)
os.Exit(1)
}
logger.Info("Successfully added current directory to PATH")
}