Skip to content

Commit

Permalink
fix: ensure that both binary (goreleaser) and source (go install) bui…
Browse files Browse the repository at this point in the history
…lds have a version number applied
  • Loading branch information
a-h committed Jun 7, 2021
1 parent ef26858 commit 7f62d8c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,32 @@ import (
"flag"
"fmt"
"os"
"runtime/debug"

"github.com/a-h/templ/cmd/templ/fmtcmd"
"github.com/a-h/templ/cmd/templ/generatecmd"
"github.com/a-h/templ/cmd/templ/lspcmd"
)

var version = "devel"
// Binary builds set this version string. goreleaser sets the value using Go build ldflags.
var version string

// Source builds use this value. When installed using `go install github.com/a-h/templ/cmd/templ@latest` the `version` variable is empty, but
// the debug.ReadBuildInfo return value provides the package version number installed by `go install`
func goInstallVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return info.Main.Version
}

func getVersion() string {
if version != "" {
return version
}
return goInstallVersion()
}

func main() {
if len(os.Args) < 2 {
Expand Down

0 comments on commit 7f62d8c

Please sign in to comment.