croc/src/install/updateversion.go

65 lines
1.2 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"io/ioutil"
2019-05-02 22:23:23 +02:00
"os/exec"
"strings"
)
func main() {
err := run()
if err != nil {
fmt.Println(err)
}
}
func run() (err error) {
2019-05-02 22:23:23 +02:00
version, err := exec.Command("git", "describe").Output()
if err != nil {
return
}
2019-05-02 22:25:03 +02:00
versionNew := strings.TrimSpace(string(version))
2019-05-02 22:25:03 +02:00
err = replaceInFile("src/cli/cli.go", `Version = "`, `"`, versionNew)
if err == nil {
fmt.Printf("updated cli.go to version %s\n", versionNew)
}
2019-05-02 22:23:23 +02:00
return
}
func replaceInFile(fname, start, end, replacement string) (err error) {
b, err := ioutil.ReadFile(fname)
if err != nil {
return
}
2019-05-02 22:23:23 +02:00
oldVersion := GetStringInBetween(string(b), start, end)
if oldVersion == "" {
err = fmt.Errorf("nothing")
return
}
2019-05-02 22:23:23 +02:00
newF := strings.Replace(
string(b),
fmt.Sprintf("%s%s%s", start, oldVersion, end),
fmt.Sprintf("%s%s%s", start, replacement, end),
1,
)
err = ioutil.WriteFile(fname, []byte(newF), 0644)
return
}
// GetStringInBetween Returns empty string if no start string found
func GetStringInBetween(str string, start string, end string) (result string) {
s := strings.Index(str, start)
if s == -1 {
return
}
s += len(start)
e := strings.Index(str[s:], end)
if e == -1 {
return
}
e += s
return str[s:e]
}