Skip to content

Commit

Permalink
Added smd-init/main.go changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shunr-hpe committed Jan 22, 2025
1 parent 92702ce commit 5c6019f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cmd/smd-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (
"strconv"
"time"

"github.com/Cray-HPE/hms-smd/v2/internal/hmsds"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
"github.com/Cray-HPE/hms-smd/v2/internal/hmsds"
)

const APP_VERSION = "1"
Expand All @@ -49,6 +49,7 @@ var dbPortStr string
var dbPort int
var dbOpts string
var forceStep int
var migrationsDir string
var migrateStep uint
var fresh *bool
var versionFlag *bool
Expand All @@ -61,6 +62,7 @@ func parseCmdLine() {
flag.StringVar(&dbPortStr, "dbport", "", "Database port")
flag.StringVar(&dbOpts, "dbopts", "", "Database options string")
flag.IntVar(&forceStep, "f", -1, "Force migration to step X")
flag.StringVar(&migrationsDir, "migrationsdir", "/persistent_migrations", "Directory with migrations v4 files")
fresh = flag.Bool("fresh", false,
"Revert all schemas before installing (drops all data)")
versionFlag = flag.Bool("v", false, "Print the version number.")
Expand Down Expand Up @@ -147,7 +149,7 @@ func parseCmdLine() {
migrateStep = uint(SCHEMA_STEPS)
}
envvar = "SMD_FRESH"
if *fresh == false {
if !*fresh {
if val := os.Getenv(envvar); val != "" {
*fresh = true
}
Expand All @@ -172,6 +174,7 @@ func parseCmdLine() {
var lg = log.New(os.Stdout, "", log.Lshortfile|log.LstdFlags|log.Lmicroseconds)

func main() {
PrintVersionInfo()
parseCmdLine()

lg.Printf("smd-init: Starting...")
Expand Down Expand Up @@ -215,7 +218,7 @@ func main() {
lg.Printf("Creating postgres driver succeeded")

m, err := migrate.NewWithDatabaseInstance(
"file:///persistent_migrations",
"file://"+migrationsDir,
"postgres", driver)
if err != nil {
lg.Printf("Creating migration failed: '%s'", err)
Expand Down Expand Up @@ -258,7 +261,7 @@ func main() {
} else {
lg.Printf("Migration: At step version %d, dirty: %t", version, dirty)
}
if dirty == true && forceStep < 0 {
if dirty && forceStep < 0 {
// Force current version to remove dirty flag. We'd prefer to avoid
// this situation in the first place.
err = m.Force(int(version))
Expand Down
58 changes: 58 additions & 0 deletions cmd/smd-init/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import "fmt"

// GitCommit stores the latest Git commit hash.
// Set via -ldflags "-X main.GitCommit=$(git rev-parse HEAD)"
var GitCommit string

// BuildTime stores the build timestamp in UTC.
// Set via -ldflags "-X main.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
var BuildTime string

// Version indicates the version of the binary, such as a release number or semantic version.
// Set via -ldflags "-X main.Version=v1.0.0"
var Version string

// GitBranch holds the name of the Git branch from which the build was created.
// Set via -ldflags "-X main.GitBranch=$(git rev-parse --abbrev-ref HEAD)"
var GitBranch string

// GitTag represents the most recent Git tag at build time, if any.
// Set via -ldflags "-X main.GitTag=$(git describe --tags --abbrev=0)"
var GitTag string

// GitState indicates whether the working directory was "clean" or "dirty" (i.e., with uncommitted changes).
// Set via -ldflags "-X main.GitState=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)"
var GitState string

// BuildHost stores the hostname of the machine where the binary was built.
// Set via -ldflags "-X main.BuildHost=$(hostname)"
var BuildHost string

// GoVersion captures the Go version used to build the binary.
// Typically, this can be obtained automatically with runtime.Version(), but you can set it manually.
// Set via -ldflags "-X main.GoVersion=$(go version | awk '{print $3}')"
var GoVersion string

// BuildUser is the username of the person or system that initiated the build process.
// Set via -ldflags "-X main.BuildUser=$(whoami)"
var BuildUser string

// PrintVersionInfo outputs all versioning information for troubleshooting or version checks.
func PrintVersionInfo() {
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Git Commit: %s\n", GitCommit)
fmt.Printf("Build Time: %s\n", BuildTime)
fmt.Printf("Git Branch: %s\n", GitBranch)
fmt.Printf("Git Tag: %s\n", GitTag)
fmt.Printf("Git State: %s\n", GitState)
fmt.Printf("Build Host: %s\n", BuildHost)
fmt.Printf("Go Version: %s\n", GoVersion)
fmt.Printf("Build User: %s\n", BuildUser)
}

func VersionInfo() string {
return fmt.Sprintf("Version: %s, Git Commit: %s, Build Time: %s, Git Branch: %s, Git Tag: %s, Git State: %s, Build Host: %s, Go Version: %s, Build User: %s",
Version, GitCommit, BuildTime, GitBranch, GitTag, GitState, BuildHost, GoVersion, BuildUser)
}

0 comments on commit 5c6019f

Please sign in to comment.