-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcmd.go
118 lines (96 loc) · 2.99 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package launchtools
import (
"bufio"
"encoding/json"
"fmt"
"path"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
flagArtifactsDir = "artifacts-dir"
flagWithConfig = "with-config"
)
func LaunchCmd(
appCreator AppCreator,
defaultGenesisGetter func(denom string) map[string]json.RawMessage,
steps []LauncherStepFuncFactory[*Config],
) *cobra.Command {
cmd := &cobra.Command{
Use: "launch",
Short: "Launch a new instance of the app",
Long: `Launch a new instance of the app. This command will execute a series of steps to
initialize the app and generate the necessary configuration files. The artifacts will be stored in the
specified directory. The command will output the artifacts to stdout too.
Example:
$ launchtools launch --artifacts-dir ./ --with-config ./config.json
`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
sdk.SetAddrCacheEnabled(false)
defer sdk.SetAddrCacheEnabled(true)
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
artifactsDir, err := cmd.Flags().GetString(flagArtifactsDir)
if err != nil {
return errors.Wrap(err, "failed to get artifacts flag")
}
configPath, err := cmd.Flags().GetString(flagWithConfig)
if err != nil {
return errors.Wrap(err, "failed to get config flag")
}
config, err := NewConfig(configPath)
if err != nil {
return err
}
if err := config.Finalize(bufio.NewReader(clientCtx.Input)); err != nil {
return errors.Wrap(err, "failed to finalize config")
}
launcher := NewLauncher(
cmd,
&clientCtx,
serverCtx,
appCreator,
defaultGenesisGetter(config.L2Config.Denom),
artifactsDir,
)
stepFns := make([]LauncherStepFunc, len(steps))
for stepI, step := range steps {
stepFns[stepI] = step(config)
}
for _, stepFn := range stepFns {
if err := stepFn(launcher); err != nil {
return errors.Wrapf(err, "failed to run launcher step")
}
}
// print out the artifacts to stdout
artifacts, err := launcher.FinalizeOutput(config)
if err != nil {
return errors.Wrap(err, "failed to get output")
}
if _, err := fmt.Fprintf(cmd.OutOrStdout(), `
############################################
Artifact written to
* %s
* %s
`,
path.Join(clientCtx.HomeDir, artifactsDir, "config.json"),
path.Join(clientCtx.HomeDir, artifactsDir, "artifacts.json"),
); err != nil {
return errors.Wrap(err, "failed to write artifacts to stdout")
}
if _, err := fmt.Fprintf(cmd.ErrOrStderr(), `%s`,
artifacts,
); err != nil {
return errors.Wrap(err, "failed to write artifacts to stdout")
}
return nil
},
}
cmd.Flags().String(flagArtifactsDir, "artifacts", "Path to the directory where artifacts will be stored")
cmd.Flags().String(flagWithConfig, "", "Path to the config file to use for the launch")
return cmd
}