mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-03 15:27:57 +08:00
refactor(cmds): use new cmds lib in log
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
This commit is contained in:
parent
27bc0ecc2e
commit
35959d623b
@ -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/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
|
||||
lwriter "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/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
|
||||
}
|
||||
|
||||
@ -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{
|
||||
|
||||
@ -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"
|
||||
)
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user