Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): dynamically set version based on environment #3693

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
zombienet test -p native zombienet_tests/functional/0001-basic-network.zndsl
6 changes: 2 additions & 4 deletions cmd/gossamer/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ package commands
import (
"fmt"

cfg "github.com/ChainSafe/gossamer/config"

"github.com/spf13/cobra"
)

// VersionCmd returns the gossamer version
// VersionCmd returns the Gossamer version
var VersionCmd = &cobra.Command{
Use: "version",
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
},
}
35 changes: 35 additions & 0 deletions cmd/gossamer/commands/version_test.go
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)
}
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -402,7 +402,7 @@ func DefaultConfig() *Config {
},
System: &SystemConfig{
SystemName: DefaultSystemName,
SystemVersion: DefaultSystemVersion,
SystemVersion: GetFullVersion(),
},
}
}
Expand Down Expand Up @@ -483,7 +483,7 @@ func DefaultConfigFromSpec(nodeSpec *genesis.Genesis) *Config {
},
System: &SystemConfig{
SystemName: DefaultSystemName,
SystemVersion: DefaultSystemVersion,
SystemVersion: GetFullVersion(),
},
}
}
Expand Down
52 changes: 52 additions & 0 deletions config/version.go
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)
}
Loading