Skip to content

Commit

Permalink
feat(config): dynamically set version based on environment (#3693)
Browse files Browse the repository at this point in the history
Co-authored-by: q9f <q9f@users.noreply.github.com>
  • Loading branch information
q9f and q9f committed Jan 16, 2024
1 parent a93feeb commit 5c534c9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
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)
}

0 comments on commit 5c534c9

Please sign in to comment.