134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"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)
|
|
}
|
|
|
|
// I actually don't even know if this works or not.........
|
|
// Changes can be seen in windows system properties fucking thing
|
|
// But not in new terminals
|
|
// And rerunning the program even with a new terminal will result in the same output as the first run
|
|
// I really don't know whether or not this worked... And I don't know how to check
|
|
func main() {
|
|
currentPath := os.Getenv("PATH")
|
|
paths := strings.Split(currentPath, ";")
|
|
|
|
alwaysInclude := []string{
|
|
"C:\\Program Files\\Git\\usr\\bin",
|
|
os.Getenv("GOPATH") + "\\bin",
|
|
}
|
|
|
|
cleanPaths := []string{}
|
|
for _, path := range paths {
|
|
var err error
|
|
path = filepath.Clean(path)
|
|
if !filepath.IsAbs(path) {
|
|
path, err = filepath.Abs(path)
|
|
if err != nil {
|
|
Error.Printf("error getting absolute path for %v: %v", path, err)
|
|
return
|
|
}
|
|
}
|
|
path = filepath.FromSlash(path)
|
|
|
|
fhandle, err := os.Open(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
Warning.Printf("Path %v does not exist", path)
|
|
continue
|
|
}
|
|
Warning.Printf("Error opening path %v: %v", path, err)
|
|
continue
|
|
}
|
|
fhandle.Close()
|
|
|
|
if slices.Contains(cleanPaths, path) {
|
|
Warning.Printf("Path %v already exists in cleanPaths", path)
|
|
continue
|
|
}
|
|
|
|
cleanPaths = append(cleanPaths, path)
|
|
log.Printf("Added path %v", path)
|
|
}
|
|
|
|
for _, path := range alwaysInclude {
|
|
path = filepath.Clean(path)
|
|
if !filepath.IsAbs(path) {
|
|
var err error
|
|
path, err = filepath.Abs(path)
|
|
if err != nil {
|
|
Error.Printf("error getting absolute path for %v: %v", path, err)
|
|
return
|
|
}
|
|
}
|
|
path = filepath.FromSlash(path)
|
|
|
|
fhandle, err := os.Open(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
Warning.Printf("Path %v does not exist", path)
|
|
continue
|
|
}
|
|
Warning.Printf("Error opening path %v: %v", path, err)
|
|
continue
|
|
}
|
|
fhandle.Close()
|
|
|
|
if slices.Contains(cleanPaths, path) {
|
|
Warning.Printf("Path %v already exists in cleanPaths", path)
|
|
continue
|
|
}
|
|
|
|
cleanPaths = append(cleanPaths, path)
|
|
log.Printf("Added path %v", path)
|
|
}
|
|
|
|
newPath := strings.Join(cleanPaths, ";")
|
|
|
|
cmd := exec.Command("setx", "PATH", newPath)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
Error.Printf("Error setting PATH with error: %v and output:\n%s", err, string(out))
|
|
return
|
|
}
|
|
|
|
log.Printf("PATH has been updated successfully")
|
|
}
|
|
|
|
func contains(slice []string, item string) bool {
|
|
for _, a := range slice {
|
|
if a == item {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|