Skip to content

Commit

Permalink
feat: add -version arg
Browse files Browse the repository at this point in the history
Very handy to know what version you have laying around locally.
This is preparation for releasing pre-built assets when publishing a new
release.
  • Loading branch information
stigok committed Jan 11, 2024
1 parent ac8c271 commit 3d16771
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Command line arguments:
- `-tls-key-file`: Path to TLS certificate private key file
- `-log-level`: Log level selection: `debug`, `info`, `warn`, `error` (default: `info`)
- `-log-format`: Log output format selection: `json`, `console` (default: `console`)
- `-version`: Print version info and exit

Additionally, depending on the selected store type, some options are described
in the next subsections.
Expand Down
50 changes: 50 additions & 0 deletions cmd/terraform-registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"net/http"
"os"
"regexp"
"runtime"
"runtime/debug"
"strings"
"time"

Expand All @@ -39,6 +41,7 @@ var (
storeType string
logLevelStr string
logFormatStr string
printVersionInfo bool

S3Region string
S3Bucket string
Expand All @@ -55,9 +58,18 @@ var (
// https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html
patternEnvVarName = regexp.MustCompile(`^[A-Z_][A-Z0-9_]*`)

// These variables are set at build time using ldflags.
version = "(devel)"
buildDate = "unknown"

logger *zap.Logger = zap.NewNop()
)

const (
developmentVersion = "(devel)"
programName = "terraform-registry"
)

func init() {
flag.StringVar(&listenAddr, "listen-addr", ":8080", "")
flag.BoolVar(&accessLogDisabled, "access-log-disabled", false, "")
Expand All @@ -71,6 +83,7 @@ func init() {
flag.StringVar(&storeType, "store", "", "Store backend to use (choices: github, s3)")
flag.StringVar(&logLevelStr, "log-level", "info", "Levels: debug, info, warn, error")
flag.StringVar(&logFormatStr, "log-format", "console", "Formats: json, console")
flag.BoolVar(&printVersionInfo, "version", false, "Print version info and exit")

flag.StringVar(&gitHubOwnerFilter, "github-owner-filter", "", "GitHub org/user repository filter")
flag.StringVar(&gitHubTopicFilter, "github-topic-filter", "", "GitHub topic repository filter")
Expand All @@ -87,6 +100,11 @@ func main() {
os.Exit(1)
}

if printVersionInfo {
fmt.Printf("%s %s\n", programName, versionString())
os.Exit(0)
}

// Configure logging
logConfig := zap.NewProductionConfig()
logLevel, err := zap.ParseAtomicLevel(logLevelStr)
Expand Down Expand Up @@ -345,3 +363,35 @@ func setEnvironmentFromJSONFile(prefix, filename string) error {
}
return nil
}

// versionString returns a string with version information for this program,
// like `v5.0.4-0.20230601165947-6ce0bf390ce3 linux amd64` for release builds,
// or `(devel).unknown-478ce46fb3ab76445001e614fec7ff1dd0c6cfe0 linux amd64` for local builds.
func versionString() string {
v := struct {
Version string
BuildDate string
GitCommit string
GoArch string
GoOS string
GoVersion string
}{
Version: version,
BuildDate: buildDate,
GitCommit: "unknown",
GoArch: runtime.GOARCH,
GoOS: runtime.GOOS,
GoVersion: runtime.Version(),
}

info, ok := debug.ReadBuildInfo()
if ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
v.GitCommit = setting.Value
}
}
}

return fmt.Sprintf("%s.%s-%s %s %s", v.Version, v.BuildDate, v.GitCommit, v.GoOS, v.GoArch)
}

0 comments on commit 3d16771

Please sign in to comment.