package main import ( "flag" "os" "strings" logger "git.site.quack-lab.dev/dave/cylogger" "golang.org/x/sys/windows/registry" ) func main() { undo := flag.Bool("u", false, "Remove current directory from PATH") 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 *undo { if !currentDirInPath { logger.Info("Current directory is not in PATH") return } // Remove current directory from PATH var newDirs []string for _, dir := range pathDirs { if !strings.EqualFold(strings.TrimSpace(dir), currentDir) { newDirs = append(newDirs, dir) } } newPath := strings.Join(newDirs, ";") err = key.SetStringValue("PATH", newPath) if err != nil { logger.Error("Failed to update PATH in registry: %v", err) os.Exit(1) } logger.Info("Successfully removed current directory from PATH") return } 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") }