version/main.go

43 lines
741 B
Go
Raw Normal View History

2025-04-20 01:52:40 +03:00
package main
2025-04-20 01:04:48 +03:00
2025-04-20 02:00:13 +03:00
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
)
2025-04-20 02:25:31 +03:00
const warning = "//WARNING! DO NOT EDIT THIS FILE BY HAND. IT IS AUTO GENERATED.\n"
2025-04-20 02:11:52 +03:00
const header = "package main\nconst gitVersion = \""
2025-04-20 02:00:13 +03:00
2025-04-20 02:25:31 +03:00
const code = `
import (
"fmt"
"os"
)
func init() {
2025-04-20 02:43:47 +03:00
for _, arg := range os.Args {
if arg == "-v" || arg == "--version" {
fmt.Println(gitVersion)
os.Exit(0)
}
}
2025-04-20 02:25:31 +03:00
}
`
2025-04-20 02:00:13 +03:00
func main() {
cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
out, err := cmd.Output()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
version := string(out)
version = version[:len(version)-1]
2025-04-20 02:25:31 +03:00
content := warning + header + version + "\"" + code
2025-04-20 02:00:13 +03:00
ioutil.WriteFile("version.go", []byte(content), 0644)
2025-04-20 01:04:48 +03:00
}