From d5c37b42ec01460be89a6f3cb0b032ddce540919 Mon Sep 17 00:00:00 2001 From: partisan Date: Tue, 10 Sep 2024 23:30:34 +0200 Subject: [PATCH] windows is bs --- main.go | 6 ++++++ spitfire/build.go | 43 ++++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index d0729bd..3b025dc 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,12 @@ func printHelp() { } func main() { + // Check and set the MOZILLABUILD environment variable globally + err2 := spitfire.SetGlobalEnv("MOZILLABUILD", "C:\\mozilla-build", "user") // For user + if err2 != nil { + log.Fatalf("Error setting global environment variable: %v", err2) + } + flag.Parse() if flag.Lookup("h").Value.(flag.Getter).Get().(bool) { diff --git a/spitfire/build.go b/spitfire/build.go index bee99ea..929292d 100644 --- a/spitfire/build.go +++ b/spitfire/build.go @@ -11,31 +11,28 @@ import ( // Array to store errors var errors []string -// Check and Set MOZILLABUILD environment variable automatically -func setMozillaBuildEnv() error { - // Check if MOZILLABUILD is already set - if os.Getenv("MOZILLABUILD") != "" { - fmt.Println("MOZILLABUILD environment variable is already set.") - return nil - } - - // Try to detect the MozillaBuild installation directory - defaultPaths := []string{ - "C:\\mozilla-build", - "C:\\Program Files\\mozilla-build", - "C:\\Program Files (x86)\\mozilla-build", - } - - for _, path := range defaultPaths { - if _, err := os.Stat(path); !os.IsNotExist(err) { - // Set the MOZILLABUILD environment variable - fmt.Printf("Setting MOZILLABUILD environment variable to: %s\n", path) - return os.Setenv("MOZILLABUILD", path) +// SetGlobalEnv sets the MOZILLABUILD environment variable globally for the user (or system) +func SetGlobalEnv(variable, value string, scope string) error { + if runtime.GOOS == "windows" { + var cmd *exec.Cmd + if scope == "user" { + cmd = exec.Command("setx", variable, value) // Set for current user + } else if scope == "system" { + cmd = exec.Command("setx", variable, value, "/M") // Set for system (requires admin privileges) + } else { + return fmt.Errorf("unknown scope: %s", scope) } - } - // If no directory was found, return an error - return fmt.Errorf("MozillaBuild directory not found. Please install MozillaBuild or set the MOZILLABUILD environment variable manually.") + // Run the command + out, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to set environment variable %s=%s: %v\nOutput: %s", variable, value, err, string(out)) + } + fmt.Printf("Successfully set %s=%s\n", variable, value) + return nil + } else { + return fmt.Errorf("global environment variable setting is not supported on non-Windows systems") + } } // Run an external command like scp or rsync