From 61514b48329117c90119c239bf3f56be85f59959 Mon Sep 17 00:00:00 2001 From: SteVen Batten Date: Fri, 13 Aug 2021 14:06:22 -0700 Subject: [PATCH] initial commit --- go.mod | 3 ++ main.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b48dafc --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.16 diff --git a/main.go b/main.go new file mode 100644 index 0000000..247cf09 --- /dev/null +++ b/main.go @@ -0,0 +1,92 @@ +package main + +import ( + "fmt" + "io" + "io/ioutil" + "net/http" + "os" + "os/exec" + "runtime" + "strings" +) + +func main() { + arch := GetSystemInfo() + installer_dir := SetupTemporaryDirectory(arch) + installer_path := DownloadInstaller(installer_dir, arch) + RunInstaller(installer_path) + Cleanup(installer_dir) +} + +func GetSystemInfo() (arch string) { + switch runtime.GOARCH { + case "amd64": + arch = "x64" + case "386": + arch = "ia32" + case "arm64": + arch = "arm64" + } + + return +} + +func SetupTemporaryDirectory(arch string) (installer_dir string) { + installer_dir, err := ioutil.TempDir("", "vscode-winsta11er") + checkError(err) + + return installer_dir +} + +func DownloadInstaller(installer_dir, arch string) (installer_path string) { + var downloadUrl = strings.Replace("https://update.code.visualstudio.com/latest/win32-$arch-user/stable", "$arch", arch, 1) + + fmt.Printf("Downloading installer from %s.\n", downloadUrl) + + file, err := os.CreateTemp(installer_dir, strings.Replace("vscode-win32-$arch-user*.exe", "$arch", arch, 1)) + checkError(err) + + client := http.Client{ + CheckRedirect: func(r *http.Request, via []*http.Request) error { + r.URL.Opaque = r.URL.Path + return nil + }, + } + + // Put content on file + resp, err := client.Get(downloadUrl) + checkError(err) + defer resp.Body.Close() + + _, err = io.Copy(file, resp.Body) + checkError(err) + defer file.Close() + + fmt.Printf("Downloaded installer to file %s.\n", file.Name()) + installer_path = file.Name() + + return installer_path +} + +func RunInstaller(installer_path string) { + path, err := exec.LookPath(installer_path) + checkError(err) + + cmd := exec.Command(path, "/verysilent", "/mergetasks=!runcode") + stdout, err := cmd.Output() + checkError(err) + + fmt.Println(string(stdout)) +} + +func Cleanup(installer_dir string) { + err := os.RemoveAll(installer_dir) + checkError(err) +} + +func checkError(e error) { + if e != nil { + panic(e) + } +}