diff --git a/go.mod b/go.mod index ee8b438..1854cfe 100644 --- a/go.mod +++ b/go.mod @@ -2,13 +2,15 @@ module windows-pathifier go 1.23.6 -require git.site.quack-lab.dev/dave/cylogger v1.4.0 +require ( + git.site.quack-lab.dev/dave/cylogger v1.4.0 + golang.org/x/sys v0.15.0 +) require ( github.com/google/go-cmp v0.5.9 // indirect github.com/hexops/valast v1.5.0 // indirect golang.org/x/mod v0.7.0 // indirect - golang.org/x/sys v0.3.0 // indirect golang.org/x/tools v0.4.0 // indirect mvdan.cc/gofumpt v0.4.0 // indirect ) diff --git a/go.sum b/go.sum index 33bd5b7..3f4b1d7 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= -golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4= golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= mvdan.cc/gofumpt v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM= diff --git a/main.go b/main.go index 01bc6e2..b072677 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,66 @@ 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() -} \ No newline at end of file + + 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") +}