Skip to content

Commit

Permalink
refactor(cmds): use new cmds lib in log
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Oct 26, 2018
1 parent be13e6a commit 37ee0e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 46 deletions.
63 changes: 23 additions & 40 deletions core/commands/log.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -52,26 +49,27 @@ 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 {
subsystem = "*"
}

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{},
}
Expand All @@ -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{},
}
Expand All @@ -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
}
5 changes: 2 additions & 3 deletions core/commands/mount_nofuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions core/commands/mount_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 37ee0e2

Please sign in to comment.