Created
December 5, 2016 15:22
-
-
Save jorinvo/5fd1eb28de977139cd52f93d1ee48ae2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"archive/zip" | |
"fmt" | |
"io" | |
"os" | |
"os/exec" | |
"path/filepath" | |
) | |
const ( | |
binary = "gh-backup" | |
usage = "Usage: go run release <version>" | |
directory = "releases" | |
) | |
var releases = []struct{ tag, os, arch string }{ | |
{"Windows-64bit", "windows", "amd64"}, | |
{"MacOS-64bit", "darwin", "amd64"}, | |
{"Linux-64bit", "linux", "amd64"}, | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Println(usage) | |
os.Exit(1) | |
} | |
version := os.Args[1] | |
// ensure directory | |
fatal(os.MkdirAll(directory, os.ModePerm)) | |
for _, b := range releases { | |
release(b.tag, b.os, b.arch, version) | |
} | |
} | |
func release(tag, system, arch, version string) { | |
target := binary + "-" + version + "-" + tag | |
zipFile := filepath.Join(directory, target+".zip") | |
print("Building %s", tag) | |
build(system, arch) | |
print("Creating zip file %s", zipFile) | |
createZip(zipFile, target, "README.md", binary) | |
fatal(os.Remove(binary)) | |
} | |
func build(system, arch string) { | |
cmd := exec.Command("go", "build", "-o="+binary) | |
cmd.Env = []string{"GOOS=" + system, "GOARCH=" + arch} | |
cmd.Stderr = os.Stderr | |
err := cmd.Run() | |
fatal(err) | |
} | |
func createZip(filename, target string, files ...string) { | |
zipFile, err := os.Create(filename) | |
fatal(err) | |
defer func() { | |
fatal(zipFile.Close()) | |
}() | |
z := zip.NewWriter(zipFile) | |
defer func() { | |
fatal(z.Close()) | |
}() | |
for _, f := range files { | |
r, err := os.Open(f) | |
fatal(err) | |
w, err := z.Create(filepath.Join(target, f)) | |
fatal(err) | |
_, err = io.Copy(w, r) | |
fatal(err) | |
} | |
} | |
func print(s string, args ...interface{}) { | |
fmt.Fprintf(os.Stderr, s+"\n", args...) | |
} | |
func fatal(err error) { | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment