From 184585d7b18fd13dee9b40378084ad14674863e1 Mon Sep 17 00:00:00 2001 From: Afr Schoe <58883403+q9f@users.noreply.github.com> Date: Mon, 15 Jan 2024 14:05:30 +0100 Subject: [PATCH 1/4] feat(config): dynamically set version based on environment --- Makefile | 2 +- cmd/gossamer/commands/version.go | 4 +-- config/config.go | 6 ++-- config/version.go | 52 ++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 config/version.go diff --git a/Makefile b/Makefile index ee013bdaef..02bf0e06a9 100644 --- a/Makefile +++ b/Makefile @@ -146,4 +146,4 @@ endif chmod a+x $(GOPATH)/bin/zombienet zombienet-test: install install-zombienet - zombienet test -p native zombienet_tests/functional/0001-basic-network.zndsl \ No newline at end of file + zombienet test -p native zombienet_tests/functional/0001-basic-network.zndsl diff --git a/cmd/gossamer/commands/version.go b/cmd/gossamer/commands/version.go index 1bf1fa8a92..31dc60a31a 100644 --- a/cmd/gossamer/commands/version.go +++ b/cmd/gossamer/commands/version.go @@ -6,8 +6,6 @@ package commands import ( "fmt" - cfg "github.com/ChainSafe/gossamer/config" - "github.com/spf13/cobra" ) @@ -17,7 +15,7 @@ var VersionCmd = &cobra.Command{ Short: "gossamer version", Long: `gossamer version`, RunE: func(cmd *cobra.Command, args []string) error { - fmt.Printf("%s version %s\n", cfg.DefaultSystemName, cfg.DefaultSystemVersion) + fmt.Printf("%s version %s\n", config.System.SystemName, config.System.SystemVersion) return nil }, } diff --git a/config/config.go b/config/config.go index 8354411b5d..dd8f919f99 100644 --- a/config/config.go +++ b/config/config.go @@ -61,7 +61,7 @@ const ( // DefaultSystemName is the default system name DefaultSystemName = "Gossamer" // DefaultSystemVersion is the default system version - DefaultSystemVersion = "0.9.0" + DefaultSystemVersion = "0.0.0" ) // DefaultRPCModules the default RPC modules @@ -402,7 +402,7 @@ func DefaultConfig() *Config { }, System: &SystemConfig{ SystemName: DefaultSystemName, - SystemVersion: DefaultSystemVersion, + SystemVersion: getFullVersion(), }, } } @@ -483,7 +483,7 @@ func DefaultConfigFromSpec(nodeSpec *genesis.Genesis) *Config { }, System: &SystemConfig{ SystemName: DefaultSystemName, - SystemVersion: DefaultSystemVersion, + SystemVersion: getFullVersion(), }, } } diff --git a/config/version.go b/config/version.go new file mode 100644 index 0000000000..eb8caeb77d --- /dev/null +++ b/config/version.go @@ -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" +) + +// Attempts to get a Git commit hash +var GitCommit = func() string { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + } + return "" +}() + +// Holds a text representation of the Gossamer version +var Version = func() string { + if VersionMeta != "stable" { + return getFullVersion() + } else { + return getStableVersion() + } +}() + +// 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 +} + +// Gets a short, stable version string, e.g., 0.9.0 +func getStableVersion() string { + return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch) +} From 1ab7a6510970acad034b5b93ca053476ec4ea937 Mon Sep 17 00:00:00 2001 From: afr q9f <58883403+q9f@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:05:29 +0100 Subject: [PATCH 2/4] cmd/gossamer: add tests to version command --- cmd/gossamer/commands/version_test.go | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 cmd/gossamer/commands/version_test.go diff --git a/cmd/gossamer/commands/version_test.go b/cmd/gossamer/commands/version_test.go new file mode 100644 index 0000000000..2ead6244af --- /dev/null +++ b/cmd/gossamer/commands/version_test.go @@ -0,0 +1,35 @@ +// Copyright 2024 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + +package commands + +import ( + "testing" + "regexp" + + 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, err := 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) +} From 3b147c5c092d3f5c5182653db2c4403bb0a6321d Mon Sep 17 00:00:00 2001 From: afr q9f <58883403+q9f@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:05:59 +0100 Subject: [PATCH 3/4] config/version: make linter happy --- cmd/gossamer/commands/version.go | 2 +- config/config.go | 4 ++-- config/version.go | 18 +++++++++--------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/gossamer/commands/version.go b/cmd/gossamer/commands/version.go index 31dc60a31a..3848bb3c50 100644 --- a/cmd/gossamer/commands/version.go +++ b/cmd/gossamer/commands/version.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -// VersionCmd returns the gossamer version +// VersionCmd returns the Gossamer version var VersionCmd = &cobra.Command{ Use: "version", Short: "gossamer version", diff --git a/config/config.go b/config/config.go index dd8f919f99..984ba00ed1 100644 --- a/config/config.go +++ b/config/config.go @@ -402,7 +402,7 @@ func DefaultConfig() *Config { }, System: &SystemConfig{ SystemName: DefaultSystemName, - SystemVersion: getFullVersion(), + SystemVersion: GetFullVersion(), }, } } @@ -483,7 +483,7 @@ func DefaultConfigFromSpec(nodeSpec *genesis.Genesis) *Config { }, System: &SystemConfig{ SystemName: DefaultSystemName, - SystemVersion: getFullVersion(), + SystemVersion: GetFullVersion(), }, } } diff --git a/config/version.go b/config/version.go index eb8caeb77d..aab0293881 100644 --- a/config/version.go +++ b/config/version.go @@ -16,7 +16,7 @@ const ( VersionMeta = "unstable" ) -// Attempts to get a Git commit hash +// 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 { @@ -28,25 +28,25 @@ var GitCommit = func() string { return "" }() -// Holds a text representation of the Gossamer version +// Version holds a text representation of the Gossamer version var Version = func() string { if VersionMeta != "stable" { - return getFullVersion() + return GetFullVersion() } else { - return getStableVersion() + return GetStableVersion() } }() -// Gets a verbose, long version string, e.g., 0.9.0-unstable-e41617ba -func getFullVersion() string { - version := getStableVersion() + "-" + VersionMeta +// 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 } -// Gets a short, stable version string, e.g., 0.9.0 -func getStableVersion() string { +// GetStableVersion gets a short, stable version string, e.g., 0.9.0 +func GetStableVersion() string { return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch) } From 8127e372a8d68a22c5c052de6919e0a99e68e17f Mon Sep 17 00:00:00 2001 From: q9f Date: Tue, 16 Jan 2024 16:23:11 +0100 Subject: [PATCH 4/4] make lint --- cmd/gossamer/commands/version_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/gossamer/commands/version_test.go b/cmd/gossamer/commands/version_test.go index 2ead6244af..729b50189c 100644 --- a/cmd/gossamer/commands/version_test.go +++ b/cmd/gossamer/commands/version_test.go @@ -4,8 +4,8 @@ package commands import ( - "testing" "regexp" + "testing" cfg "github.com/ChainSafe/gossamer/config" @@ -15,10 +15,10 @@ import ( const RegExVersion = "^([0-9]+).([0-9]+).([0-9]+)(?:-([0-9A-Za-z-]+(?:-[0-9A-Za-z-]+)*))?$" func TestVersionCommand(t *testing.T) { - rootCmd, err := NewRootCommand() + rootCmd, _ := NewRootCommand() rootCmd.AddCommand(VersionCmd) rootCmd.SetArgs([]string{VersionCmd.Name()}) - err = rootCmd.Execute() + err := rootCmd.Execute() require.NoError(t, err) }