-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (143 loc) · 3.45 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package chocolatey
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"
"time"
"golang.org/x/sys/windows"
)
func init() {
if !amAdmin() {
runMeElevated()
}
time.Sleep(3 * time.Second)
}
func Install() {
cmd := exec.Command("powershell", "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'))")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func InstallPackage(packageName string, args string) {
log.Println("Running: choco install " + packageName + " " + args)
cmd := exec.Command("choco", "install", packageName, args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
return
}
}
func UninstallPackage(packageName string, args string) {
log.Println("Running: choco uninstall " + packageName + " " + args)
cmd := exec.Command("choco", "uninstall", packageName, args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
return
}
}
func UpdatePackage(packageName string, args string) {
log.Println("Running: choco upgrade " + packageName + " " + args)
cmd := exec.Command("choco", "upgrade", packageName, args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
return
}
}
func UpdateAllPackages(args string) {
log.Println("Running: choco upgrade all " + args)
cmd := exec.Command("choco", "upgrade", "all", args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
return
}
}
func ListPackages(args string) {
log.Println("Running: choco list " + args)
cmd := exec.Command("choco", "list", args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
return
}
}
func ListInstalledPackages(args string) {
log.Println("Running: choco list --localonly" + args)
cmd := exec.Command("choco", "list", "--localonly", args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
return
}
}
func ListOutdatedPackages(args string) {
log.Println("Running: choco list --outdated" + args)
cmd := exec.Command("choco", "list", "--outdated", args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
return
}
}
// Check if chocolatey is installed
func IsInstalled() bool {
_, err := exec.LookPath("choco")
if err != nil {
return false
}
return true
}
func runMeElevated() {
verb := "runas"
exe, _ := os.Executable()
cwd, _ := os.Getwd()
args := strings.Join(os.Args[1:], " ")
verbPtr, _ := syscall.UTF16PtrFromString(verb)
exePtr, _ := syscall.UTF16PtrFromString(exe)
cwdPtr, _ := syscall.UTF16PtrFromString(cwd)
argPtr, _ := syscall.UTF16PtrFromString(args)
var showCmd int32 = 1 //SW_NORMAL
err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, showCmd)
if err != nil {
fmt.Println(err)
}
}
func amAdmin() bool {
_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
if err != nil {
fmt.Println("admin no")
return false
}
fmt.Println("admin yes")
return true
}