Skip to content

Commit

Permalink
Merge pull request #35 from aquarius-kuchain/features/get-version
Browse files Browse the repository at this point in the history
Add version cmd to get build and version info for Kuchain
  • Loading branch information
aquarius-kuchain authored Aug 19, 2020
2 parents 1e4d433 + d3e6b33 commit 287b3b2
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 22 deletions.
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/usr/bin/make -f

PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation')
#VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
VERSION := '0.0.1'
VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
COMMIT := $(shell git log -1 --format='%H')
LEDGER_ENABLED ?= true
SDK_PACK := $(shell go list -m github.com/cosmos/cosmos-sdk | sed 's/ /\@/g')
TIME_BEGIN := $(shell date -u +"%Y-%m-%d %H:%M.%S")
BRANCH := $(shell echo $(shell git rev-parse --abbrev-ref HEAD) | sed 's/^v//')

MAIN_SYMBOL := 'kuchain'
CORE_SYMBOL := 'sys'
$(info "Kuchain Version: ${VERSION} ${SDK_PACK} in ${TIME_BEGIN}")
$(info "Current branch: ${BRANCH}")

MAIN_SYMBOL := kuchain
CORE_SYMBOL := sys

export GO111MODULE = on

Expand Down Expand Up @@ -57,6 +61,10 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=$(MAIN_SYMBOL) \
-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.BuildTags=$(build_tags_comma_sep)" \
-X github.com/KuChainNetwork/kuchain/chain/constants.KuchainBuildVersion=$(VERSION) \
-X github.com/KuChainNetwork/kuchain/chain/constants.KuchainBuildBranch=$(BRANCH) \
-X "github.com/KuChainNetwork/kuchain/chain/constants.KuchainBuildTime=$(TIME_BEGIN)" \
-X github.com/KuChainNetwork/kuchain/chain/constants.KuchainBuildSDKVersion=$(SDK_PACK) \
-X github.com/KuChainNetwork/kuchain/chain/constants/keys.ChainNameStr=$(CORE_SYMBOL) \
-X github.com/KuChainNetwork/kuchain/chain/constants/keys.ChainMainNameStr=$(MAIN_SYMBOL)

Expand All @@ -68,7 +76,6 @@ ldflags := $(strip $(ldflags))

BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' -trimpath


all: clear-build build

build: go.sum
Expand Down
44 changes: 27 additions & 17 deletions chain/constants/versions.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
package constants

import (
"strconv"

"github.com/KuChainNetwork/kuchain/chain/types"
"github.com/tendermint/tendermint/libs/log"
"gopkg.in/yaml.v2"
)

// some fix height

var (
// FixAssetHeight fix asset bugs height
FixAssetHeight string = ""
FixAssetHeightVal, _ = strconv.ParseInt(FixAssetHeight, 10, 64)
KuchainBuildVersion = ""
KuchainBuildBranch = ""
KuchainBuildTime = ""
KuchainBuildSDKVersion = ""
)

// LogVersion log version info
func LogVersion(logger log.Logger) {
logger.Info("FixAsset", "height", GetFixAssetHeight())
}
// VersionInfo get Version Info
func VersionInfo() []byte {
ver := struct {
Version string `json:"version" yaml:"version"`
Branch string `json:"branch" yaml:"branch"`
BuildTime string `json:"build_time" yaml:"build_time"`
SDKVersion string `json:"sdk_version" yaml:"sdk_version"`
}{
Version: KuchainBuildVersion,
Branch: KuchainBuildBranch,
BuildTime: KuchainBuildTime,
SDKVersion: KuchainBuildSDKVersion,
}

func GetFixAssetHeight() int64 {
return FixAssetHeightVal
res, _ := yaml.Marshal(ver)
return res
}

// IsFixAssetHeight is fix asset
func IsFixAssetHeight(ctx types.Context) bool {
return ctx.BlockHeight() > FixAssetHeightVal
// LogVersion log version info
func LogVersion(logger log.Logger) {
logger.Info("Kuchain Version",
"version", KuchainBuildVersion,
"branch", KuchainBuildBranch,
"time", KuchainBuildTime,
"sdkVersion", KuchainBuildSDKVersion)
}
77 changes: 77 additions & 0 deletions cmd/kucd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package main

import (
"encoding/json"
"fmt"

"github.com/KuChainNetwork/kuchain/chain/constants"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tversion "github.com/tendermint/tendermint/version"
"gopkg.in/yaml.v2"
)

// add server commands
Expand Down Expand Up @@ -38,3 +45,73 @@ func AddCommands(
version.Cmd,
)
}

const (
flagVersionSimple = "simple"
flagJSONFormat = "json"
)

func versionCmd(ctx *server.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print kuchain version",
Long: `Print kuchain version numbers
against which this app has been compiled.
`,
RunE: func(cmd *cobra.Command, args []string) error {
var (
bs []byte
err error
)

if !viper.GetBool(flagVersionSimple) {
vs := struct {
Name string `json:"name" yaml:"name"`
Tendermint string `json:"tendermint_version" yaml:"tendermint_version"`
ABCI string `json:"abci" yaml:"abci"`
BlockProtocol uint64 `json:"block_protocol" yaml:"block_protocol"`
P2PProtocol uint64 `json:"p2p_protocol" yaml:"p2p_protocol"`
KuchainBuildVersion string `json:"version" yaml:"version"`
KuchainBuildBranch string `json:"branch" yaml:"branch"`
KuchainBuildTime string `json:"build_time" yaml:"build_time"`
SDKVersion string `json:"sdk_version" yaml:"sdk_version"`
Commit string `json:"commit" yaml:"commit"`
BuildTags string `json:"build_tags" yaml:"build_tags"`
}{
Name: version.Name,
Tendermint: tversion.Version,
ABCI: tversion.ABCIVersion,
BlockProtocol: tversion.BlockProtocol.Uint64(),
P2PProtocol: tversion.P2PProtocol.Uint64(),
KuchainBuildVersion: constants.KuchainBuildVersion,
KuchainBuildBranch: constants.KuchainBuildBranch,
KuchainBuildTime: constants.KuchainBuildTime,
SDKVersion: constants.KuchainBuildSDKVersion,
Commit: version.Commit,
BuildTags: version.BuildTags,
}

if viper.GetBool(flagJSONFormat) {
bs, err = json.Marshal(&vs)
} else {
bs, err = yaml.Marshal(&vs)
}

} else {
bs = []byte(constants.KuchainBuildVersion)
}

if err != nil {
return err
}

fmt.Println(string(bs))
return nil
},
}

cmd.Flags().BoolP(flagVersionSimple, "s", false, "if just print simple version of kucd")
cmd.Flags().BoolP(flagJSONFormat, "j", false, "print version info by json")

return cmd
}
2 changes: 2 additions & 0 deletions cmd/kucd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func main() {
rootCmd.AddCommand(replayCmd())
rootCmd.AddCommand(debug.Cmd(cdc))

rootCmd.AddCommand(versionCmd(ctx))

AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)

// prepare and add flags
Expand Down

0 comments on commit 287b3b2

Please sign in to comment.