-
Notifications
You must be signed in to change notification settings - Fork 1
/
packer.go
75 lines (62 loc) · 2.23 KB
/
packer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"os/exec"
"syscall"
)
func (a *App) CheckChoco() string {
return checkChoco()
}
func (a *App) InstallApp(app string) string {
cmd := exec.Command("powershell", "-Command", "choco install -y "+app)
// Hide the PowerShell window:
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error executing Chocolatey installation script:", err)
fmt.Println("Output:", string(output))
return "false"
}
fmt.Println("Chocolatey installation completed successfully.")
return checkChoco()
}
func checkChoco() string {
cmd := exec.Command("powershell", "-Command", "choco --version")
// Hide the PowerShell window:
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error executing Chocolatey version check:", err)
fmt.Println("Output:", string(output))
return "Error"
}
return string(output)
}
func InstallChoco() string {
if !isAdmin() {
fmt.Println("Administrative privileges are required to install Chocolatey.")
fmt.Println("Please run this program as an administrator.")
return "false"
}
cmd := exec.Command("powershell", "-windowstyle hidden", "-Command", `
Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))`)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error executing Chocolatey installation script:", err)
fmt.Println("Output:", string(output))
return "false"
}
fmt.Println("Chocolatey installation completed successfully.")
return checkChoco()
}
func isAdmin() bool {
cmd := exec.Command("powershell", "-Command", "$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()); $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error checking administrative privileges:", err)
return false
}
return output[0] == "True"[0]
}