/
cmd.go
65 lines (52 loc) · 1.94 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cmd
import (
"fmt"
"os"
"path"
initcmd "github.com/forbole/ibcjuno/cmd/init"
startcmd "github.com/forbole/ibcjuno/cmd/start"
utils "github.com/forbole/ibcjuno/utils"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/cli"
)
var (
HomeFlag = "home"
)
// BuildDefaultCmd allows to build cli Executor containing a root command that
// has name, description, version, init and start commands implementations
func BuildDefaultCmd(config *Config) cli.Executor {
rootCmd := RootCmd(config.GetName())
rootCmd.AddCommand(
getVersionCmd(),
initcmd.NewInitCmd(config.GetInitConfig()),
startcmd.NewStartCmd(config.GetStartConfig()),
)
return PrepareRootCmd(config.GetName(), rootCmd)
}
// RootCmd allows to build the default root command with the given name
func RootCmd(name string) *cobra.Command {
return &cobra.Command{
Use: name,
Short: fmt.Sprintf("%s is a IBC price aggregator and exporter", name),
Long: fmt.Sprintf(`%s is an IBC price aggregator and exporter. It queries the latest IBC tokens prices
and stores them inside PostgreSQL database. %s is meant to run with a GraphQL layer on top
to ease the ability for developers and downstream clients to query the latest price of any IBC token.`, name, name),
}
}
// PrepareRootCmd prepares the given command binding all the viper flags
func PrepareRootCmd(name string, cmd *cobra.Command) cli.Executor {
cmd.PersistentPreRunE = utils.ConcatCobraCmdFuncs(
utils.BindFlagsLoadViper,
setupHome,
cmd.PersistentPreRunE,
)
home, _ := os.UserHomeDir()
defaultConfigPath := path.Join(home, fmt.Sprintf(".%s", name))
cmd.PersistentFlags().String(HomeFlag, defaultConfigPath, "Set the home folder of the application, where all files will be stored")
return cli.Executor{Command: cmd, Exit: os.Exit}
}
// setupHome sets up home directory of the root command
func setupHome(cmd *cobra.Command, _ []string) error {
utils.HomePath, _ = cmd.Flags().GetString(HomeFlag)
return nil
}