mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 04:17:44 +08:00
Merge pull request #5659 from overbool/refactor/use-new-cmds
refactor(cmds): use new cmds
This commit is contained in:
commit
887be02489
@ -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 (
|
||||
@ -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."),
|
||||
@ -35,38 +35,27 @@ 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
|
||||
}
|
||||
Encoders: cmds.EncoderMap{
|
||||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
|
||||
verbose, _ := req.Options[verboseOptionName].(bool)
|
||||
|
||||
out, ok := v.(*[]*cmds.ReqLogEntry)
|
||||
if !ok {
|
||||
return nil, e.TypeErr(out, v)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
verbose, _, _ := res.Request().Option(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)
|
||||
@ -74,9 +63,9 @@ Lists running and recently run commands.
|
||||
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
|
||||
@ -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{},
|
||||
}
|
||||
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -11,14 +10,14 @@ import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
e "github.com/ipfs/go-ipfs/core/commands/e"
|
||||
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
|
||||
repo "github.com/ipfs/go-ipfs/repo"
|
||||
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
|
||||
|
||||
"gx/ipfs/QmP2i47tnU23ijdshrZtuvrSkQPtf9HhsMb9fwGVe8owj2/jsondiff"
|
||||
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
|
||||
config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
|
||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
// ConfigUpdateOutput is config profile apply command's output
|
||||
@ -60,7 +59,12 @@ Set the value of the 'Datastore.Path' key:
|
||||
$ ipfs config Datastore.Path ~/.ipfs/datastore
|
||||
`,
|
||||
},
|
||||
|
||||
Subcommands: map[string]*cmds.Command{
|
||||
"show": configShowCmd,
|
||||
"edit": configEditCmd,
|
||||
"replace": configReplaceCmd,
|
||||
"profile": configProfileCmd,
|
||||
},
|
||||
Arguments: []cmdkit.Argument{
|
||||
cmdkit.StringArg("key", true, false, "The key of the config entry (e.g. \"Addresses.API\")."),
|
||||
cmdkit.StringArg("value", false, false, "The value to set the config entry to."),
|
||||
@ -69,46 +73,40 @@ Set the value of the 'Datastore.Path' key:
|
||||
cmdkit.BoolOption(configBoolOptionName, "Set a boolean value."),
|
||||
cmdkit.BoolOption(configJSONOptionName, "Parse stringified JSON."),
|
||||
},
|
||||
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
|
||||
key := args[0]
|
||||
|
||||
var output *ConfigField
|
||||
defer func() {
|
||||
if output != nil {
|
||||
res.SetOutput(output)
|
||||
} else {
|
||||
res.SetOutput(nil)
|
||||
}
|
||||
}()
|
||||
|
||||
// This is a temporary fix until we move the private key out of the config file
|
||||
switch strings.ToLower(key) {
|
||||
case "identity", "identity.privkey":
|
||||
res.SetError(fmt.Errorf("cannot show or change private key through API"), cmdkit.ErrNormal)
|
||||
return
|
||||
return fmt.Errorf("cannot show or change private key through API")
|
||||
default:
|
||||
}
|
||||
|
||||
r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
|
||||
cfgRoot, err := cmdenv.GetConfigRoot(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
r, err := fsrepo.Open(cfgRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
if len(args) == 2 {
|
||||
value := args[1]
|
||||
|
||||
if parseJSON, _, _ := req.Option(configJSONOptionName).Bool(); parseJSON {
|
||||
if parseJSON, _ := req.Options[configJSONOptionName].(bool); parseJSON {
|
||||
var jsonVal interface{}
|
||||
if err := json.Unmarshal([]byte(value), &jsonVal); err != nil {
|
||||
err = fmt.Errorf("failed to unmarshal json. %s", err)
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
output, err = setConfig(r, key, jsonVal)
|
||||
} else if isbool, _, _ := req.Option(configBoolOptionName).Bool(); isbool {
|
||||
} else if isbool, _ := req.Options[configBoolOptionName].(bool); isbool {
|
||||
output, err = setConfig(r, key, value == "true")
|
||||
} else {
|
||||
output, err = setConfig(r, key, value)
|
||||
@ -116,46 +114,30 @@ Set the value of the 'Datastore.Path' key:
|
||||
} else {
|
||||
output, err = getConfig(r, key)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
return res.Emit(output)
|
||||
},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||
if len(res.Request().Arguments()) == 2 {
|
||||
return nil, nil // dont output anything
|
||||
Encoders: cmds.EncoderMap{
|
||||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ConfigField) error {
|
||||
if len(req.Arguments) == 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if res.Error() != nil {
|
||||
return nil, res.Error()
|
||||
}
|
||||
|
||||
v, err := unwrapOutput(res.Output())
|
||||
buf, err := config.HumanOutput(out.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
vf, ok := v.(*ConfigField)
|
||||
if !ok {
|
||||
return nil, e.TypeErr(vf, v)
|
||||
}
|
||||
|
||||
buf, err := config.HumanOutput(vf.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
buf = append(buf, byte('\n'))
|
||||
return bytes.NewReader(buf), nil
|
||||
},
|
||||
|
||||
w.Write(buf)
|
||||
return nil
|
||||
}),
|
||||
},
|
||||
Type: ConfigField{},
|
||||
Subcommands: map[string]*cmds.Command{
|
||||
"show": configShowCmd,
|
||||
"edit": configEditCmd,
|
||||
"replace": configReplaceCmd,
|
||||
"profile": configProfileCmd,
|
||||
},
|
||||
}
|
||||
|
||||
var configShowCmd = &cmds.Command{
|
||||
@ -166,57 +148,46 @@ NOTE: For security reasons, this command will omit your private key. If you woul
|
||||
`,
|
||||
},
|
||||
Type: map[string]interface{}{},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
cfgPath := req.InvocContext().ConfigRoot
|
||||
fname, err := config.Filename(cfgPath)
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
cfgRoot, err := cmdenv.GetConfigRoot(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
fname, err := config.Filename(cfgRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(fname)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
var cfg map[string]interface{}
|
||||
err = json.Unmarshal(data, &cfg)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = scrubValue(cfg, []string{config.IdentityTag, config.PrivKeyTag})
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
res.SetOutput(&cfg)
|
||||
|
||||
return cmds.EmitOnce(res, &cfg)
|
||||
},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||
if res.Error() != nil {
|
||||
return nil, res.Error()
|
||||
}
|
||||
|
||||
v, err := unwrapOutput(res.Output())
|
||||
Encoders: cmds.EncoderMap{
|
||||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *map[string]interface{}) error {
|
||||
buf, err := config.HumanOutput(out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg, ok := v.(*map[string]interface{})
|
||||
if !ok {
|
||||
return nil, e.TypeErr(cfg, v)
|
||||
}
|
||||
|
||||
buf, err := config.HumanOutput(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
buf = append(buf, byte('\n'))
|
||||
return bytes.NewReader(buf), nil
|
||||
},
|
||||
w.Write(buf)
|
||||
|
||||
return nil
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
@ -270,17 +241,18 @@ variable set to your preferred text editor.
|
||||
`,
|
||||
},
|
||||
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
filename, err := config.Filename(req.InvocContext().ConfigRoot)
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
cfgRoot, err := cmdenv.GetConfigRoot(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = editConfig(filename)
|
||||
filename, err := config.Filename(cfgRoot)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return err
|
||||
}
|
||||
|
||||
return editConfig(filename)
|
||||
},
|
||||
}
|
||||
|
||||
@ -296,29 +268,25 @@ can't be undone.
|
||||
Arguments: []cmdkit.Argument{
|
||||
cmdkit.FileArg("file", true, false, "The file to use as the new config."),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
// has to be called
|
||||
res.SetOutput(nil)
|
||||
|
||||
r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
cfgRoot, err := cmdenv.GetConfigRoot(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
r, err := fsrepo.Open(cfgRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
file, err := req.Files().NextFile()
|
||||
file, err := req.Files.NextFile()
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = replaceConfig(r, file)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
return replaceConfig(r, file)
|
||||
},
|
||||
}
|
||||
|
||||
@ -346,58 +314,47 @@ var configProfileApplyCmd = &cmds.Command{
|
||||
Arguments: []cmdkit.Argument{
|
||||
cmdkit.StringArg("profile", true, false, "The profile to apply to the config."),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
profile, ok := config.Profiles[req.Arguments()[0]]
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
profile, ok := config.Profiles[req.Arguments[0]]
|
||||
if !ok {
|
||||
res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmdkit.ErrNormal)
|
||||
return
|
||||
return fmt.Errorf("%s is not a profile", req.Arguments[0])
|
||||
}
|
||||
|
||||
dryRun, _, _ := req.Option("dry-run").Bool()
|
||||
oldCfg, newCfg, err := transformConfig(req.InvocContext().ConfigRoot, req.Arguments()[0], profile.Transform, dryRun)
|
||||
dryRun, _ := req.Options["dry-run"].(bool)
|
||||
cfgRoot, err := cmdenv.GetConfigRoot(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
oldCfg, newCfg, err := transformConfig(cfgRoot, req.Arguments[0], profile.Transform, dryRun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
oldCfgMap, err := scrubPrivKey(oldCfg)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
newCfgMap, err := scrubPrivKey(newCfg)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
res.SetOutput(&ConfigUpdateOutput{
|
||||
return cmds.EmitOnce(res, &ConfigUpdateOutput{
|
||||
OldCfg: oldCfgMap,
|
||||
NewCfg: newCfgMap,
|
||||
})
|
||||
},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||
if res.Error() != nil {
|
||||
return nil, res.Error()
|
||||
}
|
||||
|
||||
v, err := unwrapOutput(res.Output())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apply, ok := v.(*ConfigUpdateOutput)
|
||||
if !ok {
|
||||
return nil, e.TypeErr(apply, v)
|
||||
}
|
||||
|
||||
diff := jsondiff.Compare(apply.OldCfg, apply.NewCfg)
|
||||
Encoders: cmds.EncoderMap{
|
||||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ConfigUpdateOutput) error {
|
||||
diff := jsondiff.Compare(out.OldCfg, out.NewCfg)
|
||||
buf := jsondiff.Format(diff)
|
||||
|
||||
return strings.NewReader(string(buf)), nil
|
||||
},
|
||||
w.Write(buf)
|
||||
|
||||
return nil
|
||||
}),
|
||||
},
|
||||
Type: ConfigUpdateOutput{},
|
||||
}
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
cmds "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"
|
||||
)
|
||||
|
||||
var DiagCmd = &cmds.Command{
|
||||
|
||||
@ -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/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/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,19 @@ 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 cmds.EmitOnce(res, &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 +83,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 cmds.EmitOnce(res, &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 +105,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/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
|
||||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
var MountCmd = &cmds.Command{
|
||||
|
||||
@ -5,14 +5,18 @@ package commands
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
e "github.com/ipfs/go-ipfs/core/commands/e"
|
||||
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
|
||||
nodeMount "github.com/ipfs/go-ipfs/fuse/node"
|
||||
|
||||
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
|
||||
config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
|
||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
const (
|
||||
mountIPFSPathOptionName = "ipfs-path"
|
||||
mountIPNSPathOptionName = "ipns-path"
|
||||
)
|
||||
|
||||
var MountCmd = &cmds.Command{
|
||||
@ -73,74 +77,53 @@ baz
|
||||
`,
|
||||
},
|
||||
Options: []cmdkit.Option{
|
||||
cmdkit.StringOption("ipfs-path", "f", "The path where IPFS should be mounted."),
|
||||
cmdkit.StringOption("ipns-path", "n", "The path where IPNS should be mounted."),
|
||||
cmdkit.StringOption(mountIPFSPathOptionName, "f", "The path where IPFS should be mounted."),
|
||||
cmdkit.StringOption(mountIPNSPathOptionName, "n", "The path where IPNS should be mounted."),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
cfg, err := req.InvocContext().GetConfig()
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
cfg, err := cmdenv.GetConfig(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
node, err := req.InvocContext().GetNode()
|
||||
nd, err := cmdenv.GetNode(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
// error if we aren't running node in online mode
|
||||
if node.LocalMode() {
|
||||
res.SetError(ErrNotOnline, cmdkit.ErrClient)
|
||||
return
|
||||
if nd.LocalMode() {
|
||||
return ErrNotOnline
|
||||
}
|
||||
|
||||
fsdir, found, err := req.Option("f").String()
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
fsdir, found := req.Options[mountIPFSPathOptionName].(string)
|
||||
if !found {
|
||||
fsdir = cfg.Mounts.IPFS // use default value
|
||||
}
|
||||
|
||||
// get default mount points
|
||||
nsdir, found, err := req.Option("n").String()
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
nsdir, found := req.Options[mountIPNSPathOptionName].(string)
|
||||
if !found {
|
||||
nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare!
|
||||
}
|
||||
|
||||
err = nodeMount.Mount(node, fsdir, nsdir)
|
||||
err = nodeMount.Mount(nd, fsdir, nsdir)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
var output config.Mounts
|
||||
output.IPFS = fsdir
|
||||
output.IPNS = nsdir
|
||||
res.SetOutput(&output)
|
||||
return cmds.EmitOnce(res, &output)
|
||||
},
|
||||
Type: config.Mounts{},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||
v, err := unwrapOutput(res.Output())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
Encoders: cmds.EncoderMap{
|
||||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, mounts *config.Mounts) error {
|
||||
fmt.Fprintf(w, "IPFS mounted at: %s\n", mounts.IPFS)
|
||||
fmt.Fprintf(w, "IPNS mounted at: %s\n", mounts.IPNS)
|
||||
|
||||
mnts, ok := v.(*config.Mounts)
|
||||
if !ok {
|
||||
return nil, e.TypeErr(mnts, v)
|
||||
}
|
||||
|
||||
s := fmt.Sprintf("IPFS mounted at: %s\n", mnts.IPFS)
|
||||
s += fmt.Sprintf("IPNS mounted at: %s\n", mnts.IPNS)
|
||||
return strings.NewReader(s), nil
|
||||
},
|
||||
return nil
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
@ -3,8 +3,7 @@ package commands
|
||||
import (
|
||||
"errors"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
|
||||
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
|
||||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
@ -14,7 +13,7 @@ var MountCmd = &cmds.Command{
|
||||
ShortDescription: "Not yet implemented on Windows. :(",
|
||||
},
|
||||
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
res.SetError(errors.New("Mount isn't compatible with Windows yet"), cmdkit.ErrNormal)
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
return errors.New("Mount isn't compatible with Windows yet")
|
||||
},
|
||||
}
|
||||
|
||||
@ -123,16 +123,16 @@ var rootSubcommands = map[string]*cmds.Command{
|
||||
"repo": RepoCmd,
|
||||
"stats": StatsCmd,
|
||||
"bootstrap": lgc.NewCommand(BootstrapCmd),
|
||||
"config": lgc.NewCommand(ConfigCmd),
|
||||
"config": ConfigCmd,
|
||||
"dag": dag.DagCmd,
|
||||
"dht": DhtCmd,
|
||||
"diag": lgc.NewCommand(DiagCmd),
|
||||
"diag": DiagCmd,
|
||||
"dns": DNSCmd,
|
||||
"id": IDCmd,
|
||||
"key": KeyCmd,
|
||||
"log": lgc.NewCommand(LogCmd),
|
||||
"log": LogCmd,
|
||||
"ls": lgc.NewCommand(LsCmd),
|
||||
"mount": lgc.NewCommand(MountCmd),
|
||||
"mount": MountCmd,
|
||||
"name": name.NameCmd,
|
||||
"object": ocmd.ObjectCmd,
|
||||
"pin": lgc.NewCommand(PinCmd),
|
||||
|
||||
@ -6,11 +6,12 @@ import (
|
||||
"runtime"
|
||||
|
||||
version "github.com/ipfs/go-ipfs"
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
|
||||
|
||||
manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
|
||||
sysi "gx/ipfs/QmZRjKbHa6DenStpQJFiaPcEwkZqrx7TH6xTf342LDU3qM/go-sysinfo"
|
||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
|
||||
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||
)
|
||||
|
||||
var sysDiagCmd = &cmds.Command{
|
||||
@ -20,46 +21,40 @@ var sysDiagCmd = &cmds.Command{
|
||||
Prints out information about your computer to aid in easier debugging.
|
||||
`,
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
info := make(map[string]interface{})
|
||||
err := runtimeInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = envVarInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = diskSpaceInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = memInfo(info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
node, err := req.InvocContext().GetNode()
|
||||
nd, err := cmdenv.GetNode(env)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = netInfo(node.OnlineMode(), info)
|
||||
err = netInfo(nd.OnlineMode(), info)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
info["ipfs_version"] = version.CurrentVersionNumber
|
||||
info["ipfs_commit"] = version.CurrentCommit
|
||||
res.SetOutput(info)
|
||||
return cmds.EmitOnce(res, info)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user