Remove baseapp dependency on the version package (#4250)
The version package is meant to be a convenience utility that provides SDK consumers with a ready-to-use version command that produces app's versioning information from flags passed at compile time. It will not make sense anymore for the baseapp package to depend on the version package once gaia will have been migrated away from the SDK main repository as we neither want to make assumptions nor set expectations on downstream apps buildsystems. Thus BaseApp now provides SetAppVersion() and AppVersion() to to allow SDK consumers to set BaseApp's version information string once the struct is initialised.
This commit is contained in:
parent
114de631a5
commit
38f93128eb
|
@ -0,0 +1,3 @@
|
||||||
|
#4250 BaseApp.Query() returns app's version string set via BaseApp.SetAppVersion()
|
||||||
|
when handling /app/version queries instead of the version string passed as build
|
||||||
|
flag at compile time.
|
|
@ -0,0 +1 @@
|
||||||
|
#4250 New BaseApp.{,Set}AppVersion() methods to get/set app's version string.
|
3
Makefile
3
Makefile
|
@ -44,7 +44,8 @@ build_tags := $(strip $(build_tags))
|
||||||
|
|
||||||
# process linker flags
|
# process linker flags
|
||||||
|
|
||||||
ldflags = -X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
|
ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=gaia \
|
||||||
|
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
|
||||||
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
|
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
|
||||||
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)"
|
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)"
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
"github.com/cosmos/cosmos-sdk/store"
|
"github.com/cosmos/cosmos-sdk/store"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/version"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Key to store the consensus params in the main store.
|
// Key to store the consensus params in the main store.
|
||||||
|
@ -85,6 +84,9 @@ type BaseApp struct {
|
||||||
|
|
||||||
// height at which to halt the chain and gracefully shutdown
|
// height at which to halt the chain and gracefully shutdown
|
||||||
haltHeight uint64
|
haltHeight uint64
|
||||||
|
|
||||||
|
// application's version string
|
||||||
|
appVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ abci.Application = (*BaseApp)(nil)
|
var _ abci.Application = (*BaseApp)(nil)
|
||||||
|
@ -120,6 +122,11 @@ func (app *BaseApp) Name() string {
|
||||||
return app.name
|
return app.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppVersion returns the application's version string.
|
||||||
|
func (app *BaseApp) AppVersion() string {
|
||||||
|
return app.appVersion
|
||||||
|
}
|
||||||
|
|
||||||
// Logger returns the logger of the BaseApp.
|
// Logger returns the logger of the BaseApp.
|
||||||
func (app *BaseApp) Logger() log.Logger {
|
func (app *BaseApp) Logger() log.Logger {
|
||||||
return app.logger
|
return app.logger
|
||||||
|
@ -439,7 +446,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc
|
||||||
return abci.ResponseQuery{
|
return abci.ResponseQuery{
|
||||||
Code: uint32(sdk.CodeOK),
|
Code: uint32(sdk.CodeOK),
|
||||||
Codespace: string(sdk.CodespaceRoot),
|
Codespace: string(sdk.CodespaceRoot),
|
||||||
Value: []byte(version.Version),
|
Value: []byte(app.appVersion),
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -130,6 +130,26 @@ func TestLoadVersion(t *testing.T) {
|
||||||
testLoadVersionHelper(t, app, int64(2), commitID2)
|
testLoadVersionHelper(t, app, int64(2), commitID2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAppVersionSetterGetter(t *testing.T) {
|
||||||
|
logger := defaultLogger()
|
||||||
|
pruningOpt := SetPruning(store.PruneSyncable)
|
||||||
|
db := dbm.NewMemDB()
|
||||||
|
name := t.Name()
|
||||||
|
app := NewBaseApp(name, logger, db, nil, pruningOpt)
|
||||||
|
|
||||||
|
require.Equal(t, "", app.AppVersion())
|
||||||
|
res := app.Query(abci.RequestQuery{Path: "app/version"})
|
||||||
|
require.True(t, res.IsOK())
|
||||||
|
require.Equal(t, "", string(res.Value))
|
||||||
|
|
||||||
|
versionString := "1.0.0"
|
||||||
|
app.SetAppVersion(versionString)
|
||||||
|
require.Equal(t, versionString, app.AppVersion())
|
||||||
|
res = app.Query(abci.RequestQuery{Path: "app/version"})
|
||||||
|
require.True(t, res.IsOK())
|
||||||
|
require.Equal(t, versionString, string(res.Value))
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadVersionInvalid(t *testing.T) {
|
func TestLoadVersionInvalid(t *testing.T) {
|
||||||
logger := log.NewNopLogger()
|
logger := log.NewNopLogger()
|
||||||
pruningOpt := SetPruning(store.PruneSyncable)
|
pruningOpt := SetPruning(store.PruneSyncable)
|
||||||
|
@ -226,6 +246,9 @@ func TestBaseAppOptionSeal(t *testing.T) {
|
||||||
require.Panics(t, func() {
|
require.Panics(t, func() {
|
||||||
app.SetName("")
|
app.SetName("")
|
||||||
})
|
})
|
||||||
|
require.Panics(t, func() {
|
||||||
|
app.SetAppVersion("")
|
||||||
|
})
|
||||||
require.Panics(t, func() {
|
require.Panics(t, func() {
|
||||||
app.SetDB(nil)
|
app.SetDB(nil)
|
||||||
})
|
})
|
||||||
|
|
|
@ -40,6 +40,14 @@ func (app *BaseApp) SetName(name string) {
|
||||||
app.name = name
|
app.name = name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetAppVersion sets the application's version string.
|
||||||
|
func (app *BaseApp) SetAppVersion(v string) {
|
||||||
|
if app.sealed {
|
||||||
|
panic("SetAppVersion() on sealed BaseApp")
|
||||||
|
}
|
||||||
|
app.appVersion = v
|
||||||
|
}
|
||||||
|
|
||||||
func (app *BaseApp) SetDB(db dbm.DB) {
|
func (app *BaseApp) SetDB(db dbm.DB) {
|
||||||
if app.sealed {
|
if app.sealed {
|
||||||
panic("SetDB() on sealed BaseApp")
|
panic("SetDB() on sealed BaseApp")
|
||||||
|
|
|
@ -43,7 +43,8 @@ build_tags := $(strip $(build_tags))
|
||||||
|
|
||||||
# process linker flags
|
# process linker flags
|
||||||
|
|
||||||
ldflags = -X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
|
ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=gaia \
|
||||||
|
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
|
||||||
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
|
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
|
||||||
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)"
|
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)"
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/version"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||||
"github.com/cosmos/cosmos-sdk/x/crisis"
|
"github.com/cosmos/cosmos-sdk/x/crisis"
|
||||||
|
@ -78,6 +79,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
|
||||||
|
|
||||||
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
|
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
|
||||||
bApp.SetCommitMultiStoreTracer(traceStore)
|
bApp.SetCommitMultiStoreTracer(traceStore)
|
||||||
|
bApp.SetAppVersion(version.Version)
|
||||||
|
|
||||||
var app = &GaiaApp{
|
var app = &GaiaApp{
|
||||||
BaseApp: bApp,
|
BaseApp: bApp,
|
||||||
|
|
|
@ -16,7 +16,8 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
||||||
// VersionCmd prints out the current sdk version
|
// VersionCmd prints out the application's version
|
||||||
|
// information passed via build flags.
|
||||||
VersionCmd = &cobra.Command{
|
VersionCmd = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print the app version",
|
Short: "Print the app version",
|
||||||
|
@ -24,12 +25,12 @@ var (
|
||||||
verInfo := newVersionInfo()
|
verInfo := newVersionInfo()
|
||||||
|
|
||||||
if !viper.GetBool(flagLong) {
|
if !viper.GetBool(flagLong) {
|
||||||
fmt.Println(verInfo.CosmosSDK)
|
fmt.Println(verInfo.Version)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if viper.GetString(cli.OutputFlag) != "json" {
|
if viper.GetString(cli.OutputFlag) != "json" {
|
||||||
fmt.Print(verInfo)
|
fmt.Println(verInfo)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,19 @@
|
||||||
//nolint
|
// This package is a convenience utility that provides SDK
|
||||||
|
// consumers with a ready-to-use version command that
|
||||||
|
// produces apps versioning information based on flags
|
||||||
|
// passed at compile time.
|
||||||
|
//
|
||||||
|
// Configure the version command
|
||||||
|
//
|
||||||
|
// The version command can be just added to your cobra root command.
|
||||||
|
// At build time, the variables Name, Version, Commit, GoSumHash, and
|
||||||
|
// BuildTags can be passed as build flags as shown in the following
|
||||||
|
// example:
|
||||||
|
//
|
||||||
|
// go build -X github.com/cosmos/cosmos-sdk/version.Name=dapp \
|
||||||
|
// -X github.com/cosmos/cosmos-sdk/version.Version=1.0 \
|
||||||
|
// -X github.com/cosmos/cosmos-sdk/version.Commit=f0f7b7dab7e36c20b757cebce0e8f4fc5b95de60 \
|
||||||
|
// -X "github.com/cosmos/cosmos-sdk/version.BuildTags=linux darwin amd64"
|
||||||
package version
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -6,16 +21,22 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Variables set by build flags
|
|
||||||
var (
|
var (
|
||||||
Commit = ""
|
// Application's name
|
||||||
|
Name = ""
|
||||||
|
// Application's version string
|
||||||
Version = ""
|
Version = ""
|
||||||
|
// Commit
|
||||||
|
Commit = ""
|
||||||
|
// Hash of the go.sum file
|
||||||
GoSumHash = ""
|
GoSumHash = ""
|
||||||
|
// Build tags
|
||||||
BuildTags = ""
|
BuildTags = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
type versionInfo struct {
|
type versionInfo struct {
|
||||||
CosmosSDK string `json:"cosmos_sdk"`
|
Name string `json:"name"`
|
||||||
|
Version string `json:"version"`
|
||||||
GitCommit string `json:"commit"`
|
GitCommit string `json:"commit"`
|
||||||
GoSumHash string `json:"gosum_hash"`
|
GoSumHash string `json:"gosum_hash"`
|
||||||
BuildTags string `json:"build_tags"`
|
BuildTags string `json:"build_tags"`
|
||||||
|
@ -23,18 +44,19 @@ type versionInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v versionInfo) String() string {
|
func (v versionInfo) String() string {
|
||||||
return fmt.Sprintf(`cosmos-sdk: %s
|
return fmt.Sprintf(`%s: %s
|
||||||
git commit: %s
|
git commit: %s
|
||||||
go.sum hash: %s
|
go.sum hash: %s
|
||||||
build tags: %s
|
build tags: %s
|
||||||
%s`, v.CosmosSDK, v.GitCommit, v.GoSumHash, v.BuildTags, v.GoVersion)
|
%s`, v.Name, v.Version, v.GitCommit, v.GoSumHash, v.BuildTags, v.GoVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newVersionInfo() versionInfo {
|
func newVersionInfo() versionInfo {
|
||||||
return versionInfo{
|
return versionInfo{
|
||||||
|
Name,
|
||||||
Version,
|
Version,
|
||||||
Commit,
|
Commit,
|
||||||
GoSumHash,
|
GoSumHash,
|
||||||
BuildTags,
|
BuildTags,
|
||||||
fmt.Sprintf("go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)}
|
fmt.Sprintf("go version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue