From fb00267a5b93116ff8988e8b7e23c7282e6cc1b7 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 2 Oct 2024 22:25:50 +0200 Subject: [PATCH] Initial commit --- go.mod | 3 +++ main.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f05f569 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module pathmaster + +go 1.23.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..744b11c --- /dev/null +++ b/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "io" + "log" + "os" + "os/exec" + "strings" +) + +var Error *log.Logger +var Warning *log.Logger +func init() { + log.SetFlags(log.Lmicroseconds | log.Lshortfile) + logFile, err := os.Create("main.log") + if err != nil { + log.Printf("Error creating log file: %v", err) + return + } + logger := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(logger) + + Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout), + fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"), + log.Lmicroseconds|log.Lshortfile) + Warning = log.New(io.MultiWriter(logFile, os.Stdout), + fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"), + log.Lmicroseconds|log.Lshortfile) +} + +func main() { + // Get the current PATH + currentPath := os.Getenv("PATH") + paths := strings.Split(currentPath, ";") + + // Define paths to always include + alwaysInclude := []string{ + "C:\\Program Files\\Git\\usr\\bin", + os.Getenv("GOPATH") + "\\bin", + } + + // Clean up the PATH + cleanPaths := []string{} + for _, path := range paths { + // Check if the path exists + if _, err := os.Stat(path); err == nil { + cleanPaths = append(cleanPaths, path) + } + } + + // Add the always include paths + for _, path := range alwaysInclude { + if !contains(cleanPaths, path) { + cleanPaths = append(cleanPaths, path) + } + } + + // Join the clean paths + newPath := strings.Join(cleanPaths, ";") + + // Set the new PATH permanently + cmd := exec.Command("setx", "PATH", newPath) + err := cmd.Run() + if err != nil { + fmt.Println("Error setting PATH:", err) + return + } + + fmt.Println("PATH has been updated successfully.") +} + +func contains(slice []string, item string) bool { + for _, a := range slice { + if a == item { + return true + } + } + return false +} \ No newline at end of file