CleanEnv clear all environments that not required and reset PATH.
()
| 13 | |
| 14 | // CleanEnv clear all environments that not required and reset PATH. |
| 15 | func CleanEnv() { |
| 16 | // Cache necessary environments. |
| 17 | temp := os.Getenv("TEMP") |
| 18 | tmp := os.Getenv("TMP") |
| 19 | operatingSystem := os.Getenv("OS") |
| 20 | homeDriver := os.Getenv("HOMEDRIVE") |
| 21 | homePath := os.Getenv("HOMEPATH") |
| 22 | username := os.Getenv("USERNAME") |
| 23 | userProfile := os.Getenv("USERPROFILE") |
| 24 | systemRoot := os.Getenv("SystemRoot") |
| 25 | systemDrive := os.Getenv("SystemDrive") |
| 26 | localAppData := os.Getenv("LOCALAPPDATA") |
| 27 | processorArchitecture := os.Getenv("PROCESSOR_ARCHITECTURE") |
| 28 | processorIdentifier := os.Getenv("PROCESSOR_IDENTIFIER") |
| 29 | processorLevel := os.Getenv("PROCESSOR_LEVEL") |
| 30 | processorRevision := os.Getenv("PROCESSOR_REVISION") |
| 31 | numberOfProcessors := os.Getenv("NUMBER_OF_PROCESSORS") |
| 32 | portsRepo := os.Getenv("CELER_PORTS_REPO") |
| 33 | githubActions := os.Getenv("GITHUB_ACTIONS") |
| 34 | |
| 35 | os.Clearenv() |
| 36 | |
| 37 | // Restore necessary environemnts. |
| 38 | setEnvIfNotEmpty("TEMP", temp) |
| 39 | setEnvIfNotEmpty("TMP", tmp) |
| 40 | setEnvIfNotEmpty("OS", operatingSystem) |
| 41 | setEnvIfNotEmpty("HOMEDRIVE", homeDriver) |
| 42 | setEnvIfNotEmpty("HOMEPATH", homePath) |
| 43 | setEnvIfNotEmpty("USERNAME", username) |
| 44 | setEnvIfNotEmpty("USERPROFILE", userProfile) |
| 45 | setEnvIfNotEmpty("SystemRoot", systemRoot) |
| 46 | setEnvIfNotEmpty("SystemDrive", systemDrive) |
| 47 | setEnvIfNotEmpty("LOCALAPPDATA", localAppData) |
| 48 | setEnvIfNotEmpty("PROCESSOR_ARCHITECTURE", processorArchitecture) |
| 49 | setEnvIfNotEmpty("PROCESSOR_IDENTIFIER", processorIdentifier) |
| 50 | setEnvIfNotEmpty("PROCESSOR_LEVEL", processorLevel) |
| 51 | setEnvIfNotEmpty("PROCESSOR_REVISION", processorRevision) |
| 52 | setEnvIfNotEmpty("NUMBER_OF_PROCESSORS", numberOfProcessors) |
| 53 | setEnvIfNotEmpty("CELER_PORTS_REPO", portsRepo) |
| 54 | setEnvIfNotEmpty("GITHUB_ACTIONS", githubActions) |
| 55 | |
| 56 | // Reset PATH. |
| 57 | var paths []string |
| 58 | paths = append(paths, `C:\Windows`) |
| 59 | paths = append(paths, `C:\Windows\System32`) |
| 60 | paths = append(paths, `C:\Windows\SysWOW64`) |
| 61 | paths = append(paths, `C:\Windows\System32\Wbem`) |
| 62 | paths = append(paths, `C:\Windows\System32\downlevel`) |
| 63 | paths = append(paths, `C:\Windows\SysWOW64\WindowsPowerShell\v1.0`) |
| 64 | paths = append(paths, `C:\Windows\System32\WindowsPowerShell\v1.0`) |
| 65 | paths = append(paths, `C:\ProgramData\chocolatey\bin`) |
| 66 | |
| 67 | // Use PATH instead of Path. |
| 68 | os.Unsetenv("Path") |
| 69 | os.Setenv("PATH", env.JoinPaths("PATH", paths...)) |
| 70 | os.Setenv("PYTHONUSERBASE", dirs.PythonUserBase) |
| 71 | } |
| 72 |
nothing calls this directly
no test coverage detected