main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/exec"
  8. "regexp"
  9. "strings"
  10. "time"
  11. )
  12. func main() {
  13. var pkg string
  14. var dst string
  15. flag.StringVar(&pkg, "package", "", "Set package name")
  16. flag.StringVar(&dst, "out", "", "Output file")
  17. flag.Parse()
  18. if pkg == "" {
  19. log.Println("Missing argument -package")
  20. flag.Usage()
  21. return
  22. }
  23. if dst == "" {
  24. log.Println("Missing argument -out")
  25. flag.Usage()
  26. return
  27. }
  28. tag, err := exec.Command("git", "describe", "--tags").Output()
  29. if err != nil {
  30. log.Fatal("Error Getting tag", err)
  31. }
  32. re := regexp.MustCompile(`\r?\n`)
  33. vtag := re.ReplaceAll(tag, []byte(" "))
  34. version := fmt.Sprintf("%s - built: %s", strings.TrimSpace(string(vtag)), time.Now().UTC().Format("2006-01-02 15:04:05 UTC"))
  35. fmt.Printf("Version: %s\n", version)
  36. f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  37. if err != nil {
  38. fmt.Println("Error opening file", err)
  39. return
  40. }
  41. fmt.Fprintf(f, "package %s\n", pkg)
  42. fmt.Fprintln(f, "\nconst (")
  43. fmt.Fprintf(f, " //Version contains version of the package\n")
  44. fmt.Fprintf(f, " Version = \"%s\"\n", version)
  45. fmt.Fprintln(f, ")")
  46. f.Sync()
  47. f.Close()
  48. }