[#624] cli/root: Delete version command and add corresponding flag

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-20 19:11:49 +03:00 committed by Alex Vanin
parent e3b4216fa7
commit 87ce64fbbb
2 changed files with 19 additions and 48 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-node/misc"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -48,6 +49,7 @@ It contains commands for interaction with NeoFS nodes using different versions
of neofs-api and some useful utilities for compiling ACL rules from JSON of neofs-api and some useful utilities for compiling ACL rules from JSON
notation, managing container access through protocol gates, querying network map notation, managing container access through protocol gates, querying network map
and much more!`, and much more!`,
Run: entryPoint,
} }
var ( var (
@ -107,7 +109,23 @@ func init() {
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") rootCmd.Flags().Bool("version", false, "Application version and NeoFS API compatibility")
}
func entryPoint(cmd *cobra.Command, _ []string) {
printVersion, _ := cmd.Flags().GetBool("version")
if printVersion {
cmd.Printf(
"Version: %s \nBuild: %s \nDebug: %s\n",
misc.Version,
misc.Build,
misc.Debug,
)
return
}
_ = cmd.Usage()
} }
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.

View file

@ -1,47 +0,0 @@
package cmd
import (
"encoding/json"
"fmt"
"github.com/nspcc-dev/neofs-node/misc"
"github.com/spf13/cobra"
)
var (
// versionCmd represents the version command
versionCmd = &cobra.Command{
Use: "version",
Short: "Print version and exit",
Run: versionRun,
}
)
var flagJSON bool
type VersionInfo struct {
Version string `json:"Version,omitempty"`
Build string `json:"Build,omitempty"`
Debug string `json:"Debug,omitempty"`
}
func init() {
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().BoolVarP(&flagJSON, "json", "j", false, "Print version information in JSON")
}
func versionRun(cmd *cobra.Command, args []string) {
versionInfo := VersionInfo{Version: misc.Version, Build: misc.Build, Debug: misc.Debug}
if flagJSON {
bytes, _ := json.Marshal(versionInfo)
fmt.Printf("%s", string(bytes)+"\n")
return
}
fmt.Printf("Version: %s \nBuild: %s \nDebug: %s\n",
versionInfo.Version,
versionInfo.Build,
versionInfo.Debug)
}