diff --git a/.goreleaser.yml b/.goreleaser.yml index 89b934037..a1c7d7630 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -42,7 +42,7 @@ builds: tags: - forceposix ldflags: - - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser -X github.com/pelicanplatform/pelican/config.version={{.Version}} + - -s -w -X github.com/pelicanplatform/pelican/config.commit={{.Commit}} -X github.com/pelicanplatform/pelican/config.date={{.Date}} -X github.com/pelicanplatform/pelican/config.builtBy=goreleaser -X github.com/pelicanplatform/pelican/config.version={{.Version}} ignore: - goos: windows goarch: arm64 diff --git a/cmd/main.go b/cmd/main.go index be2edbd21..8db9de1c7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,7 +19,6 @@ package main import ( - "fmt" "os" "path/filepath" "strings" @@ -56,10 +55,7 @@ func handleCLI(args []string) error { // version info regardless of the commands and whether they are defined // * Remove the -v shorthand since in "origin serve" flagset it's already used for "volume" flag if args[len(args)-1] == "--version" { - fmt.Println("Version:", config.GetVersion()) - fmt.Println("Build Date:", date) - fmt.Println("Build Commit:", commit) - fmt.Println("Built By:", builtBy) + config.PrintPelicanVersion() return nil } err := Execute() diff --git a/cmd/main_test.go b/cmd/main_test.go index b87cf6b92..4f6dc808d 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -32,23 +32,39 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHandleCLIVersionFlag(t *testing.T) { // Save the current version to reset this variable - var currentVersion = config.GetVersion() - config.SetVersion("0.0.1") - date = "2023-10-06T15:26:50Z" - commit = "f0f94a3edf6641c2472345819a0d5453fc9e68d1" - builtBy = "goreleaser" - + currentVersion := config.GetVersion() + currentDate := config.GetBuiltDate() + currentCommit := config.GetBuiltCommit() + currentBuiltBy := config.GetBuiltBy() // Reset os.Args to ensure Windows doesn't do weird things to the test oldArgs := os.Args + + config.SetVersion("0.0.1") + config.SetBuiltDate("2023-10-06T15:26:50Z") + config.SetBuiltCommit("f0f94a3edf6641c2472345819a0d5453fc9e68d1") + config.SetBuiltBy("goreleaser") + + t.Cleanup(func() { + // Restore the args back when test finished + os.Args = oldArgs + + // Set the version back to what it was + config.SetVersion(currentVersion) + config.SetBuiltDate(currentDate) + config.SetBuiltCommit(currentCommit) + config.SetBuiltBy(currentBuiltBy) + }) + os.Args = []string{os.Args[0]} mockVersionOutput := fmt.Sprintf( "Version: %s\nBuild Date: %s\nBuild Commit: %s\nBuilt By: %s", - config.GetVersion(), date, commit, builtBy, + config.GetVersion(), config.GetBuiltDate(), config.GetBuiltCommit(), config.GetBuiltBy(), ) testCases := []struct { @@ -92,20 +108,40 @@ func TestHandleCLIVersionFlag(t *testing.T) { } batchTest := func(t *testing.T, arguments []string, expected string) { - // Redirect output to a pip - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w + got := "" + + if expected == mockVersionOutput { + // Redirect output to a pip + oldStderr := os.Stderr + r, w, _ := os.Pipe() + os.Stderr = w - err := handleCLI(arguments) + err := handleCLI(arguments) + require.NoError(t, err) - // Close the write of pip and redirect output back to stdout - w.Close() - out, _ := io.ReadAll(r) - os.Stdout = oldStdout + // Close the write of pip and redirect output back to Stderr + w.Close() + out, _ := io.ReadAll(r) + os.Stderr = oldStderr + + got = strings.TrimSpace(string(out)) + } else { + // Redirect output to a pip + oldStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + err := handleCLI(arguments) + require.NoError(t, err) + + // Close the write of pip and redirect output back to Stderr + w.Close() + out, _ := io.ReadAll(r) + os.Stdout = oldStdout + + got = strings.TrimSpace(string(out)) + } - got := strings.TrimSpace(string(out)) - assert.NoError(t, err, "Should not have error running the function") if expected != mockVersionOutput { // If the expected string is not the version output, use Contains to check // This is mainly for checking against command help output @@ -120,12 +156,6 @@ func TestHandleCLIVersionFlag(t *testing.T) { batchTest(t, tc.args, tc.expected) }) } - - // Restore the args back when test finished - os.Args = oldArgs - - // Set the version back to what it was - config.SetVersion(currentVersion) } func TestHandleCLIExecutableAlias(t *testing.T) { diff --git a/cmd/object_copy.go b/cmd/object_copy.go index 85ded6cb6..d0daed41f 100644 --- a/cmd/object_copy.go +++ b/cmd/object_copy.go @@ -104,10 +104,7 @@ func copyMain(cmd *cobra.Command, args []string) { } if val, err := cmd.Flags().GetBool("version"); err == nil && val { - fmt.Println("Version:", config.GetVersion()) - fmt.Println("Build Date:", date) - fmt.Println("Build Commit:", commit) - fmt.Println("Built By:", builtBy) + config.PrintPelicanVersion() os.Exit(0) } diff --git a/cmd/plugin.go b/cmd/plugin.go index 1ad0df85d..e03c6650b 100644 --- a/cmd/plugin.go +++ b/cmd/plugin.go @@ -42,10 +42,6 @@ import ( ) var ( - commit = "none" - date = "unknown" - builtBy = "unknown" - // Holds the various plugin commands rootPluginCmd = &cobra.Command{ Use: "plugin", @@ -133,10 +129,7 @@ func stashPluginMain(args []string) { fmt.Println("SupportedMethods = \"stash, osdf\"") os.Exit(0) } else if args[0] == "-version" || args[0] == "-v" { - fmt.Println("Version:", config.GetVersion()) - fmt.Println("Build Date:", date) - fmt.Println("Build Commit:", commit) - fmt.Println("Built By:", builtBy) + config.PrintPelicanVersion() os.Exit(0) } else if args[0] == "-upload" { log.Debugln("Upload detected") diff --git a/config/config.go b/config/config.go index 56e473d92..f5cee1606 100644 --- a/config/config.go +++ b/config/config.go @@ -112,6 +112,15 @@ const ( TokenSharedRead ) +// This block of variables will be overwritten at build time +var ( + commit = "none" + date = "unknown" + builtBy = "unknown" + // Pelican version + version = "dev" +) + var ( // Some of the unit tests probe behavior specific to OSDF vs Pelican. Hence, // we need a way to override the preferred prefix. @@ -139,9 +148,6 @@ var ( RestartFlag = make(chan any) // A channel flag to restart the server instance that launcher listens to (including cache) - // Pelican version, this is overwritten at build time - version string = "dev" - MetadataTimeoutErr *MetadataErr = &MetadataErr{msg: "Timeout when querying metadata"} ) @@ -239,6 +245,30 @@ func SetVersion(newVersion string) { version = newVersion } +func GetBuiltCommit() string { + return commit +} + +func SetBuiltCommit(newCommit string) { + commit = newCommit +} + +func GetBuiltDate() string { + return date +} + +func SetBuiltDate(builtDate string) { + date = builtDate +} + +func GetBuiltBy() string { + return builtBy +} + +func SetBuiltBy(newBuiltBy string) { + builtBy = newBuiltBy +} + // Get a string slice of currently enabled servers, sorted by alphabetical order. // By default, it calls String method of each enabled server. // To get strings in lowerCase, set lowerCase = true. @@ -803,6 +833,13 @@ func setXrootdRunLocations(currentServers ServerType, dir string) error { return nil } +func PrintPelicanVersion() { + fmt.Fprintln(os.Stderr, "Version:", GetVersion()) + fmt.Fprintln(os.Stderr, "Build Date:", GetBuiltDate()) + fmt.Fprintln(os.Stderr, "Build Commit:", GetBuiltCommit()) + fmt.Fprintln(os.Stderr, "Built By:", GetBuiltBy()) +} + // Print Pelican configuration to stderr func PrintConfig() error { rawConfig, err := param.UnmarshalConfig() diff --git a/launchers/launcher.go b/launchers/launcher.go index 696ba74bd..995d10506 100644 --- a/launchers/launcher.go +++ b/launchers/launcher.go @@ -57,6 +57,8 @@ func LaunchModules(ctx context.Context, modules config.ServerType) (context.Canc ctx, shutdownCancel := context.WithCancel(ctx) + config.PrintPelicanVersion() + // Print Pelican config at server start if it's in debug or info level if log.GetLevel() >= log.InfoLevel { if err := config.PrintConfig(); err != nil {