|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/lightninglabs/lightning-terminal/litrpc" |
| 9 | + "github.com/urfave/cli" |
| 10 | +) |
| 11 | + |
| 12 | +var listActionsCommand = cli.Command{ |
| 13 | + Name: "actions", |
| 14 | + Usage: "List actions performed on the Litd server", |
| 15 | + Action: listActions, |
| 16 | + Flags: []cli.Flag{ |
| 17 | + cli.StringFlag{ |
| 18 | + Name: "feature", |
| 19 | + Usage: "The name of the feature to " + |
| 20 | + "filter the actions by. If " + |
| 21 | + "left empty, then all " + |
| 22 | + "actions will be returned.", |
| 23 | + }, |
| 24 | + cli.StringFlag{ |
| 25 | + Name: "actor", |
| 26 | + Usage: "The actor name to filter the actions by. If " + |
| 27 | + "left empty, then all actions will be " + |
| 28 | + "returned.", |
| 29 | + }, |
| 30 | + cli.StringFlag{ |
| 31 | + Name: "method", |
| 32 | + Usage: "The method name to filter the actions by. If " + |
| 33 | + "left empty, then all actions will be " + |
| 34 | + "returned.", |
| 35 | + }, |
| 36 | + cli.StringFlag{ |
| 37 | + Name: "session_id", |
| 38 | + Usage: "The session ID to filter the actions by. If " + |
| 39 | + "left empty, then all actions will be " + |
| 40 | + "returned.", |
| 41 | + }, |
| 42 | + cli.Uint64Flag{ |
| 43 | + Name: "start_timestamp", |
| 44 | + Usage: "Only actions executed after this unix " + |
| 45 | + "timestamp will be considered.", |
| 46 | + }, |
| 47 | + cli.Uint64Flag{ |
| 48 | + Name: "end_timestamp", |
| 49 | + Usage: "Only actions executed before this unix " + |
| 50 | + "timestamp will be considered.", |
| 51 | + }, |
| 52 | + cli.StringFlag{ |
| 53 | + Name: "state", |
| 54 | + Usage: "The action state to filter on. If not set, " + |
| 55 | + "then actions of any state will be returned. " + |
| 56 | + "Options include: 'pending', 'done' and 'error", |
| 57 | + }, |
| 58 | + cli.Uint64Flag{ |
| 59 | + Name: "index_offset", |
| 60 | + Usage: "The index of an action that will be used as " + |
| 61 | + "either the start a query to determine which " + |
| 62 | + "actions should be returned in the response", |
| 63 | + }, |
| 64 | + cli.Uint64Flag{ |
| 65 | + Name: "max_num_actions", |
| 66 | + Usage: "The max number of actions to return", |
| 67 | + }, |
| 68 | + cli.BoolFlag{ |
| 69 | + Name: "oldest_first", |
| 70 | + Usage: "Tf set, actions succeeding the index_offset " + |
| 71 | + "will be returned", |
| 72 | + }, |
| 73 | + cli.BoolFlag{ |
| 74 | + Name: "count_total", |
| 75 | + Usage: "Set to true if the total number of all " + |
| 76 | + "actions that match the given filters should " + |
| 77 | + "be counted and returned in the request. " + |
| 78 | + "Note that setting this will significantly " + |
| 79 | + "decrease the performance of the query if " + |
| 80 | + "there are many actions in the db. Also note " + |
| 81 | + "that if this option is set, the " + |
| 82 | + "index_offset is the index in this set of " + |
| 83 | + "actions where as if it is not set, then " + |
| 84 | + "index_offset is the index of the action in " + |
| 85 | + "the db regardless of filter.", |
| 86 | + }, |
| 87 | + }, |
| 88 | +} |
| 89 | + |
| 90 | +func listActions(ctx *cli.Context) error { |
| 91 | + ctxb := context.Background() |
| 92 | + clientConn, cleanup, err := connectClient(ctx) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + defer cleanup() |
| 97 | + client := litrpc.NewFirewallClient(clientConn) |
| 98 | + |
| 99 | + state, err := parseActionState(ctx.String("state")) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + var sessionID []byte |
| 105 | + if ctx.String("session_id") != "" { |
| 106 | + sessionID, err = hex.DecodeString(ctx.String("session_id")) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + resp, err := client.ListActions( |
| 113 | + ctxb, &litrpc.ListActionsRequest{ |
| 114 | + SessionId: sessionID, |
| 115 | + FeatureName: ctx.String("feature"), |
| 116 | + ActorName: ctx.String("actor"), |
| 117 | + MethodName: ctx.String("method"), |
| 118 | + State: state, |
| 119 | + IndexOffset: ctx.Uint64("index_offset"), |
| 120 | + MaxNumActions: ctx.Uint64("max_num_actions"), |
| 121 | + Reversed: !ctx.Bool("oldest_first"), |
| 122 | + CountTotal: ctx.Bool("count_total"), |
| 123 | + StartTimestamp: ctx.Uint64("start_timestamp"), |
| 124 | + EndTimestamp: ctx.Uint64("end_timestamp"), |
| 125 | + }, |
| 126 | + ) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + printRespJSON(resp) |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func parseActionState(actionStr string) (litrpc.ActionState, error) { |
| 137 | + switch actionStr { |
| 138 | + case "": |
| 139 | + return litrpc.ActionState_STATE_UNKNOWN, nil |
| 140 | + case "pending": |
| 141 | + return litrpc.ActionState_STATE_PENDING, nil |
| 142 | + case "done": |
| 143 | + return litrpc.ActionState_STATE_DONE, nil |
| 144 | + case "error": |
| 145 | + return litrpc.ActionState_STATE_ERROR, nil |
| 146 | + default: |
| 147 | + return 0, fmt.Errorf("unknown action state %s. Valid options "+ |
| 148 | + "include 'pending', 'done' and 'error'", actionStr) |
| 149 | + } |
| 150 | +} |
0 commit comments