master
go 116 lines 2.69 KB
Raw
1 package commands
2
3 import (
4 "fmt"
5 "io"
6 "slices"
7 "text/tabwriter"
8 "time"
9
10 oldcmds "github.com/ipfs/kubo/commands"
11
12 cmds "github.com/ipfs/go-ipfs-cmds"
13 )
14
15 const (
16 verboseOptionName = "verbose"
17 )
18
19 var ActiveReqsCmd = &cmds.Command{
20 Helptext: cmds.HelpText{
21 Tagline: "List commands run on this IPFS node.",
22 ShortDescription: `
23 Lists running and recently run commands.
24 `,
25 },
26 NoLocal: true,
27 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
28 ctx := env.(*oldcmds.Context)
29 return cmds.EmitOnce(res, ctx.ReqLog.Report())
30 },
31 Options: []cmds.Option{
32 cmds.BoolOption(verboseOptionName, "v", "Print extra information."),
33 },
34 Subcommands: map[string]*cmds.Command{
35 "clear": clearInactiveCmd,
36 "set-time": setRequestClearCmd,
37 },
38 Encoders: cmds.EncoderMap{
39 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
40 verbose, _ := req.Options[verboseOptionName].(bool)
41
42 tw := tabwriter.NewWriter(w, 4, 4, 2, ' ', 0)
43 if verbose {
44 fmt.Fprint(tw, "ID\t")
45 }
46 fmt.Fprint(tw, "Command\t")
47 if verbose {
48 fmt.Fprint(tw, "Arguments\tOptions\t")
49 }
50 fmt.Fprintln(tw, "Active\tStartTime\tRunTime")
51
52 for _, req := range *out {
53 if verbose {
54 fmt.Fprintf(tw, "%d\t", req.ID)
55 }
56 fmt.Fprintf(tw, "%s\t", req.Command)
57 if verbose {
58 fmt.Fprintf(tw, "%v\t[", req.Args)
59 var keys []string
60 for k := range req.Options {
61 keys = append(keys, k)
62 }
63 slices.Sort(keys)
64
65 for _, k := range keys {
66 fmt.Fprintf(tw, "%s=%v,", k, req.Options[k])
67 }
68 fmt.Fprintf(tw, "]\t")
69 }
70
71 var live time.Duration
72 if req.Active {
73 live = time.Since(req.StartTime)
74 } else {
75 live = req.EndTime.Sub(req.StartTime)
76 }
77 t := req.StartTime.Format(time.Stamp)
78 fmt.Fprintf(tw, "%t\t%s\t%s\n", req.Active, t, live)
79 }
80 tw.Flush()
81
82 return nil
83 }),
84 },
85 Type: []*cmds.ReqLogEntry{},
86 }
87
88 var clearInactiveCmd = &cmds.Command{
89 Helptext: cmds.HelpText{
90 Tagline: "Clear inactive requests from the log.",
91 },
92 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
93 ctx := env.(*oldcmds.Context)
94 ctx.ReqLog.ClearInactive()
95 return nil
96 },
97 }
98
99 var setRequestClearCmd = &cmds.Command{
100 Helptext: cmds.HelpText{
101 Tagline: "Set how long to keep inactive requests in the log.",
102 },
103 Arguments: []cmds.Argument{
104 cmds.StringArg("time", true, false, "Time to keep inactive requests in log."),
105 },
106 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
107 tval, err := time.ParseDuration(req.Arguments[0])
108 if err != nil {
109 return err
110 }
111 ctx := env.(*oldcmds.Context)
112 ctx.ReqLog.SetKeepTime(tval)
113
114 return nil
115 },
116 }