-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): dynamically set version based on environment (#3693)
Co-authored-by: q9f <q9f@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2024 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package commands | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
cfg "github.com/ChainSafe/gossamer/config" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const RegExVersion = "^([0-9]+).([0-9]+).([0-9]+)(?:-([0-9A-Za-z-]+(?:-[0-9A-Za-z-]+)*))?$" | ||
|
||
func TestVersionCommand(t *testing.T) { | ||
rootCmd, _ := NewRootCommand() | ||
rootCmd.AddCommand(VersionCmd) | ||
rootCmd.SetArgs([]string{VersionCmd.Name()}) | ||
err := rootCmd.Execute() | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestVersionString(t *testing.T) { | ||
stableVersion := cfg.GetStableVersion() | ||
stableMatch, err := regexp.MatchString(RegExVersion, stableVersion) | ||
require.NoError(t, err) | ||
require.True(t, stableMatch) | ||
|
||
dirtyVersion := cfg.GetFullVersion() | ||
dirtyMatch, err := regexp.MatchString(RegExVersion, dirtyVersion) | ||
require.NoError(t, err) | ||
require.True(t, dirtyMatch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2024 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
"runtime/debug" | ||
) | ||
|
||
// Sets the numeric Gossamer version here | ||
const ( | ||
VersionMajor = 0 | ||
VersionMinor = 9 | ||
VersionPatch = 0 | ||
VersionMeta = "unstable" | ||
) | ||
|
||
// GitCommit attempts to get a Git commit hash; empty string otherwise | ||
var GitCommit = func() string { | ||
if info, ok := debug.ReadBuildInfo(); ok { | ||
for _, setting := range info.Settings { | ||
if setting.Key == "vcs.revision" { | ||
return setting.Value | ||
} | ||
} | ||
} | ||
return "" | ||
}() | ||
|
||
// Version holds a text representation of the Gossamer version | ||
var Version = func() string { | ||
if VersionMeta != "stable" { | ||
return GetFullVersion() | ||
} else { | ||
return GetStableVersion() | ||
} | ||
}() | ||
|
||
// GetFullVersion gets a verbose, long version string, e.g., 0.9.0-unstable-e41617ba | ||
func GetFullVersion() string { | ||
version := GetStableVersion() + "-" + VersionMeta | ||
if len(GitCommit) >= 8 { | ||
version += "-" + GitCommit[:8] | ||
} | ||
return version | ||
} | ||
|
||
// GetStableVersion gets a short, stable version string, e.g., 0.9.0 | ||
func GetStableVersion() string { | ||
return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch) | ||
} |