summaryrefslogtreecommitdiffstats
path: root/scripts/push_new_patch/main.go
blob: b1660d3c14bb2eafc9af795f0068531ff1ed0a55 (plain)
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
// call from project root with
// go run scripts/push_new_patch/main.go

// goreleaser expects a $GITHUB_TOKEN env variable to be defined
// in order to push the release got github

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strconv"
	"strings"
)

func main() {

	version, err := ioutil.ReadFile("VERSION")
	if err != nil {
		log.Panicln(err.Error())
	}
	stringVersion := string(version)
	fmt.Println("VERSION was " + stringVersion)

	runCommand("git", "pull")

	splitVersion := strings.Split(stringVersion, ".")
	patch := splitVersion[len(splitVersion)-1]
	newPatch, err := strconv.Atoi(patch)
	splitVersion[len(splitVersion)-1] = strconv.FormatInt(int64(newPatch)+1, 10)
	newVersion := strings.Join(splitVersion, ".")

	err = ioutil.WriteFile("VERSION", []byte(newVersion), 0644)
	if err != nil {
		log.Panicln(err.Error())
	}

	runCommand("git", "add", "VERSION")
	runCommand("git", "commit", "-m", "bump version to "+newVersion, "--", "VERSION")
	runCommand("git", "push")
	runCommand("git", "tag", newVersion)
	runCommand("git", "push", "origin", newVersion)
	runCommand("goreleaser", "--rm-dist")
	runCommand("rm", "-rf", "dist")
}

func runCommand(args ...string) {
	fmt.Println(strings.Join(args, " "))
	cmd := exec.Command(args[0], args[1:]...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Start()
	if err != nil {
		panic(err.Error())
	}
	err = cmd.Wait()
	if err != nil {
		panic(err.Error())
	}
}