From 37ee0e21ddb43f1e817f4e2872f8cc185b594af2 Mon Sep 17 00:00:00 2001 From: Overbool Date: Fri, 26 Oct 2018 23:54:30 +0800 Subject: [PATCH] refactor(cmds): use new cmds lib in log License: MIT Signed-off-by: Overbool --- core/commands/log.go | 63 +++++++++++++--------------------- core/commands/mount_nofuse.go | 5 ++- core/commands/mount_windows.go | 3 +- core/commands/root.go | 2 +- 4 files changed, 27 insertions(+), 46 deletions(-) diff --git a/core/commands/log.go b/core/commands/log.go index 9d31f9c31e53..035c8e96fa94 100644 --- a/core/commands/log.go +++ b/core/commands/log.go @@ -1,16 +1,13 @@ package commands import ( - "bytes" "fmt" "io" - cmds "github.com/ipfs/go-ipfs/commands" - e "github.com/ipfs/go-ipfs/core/commands/e" - + cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds" logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log" lwriter "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log/writer" - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) // Golang os.Args overrides * and replaces the character argument with @@ -52,9 +49,8 @@ the event log. One of: debug, info, warning, error, critical. `), }, - Run: func(req cmds.Request, res cmds.Response) { - - args := req.Arguments() + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + args := req.Arguments subsystem, level := args[0], args[1] if subsystem == logAllKeyword { @@ -62,16 +58,18 @@ the event log. } if err := logging.SetLogLevel(subsystem, level); err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } s := fmt.Sprintf("Changed log level of '%s' to '%s'\n", subsystem, level) log.Info(s) - res.SetOutput(&MessageOutput{s}) + return res.Emit(&MessageOutput{s}) }, - Marshalers: cmds.MarshalerMap{ - cmds.Text: MessageTextMarshaler, + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *MessageOutput) error { + fmt.Fprint(w, out.Message) + return nil + }), }, Type: MessageOutput{}, } @@ -84,11 +82,16 @@ var logLsCmd = &cmds.Command{ subsystems of a running daemon. `, }, - Run: func(req cmds.Request, res cmds.Response) { - res.SetOutput(&stringList{logging.GetSubsystems()}) + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + return res.Emit(&stringList{logging.GetSubsystems()}) }, - Marshalers: cmds.MarshalerMap{ - cmds.Text: stringListMarshaler, + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *stringList) error { + for _, s := range list.Strings { + fmt.Fprintln(w, s) + } + return nil + }), }, Type: stringList{}, } @@ -101,34 +104,14 @@ Outputs event log messages (not other log messages) as they are generated. `, }, - Run: func(req cmds.Request, res cmds.Response) { - ctx := req.Context() + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + ctx := req.Context r, w := io.Pipe() go func() { defer w.Close() <-ctx.Done() }() lwriter.WriterGroup.AddWriter(w) - res.SetOutput(r) + return res.Emit(r) }, } - -func stringListMarshaler(res cmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - list, ok := v.(*stringList) - if !ok { - return nil, e.TypeErr(list, v) - } - - buf := new(bytes.Buffer) - for _, s := range list.Strings { - buf.WriteString(s) - buf.WriteString("\n") - } - - return buf, nil -} diff --git a/core/commands/mount_nofuse.go b/core/commands/mount_nofuse.go index 8e67a7cd1c22..8b21729c7e03 100644 --- a/core/commands/mount_nofuse.go +++ b/core/commands/mount_nofuse.go @@ -3,9 +3,8 @@ package commands import ( - cmds "github.com/ipfs/go-ipfs/commands" - - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" + cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds" + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) var MountCmd = &cmds.Command{ diff --git a/core/commands/mount_windows.go b/core/commands/mount_windows.go index 9114ba9c4b25..1840dea1c4f3 100644 --- a/core/commands/mount_windows.go +++ b/core/commands/mount_windows.go @@ -3,8 +3,7 @@ package commands import ( "errors" - cmds "github.com/ipfs/go-ipfs/commands" - + cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) diff --git a/core/commands/root.go b/core/commands/root.go index 010a27fcd8dd..93b8ce10f4ab 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -130,7 +130,7 @@ var rootSubcommands = map[string]*cmds.Command{ "dns": DNSCmd, "id": IDCmd, "key": KeyCmd, - "log": lgc.NewCommand(LogCmd), + "log": LogCmd, "ls": lgc.NewCommand(LsCmd), "mount": MountCmd, "name": name.NameCmd,