-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassthrough.go
74 lines (62 loc) · 2.3 KB
/
passthrough.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
package nova
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/01builders/nova/abci"
)
// NewPassthroughCmd creates a command that allows executing commands on any app version.
// This enables direct interaction with older app versions for debugging or older queries.
func NewPassthroughCmd(versions abci.Versions) (*cobra.Command, error) {
if err := versions.Validate(); err != nil {
return nil, fmt.Errorf("invalid versions: %w", err)
}
cmd := &cobra.Command{
Use: "passthrough [version/height] [command]",
Args: cobra.MinimumNArgs(1),
DisableFlagParsing: true,
Short: "Execute a command on a specific app version",
Long: `Execute a command on a specific app version.
This allows interacting with older app versions for debugging or older queries.
Use a version name to specify a named version or a height to execute on the version active at a specific height as first argument.`,
Example: `passthrough v3 status`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) >= 2 && strings.EqualFold("start", args[1]) {
return errors.New("cannot passthrough start command")
}
// Get the version name or determine from height
versionName := args[0]
height, errParseHeight := strconv.ParseInt(versionName, 10, 64)
var (
appVersion abci.Version
err error
)
// Determine which version to use based on flags
if versionName != "" && height == 0 {
// Get by name
appVersion, err = versions.GetForName(versionName)
if err != nil {
return fmt.Errorf("version %s not found: %w", versionName, err)
}
} else if errParseHeight == nil { // Get by height
if versions.ShouldLatestApp(height) {
return fmt.Errorf("height %d requires the latest app, use the command directly without passthrough", height)
}
appVersion, err = versions.GetForHeight(height)
if err != nil {
return fmt.Errorf("no version found for height %d: %w", height, err)
}
}
// ensure we have a valid appd instance
if appVersion.Appd == nil {
return fmt.Errorf("no binary available for version %s", versionName)
}
// prepare the command to be executed
execCmd := appVersion.Appd.CreateExecCommand(args[1:]...)
return execCmd.Run()
},
}
return cmd, nil
}