feat(cli): add version command

Co-authored-by: Gaze <dev@gaze.network>
This commit is contained in:
Gaze
2024-04-16 00:27:31 +07:00
parent d0dfff9dc0
commit e0e7511864
5 changed files with 54 additions and 10 deletions

View File

@@ -51,6 +51,7 @@ func Execute() {
// Execute command
if err := cmd.Execute(); err != nil {
logger.Panic("Failed to execute root command", slogx.Error(err))
// use cobra to log error message by default
logger.Debug("Failed to execute root command", slogx.Error(err))
}
}

View File

@@ -3,19 +3,47 @@ package cmd
import (
"fmt"
"github.com/cockroachdb/errors"
"github.com/gaze-network/indexer-network/common/errs"
"github.com/gaze-network/indexer-network/core/constants"
"github.com/gaze-network/indexer-network/modules/bitcoin"
"github.com/gaze-network/indexer-network/modules/runes"
"github.com/spf13/cobra"
)
func NewVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show indexer-network version",
RunE: versionHandler,
}
var versions = map[string]string{
"": constants.Version,
"bitcoin": bitcoin.Version,
"runes": runes.Version,
}
func versionHandler(cmd *cobra.Command, args []string) error {
// TODO: create constant package
fmt.Println("gaze-network v0.0.1")
type versionCmdOptions struct {
Modules string
}
func NewVersionCommand() *cobra.Command {
opts := &versionCmdOptions{}
cmd := &cobra.Command{
Use: "version",
Short: "Show indexer-network version",
RunE: func(cmd *cobra.Command, args []string) error {
return versionHandler(opts, cmd, args)
},
}
flags := cmd.Flags()
flags.StringVar(&opts.Modules, "module", "", `Show version of a specific module. E.g. "bitcoin" | "runes"`)
return cmd
}
func versionHandler(opts *versionCmdOptions, _ *cobra.Command, _ []string) error {
version, ok := versions[opts.Modules]
if !ok {
// fmt.Fprintln(cmd.ErrOrStderr(), "Unknown module")
return errors.Wrap(errs.Unsupported, "Invalid module name")
}
fmt.Println(version)
return nil
}

View File

@@ -0,0 +1,5 @@
package constants
const (
Version = "v0.0.1"
)

View File

@@ -0,0 +1,5 @@
package bitcoin
const (
Version = "v0.0.1"
)

View File

@@ -0,0 +1,5 @@
package runes
const (
Version = "v0.0.1"
)