@cryptotaxi247 / kubo / commits / 27bc0ecc2

refactor(cmds): use new cmds lib in mount

License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>

Overbool committed Oct 26, 2018 at 23:43 UTC 27bc0ecc2e691c353e3f2bbe75bfcf83efa3c8f6
2 files changed +22 -44
core/commands/mount_unix.go
+21 -43
@@ -5,12 +5,11 @@ package commands
5 import (
6 "fmt"
7 "io"
8 - "strings"
8
10 - cmds "github.com/ipfs/go-ipfs/commands"
11 - e "github.com/ipfs/go-ipfs/core/commands/e"
9 + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10 nodeMount "github.com/ipfs/go-ipfs/fuse/node"
11
12 + config "gx/ipfs/QmPEpj17FDRpc7K1aArKZp3RsHtzRMKykeK9GVgn4WQGPR/go-ipfs-config"
13 config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
14 "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
15 )
@@ -76,71 +75,50 @@ baz
75 cmdkit.StringOption("ipfs-path", "f", "The path where IPFS should be mounted."),
76 cmdkit.StringOption("ipns-path", "n", "The path where IPNS should be mounted."),
77 },
79 - Run: func(req cmds.Request, res cmds.Response) {
80 - cfg, err := req.InvocContext().GetConfig()
78 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
79 + cfg, err := cmdenv.GetConfig(env)
80 if err != nil {
82 - res.SetError(err, cmdkit.ErrNormal)
83 - return
81 + return err
82 }
83
86 - node, err := req.InvocContext().GetNode()
84 + nd, err := cmdenv.GetNode(env)
85 if err != nil {
88 - res.SetError(err, cmdkit.ErrNormal)
89 - return
86 + return err
87 }
88
89 // error if we aren't running node in online mode
93 - if node.LocalMode() {
94 - res.SetError(ErrNotOnline, cmdkit.ErrClient)
95 - return
90 + if nd.LocalMode() {
91 + return err
92 }
93
98 - fsdir, found, err := req.Option("f").String()
99 - if err != nil {
100 - res.SetError(err, cmdkit.ErrNormal)
101 - return
102 - }
94 + fsdir, found := req.Options["f"].(string)
95 if !found {
96 fsdir = cfg.Mounts.IPFS // use default value
97 }
98
99 // get default mount points
108 - nsdir, found, err := req.Option("n").String()
109 - if err != nil {
110 - res.SetError(err, cmdkit.ErrNormal)
111 - return
112 - }
100 + nsdir, found := req.Options["n"].(string)
101 if !found {
102 nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare!
103 }
104
117 - err = nodeMount.Mount(node, fsdir, nsdir)
105 + err = nodeMount.Mount(nd, fsdir, nsdir)
106 if err != nil {
119 - res.SetError(err, cmdkit.ErrNormal)
120 - return
107 + return err
108 }
109
110 var output config.Mounts
111 output.IPFS = fsdir
112 output.IPNS = nsdir
126 - res.SetOutput(&output)
113 + return res.Emit(&output)
114 },
115 Type: config.Mounts{},
129 - Marshalers: cmds.MarshalerMap{
130 - cmds.Text: func(res cmds.Response) (io.Reader, error) {
131 - v, err := unwrapOutput(res.Output())
132 - if err != nil {
133 - return nil, err
134 - }
135 -
136 - mnts, ok := v.(*config.Mounts)
137 - if !ok {
138 - return nil, e.TypeErr(mnts, v)
139 - }
140 -
141 - s := fmt.Sprintf("IPFS mounted at: %s\n", mnts.IPFS)
142 - s += fmt.Sprintf("IPNS mounted at: %s\n", mnts.IPNS)
143 - return strings.NewReader(s), nil
144 - },
116 + Encoders: cmds.EncoderMap{
117 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, mounts *config.Mounts) error {
118 + s := fmt.Sprintf("IPFS mounted at: %s\n", mounts.IPFS)
119 + s += fmt.Sprintf("IPNS mounted at: %s\n", mounts.IPNS)
120 + fmt.Fprint(w, s)
121 + return nil
122 + }),
123 },
124 }
core/commands/root.go
+1 -1
@@ -132,7 +132,7 @@ var rootSubcommands = map[string]*cmds.Command{
132 "key": KeyCmd,
133 "log": lgc.NewCommand(LogCmd),
134 "ls": lgc.NewCommand(LsCmd),
135 - "mount": lgc.NewCommand(MountCmd),
135 + "mount": MountCmd,
136 "name": name.NameCmd,
137 "object": ocmd.ObjectCmd,
138 "pin": lgc.NewCommand(PinCmd),