-
Notifications
You must be signed in to change notification settings - Fork 4
/
start.go
77 lines (63 loc) · 1.77 KB
/
start.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
66
67
68
69
70
71
72
73
74
75
76
77
package cmd
import (
"fmt"
"os"
"github.com/desmos-labs/desmos/v4/app"
"github.com/spf13/cobra"
"github.com/desmos-labs/hephaestus/network"
"github.com/desmos-labs/hephaestus/bot"
"github.com/desmos-labs/hephaestus/types"
)
// StartCmd returns a Cobra command allowing to start the bot
func StartCmd() *cobra.Command {
return &cobra.Command{
Use: "start [[config-file]]",
Short: "Starts the bot using the provided configuration file",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
// Read the configuration
cfgPath, err := getConfigPath(args)
if err != nil {
return err
}
cfg, err := types.Parse(cfgPath)
if err != nil {
return err
}
encodingConfig := app.MakeTestEncodingConfig()
// Build the networks clients
var mainnet, testnet *network.Client
if cfg.Networks.Testnet != nil {
testnet, err = network.NewClient(cfg.Networks.Testnet, encodingConfig)
if err != nil {
return err
}
}
if cfg.Networks.Mainnet != nil {
mainnet, err = network.NewClient(cfg.Networks.Mainnet, encodingConfig)
if err != nil {
return err
}
}
// Create the bot
hephaestus, err := bot.Create(cfg.BotConfig, testnet, mainnet)
if err != nil {
return err
}
// Start the bot
hephaestus.Start()
return nil
},
}
}
// getConfigPath gets the configuration file path reading it from the
// environmental variables, or from the arguments list provided
func getConfigPath(args []string) (string, error) {
if value, ok := os.LookupEnv(types.EnvVariableConfigPath); ok {
return value, nil
}
if len(args) < 1 {
return "", fmt.Errorf("no config path found: use either the %s env variable, or the first argument", types.EnvVariableConfigPath)
}
return args[0], nil
}