Files
pathmaster/main.go
2024-10-02 22:25:50 +02:00

80 lines
1.7 KiB
Go

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
}