Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cmds): use new cmds #5659

Merged
merged 8 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 33 additions & 41 deletions core/commands/active.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package commands

import (
"bytes"
"fmt"
"io"
"sort"
"text/tabwriter"
"time"

cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
oldcmds "github.com/ipfs/go-ipfs/commands"

"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

const (
Expand All @@ -25,8 +24,9 @@ var ActiveReqsCmd = &cmds.Command{
Lists running and recently run commands.
`,
},
Run: func(req cmds.Request, res cmds.Response) {
res.SetOutput(req.InvocContext().ReqLog.Report())
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
return cmds.EmitOnce(res, ctx.ReqLog.Report())
},
Options: []cmdkit.Option{
cmdkit.BoolOption("verbose", verboseOptionName, "Print extra information."),
Expand All @@ -35,48 +35,37 @@ Lists running and recently run commands.
"clear": clearInactiveCmd,
"set-time": setRequestClearCmd,
},
Marshalers: map[cmds.EncodingType]cmds.Marshaler{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

out, ok := v.(*[]*cmds.ReqLogEntry)
if !ok {
return nil, e.TypeErr(out, v)
}
buf := new(bytes.Buffer)

verbose, _, _ := res.Request().Option(verboseOptionName).Bool()
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
verbose, _ := req.Options[verboseOptionName].(bool)

w := tabwriter.NewWriter(buf, 4, 4, 2, ' ', 0)
tw := tabwriter.NewWriter(w, 4, 4, 2, ' ', 0)
if verbose {
fmt.Fprint(w, "ID\t")
fmt.Fprint(tw, "ID\t")
}
fmt.Fprint(w, "Command\t")
fmt.Fprint(tw, "Command\t")
if verbose {
fmt.Fprint(w, "Arguments\tOptions\t")
fmt.Fprint(tw, "Arguments\tOptions\t")
}
fmt.Fprintln(w, "Active\tStartTime\tRunTime")
fmt.Fprintln(tw, "Active\tStartTime\tRunTime")

for _, req := range *out {
if verbose {
fmt.Fprintf(w, "%d\t", req.ID)
fmt.Fprintf(tw, "%d\t", req.ID)
}
fmt.Fprintf(w, "%s\t", req.Command)
fmt.Fprintf(tw, "%s\t", req.Command)
if verbose {
fmt.Fprintf(w, "%v\t[", req.Args)
fmt.Fprintf(tw, "%v\t[", req.Args)
var keys []string
for k := range req.Options {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
fmt.Fprintf(w, "%s=%v,", k, req.Options[k])
fmt.Fprintf(tw, "%s=%v,", k, req.Options[k])
}
fmt.Fprintf(w, "]\t")
fmt.Fprintf(tw, "]\t")
}

var live time.Duration
Expand All @@ -86,12 +75,12 @@ Lists running and recently run commands.
live = req.EndTime.Sub(req.StartTime)
}
t := req.StartTime.Format(time.Stamp)
fmt.Fprintf(w, "%t\t%s\t%s\n", req.Active, t, live)
fmt.Fprintf(tw, "%t\t%s\t%s\n", req.Active, t, live)
}
w.Flush()
tw.Flush()

return buf, nil
},
return nil
}),
},
Type: []*cmds.ReqLogEntry{},
}
Expand All @@ -100,8 +89,10 @@ var clearInactiveCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Clear inactive requests from the log.",
},
Run: func(req cmds.Request, res cmds.Response) {
req.InvocContext().ReqLog.ClearInactive()
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
ctx.ReqLog.ClearInactive()
return nil
},
}

Expand All @@ -112,13 +103,14 @@ var setRequestClearCmd = &cmds.Command{
Arguments: []cmdkit.Argument{
cmdkit.StringArg("time", true, false, "Time to keep inactive requests in log."),
},
Run: func(req cmds.Request, res cmds.Response) {
tval, err := time.ParseDuration(req.Arguments()[0])
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
tval, err := time.ParseDuration(req.Arguments[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}
ctx := env.(*oldcmds.Context)
ctx.ReqLog.SetKeepTime(tval)

req.InvocContext().ReqLog.SetKeepTime(tval)
return nil
},
}
Loading