diff --git a/main.go b/main.go index 66ad3ca..389a80e 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,42 @@ import ( "os" "os/exec" ) -//? + +const warning = "//WARNING! DO NOT EDIT THIS FILE BY HAND. IT IS AUTO GENERATED.\n" + const header = "package main\nconst gitVersion = \"" +const code = ` +import ( + "fmt" + "os" + + "github.com/spf13/pflag" +) + +func init() { + pflag.ErrHelp = nil + versionFlagSet := pflag.NewFlagSet("version", pflag.ContinueOnError) + versionFlagSet.ParseErrorsWhitelist.UnknownFlags = true + versionFlagSet.SetOutput(nil) + + versionIsAsked := versionFlagSet.BoolP("version", "v", false, "Prints version of this application.") + versionFlagSet.BoolP("help", "h", false, "") // This line is needed to supress (redefine) built-in help flag. + + err := versionFlagSet.Parse(os.Args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if *versionIsAsked { + fmt.Println("Simple Serial version: " + Value) + os.Exit(0) + } +} + +` + func main() { cmd := exec.Command("git", "describe", "--tags", "--abbrev=0") out, err := cmd.Output() @@ -18,7 +51,7 @@ func main() { } version := string(out) version = version[:len(version)-1] - content := header + version + "\"" + content := warning + header + version + "\"" + code ioutil.WriteFile("version.go", []byte(content), 0644) }