wanna-goon/goon.go

82 lines
2.4 KiB
Go

package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/Merith-TK/utils/debug"
)
// goonFiles moves files from the specified source path to the `.wanna-goon` directory.
// This is a "soft punishment" for users who break the rules.
func goonFiles(src string) error {
debug.SetTitle("goonFiles") // Set the debug title to "goonFiles"
defer debug.ResetTitle() // Reset the debug title when the function exits
var goonError error
var goonDest string
// Get the absolute path of the source file/directory
src, goonError = filepath.Abs(src)
if goonError != nil {
return goonError
}
goonDest = filepath.ToSlash(src)
// Check if the source path exists
_, err := os.Stat(src)
if os.IsNotExist(err) {
return fmt.Errorf("source path does not exist: %s", src)
}
if err != nil {
return fmt.Errorf("could not stat source path: %v", err)
}
// Prepare the destination path within the `.wanna-goon` directory
goonDest = strings.TrimPrefix(goonDest, "C:/")
userProfile := os.Getenv("USERPROFILE")
if userProfile == "" {
return fmt.Errorf("could not get USERPROFILE environment variable")
}
goonDestPath := filepath.Join("C:/", ".wanna-goon", goonDest)
debug.Print("gooning:", src)
debug.Print("to:", goonDestPath)
// Create the necessary directories in the `.wanna-goon` directory
goonError = os.MkdirAll(filepath.Dir(goonDestPath), os.ModePerm)
if goonError != nil {
return fmt.Errorf("could not create directory %s: %v", goonDestPath, goonError)
}
// Move the file/directory to the `.wanna-goon` directory
goonError = os.Rename(src, goonDestPath)
if goonError != nil {
return fmt.Errorf("could not move file from %s to %s: %v", src, goonDestPath, goonError)
}
return goonError
}
// goonProgram kills a running process by its executable name.
// This is used to stop programs that are running against the rules.
func goonProgram(exeName string) error {
var err error
debug.SetTitle("cooming") // Set the debug title to "cooming"
defer debug.ResetTitle() // Reset the debug title when the function exits
debug.Print("gooning:", exeName)
// Execute a command to forcefully kill the process by its name
cmdKill := exec.Command("cmd", "/C", "taskkill /IM", exeName, "/F")
err = cmdKill.Run()
if err != nil {
fmt.Println("Error killing Discord process:", err)
return err
}
return nil
}