@cryptotaxi247 / kubo / commits / 264a9fee6

add `allow-offline` to judge publish in offline mode

License: MIT Signed-off-by: Kejie Zhang <601172892@qq.com>

Kejie Zhang committed Sep 17, 2018 at 16:10 UTC 264a9fee646c54c146c05af9d4ae22c5e504adb0
1 file changed +32 -11
core/commands/name/publish.go
+32 -11
@@ -19,6 +19,21 @@ import (
19 path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
20 )
21
22 +var (
23 + ErrAllowOffline = errors.New("cannot publish in offline mode by default,using `--allow-offline` to publish again")
24 + ErrIpnsMount = errors.New("cannot manually publish while IPNS is mounted")
25 + ErrIdentityLoad = errors.New("identity not loaded")
26 +)
27 +
28 +const (
29 + ipfsPathOptionName = "ipfs-path"
30 + resolveOptionName = "resolve"
31 + allowOfflineOptionName = "allow-offline"
32 + lifeTimeOptionName = "lifetime"
33 + ttlOptionName = "ttl"
34 + keyOptionName = "key"
35 +)
36 +
37 var PublishCmd = &cmds.Command{
38 Helptext: cmdkit.HelpText{
39 Tagline: "Publish IPNS names.",
@@ -60,16 +75,17 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
75 },
76
77 Arguments: []cmdkit.Argument{
63 - cmdkit.StringArg("ipfs-path", true, false, "ipfs path of the object to be published.").EnableStdin(),
78 + cmdkit.StringArg(ipfsPathOptionName, true, false, "ipfs path of the object to be published.").EnableStdin(),
79 },
80 Options: []cmdkit.Option{
66 - cmdkit.BoolOption("resolve", "Resolve given path before publishing.").WithDefault(true),
67 - cmdkit.StringOption("lifetime", "t",
81 + cmdkit.BoolOption(resolveOptionName, "Resolve given path before publishing.").WithDefault(true),
82 + cmdkit.StringOption(lifeTimeOptionName, "t",
83 `Time duration that the record will be valid for. <<default>>
84 This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are
85 "ns", "us" (or "µs"), "ms", "s", "m", "h".`).WithDefault("24h"),
71 - cmdkit.StringOption("ttl", "Time duration this record should be cached for (caution: experimental)."),
72 - cmdkit.StringOption("key", "k", "Name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'. Default: <<default>>.").WithDefault("self"),
86 + cmdkit.BoolOption(allowOfflineOptionName, "Allow publish in offline mode"),
87 + cmdkit.StringOption(ttlOptionName, "Time duration this record should be cached for (caution: experimental)."),
88 + cmdkit.StringOption(keyOptionName, "k", "Name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'. Default: <<default>>.").WithDefault("self"),
89 },
90 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
91 n, err := cmdenv.GetNode(env)
@@ -78,7 +94,12 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
94 return
95 }
96
97 + allowOffline, _ := req.Options[allowOfflineOptionName].(bool)
98 if !n.OnlineMode() {
99 + if !allowOffline {
100 + res.SetError(ErrAllowOffline,cmdkit.ErrNormal)
101 + return
102 + }
103 err := n.SetupOfflineRouting()
104 if err != nil {
105 res.SetError(err, cmdkit.ErrNormal)
@@ -87,22 +108,22 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
108 }
109
110 if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
90 - res.SetError(errors.New("cannot manually publish while IPNS is mounted"), cmdkit.ErrNormal)
111 + res.SetError(ErrIpnsMount, cmdkit.ErrNormal)
112 return
113 }
114
115 pstr := req.Arguments[0]
116
117 if n.Identity == "" {
97 - res.SetError(errors.New("identity not loaded"), cmdkit.ErrNormal)
118 + res.SetError(ErrIdentityLoad, cmdkit.ErrNormal)
119 return
120 }
121
122 popts := new(publishOpts)
123
103 - popts.verifyExists, _ = req.Options["resolve"].(bool)
124 + popts.verifyExists, _ = req.Options[resolveOptionName].(bool)
125
105 - validtime, _ := req.Options["lifetime"].(string)
126 + validtime, _ := req.Options[lifeTimeOptionName].(string)
127 d, err := time.ParseDuration(validtime)
128 if err != nil {
129 res.SetError(fmt.Errorf("error parsing lifetime option: %s", err), cmdkit.ErrNormal)
@@ -112,7 +133,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
133 popts.pubValidTime = d
134
135 ctx := req.Context
115 - if ttl, found := req.Options["ttl"].(string); found {
136 + if ttl, found := req.Options[ttlOptionName].(string); found {
137 d, err := time.ParseDuration(ttl)
138 if err != nil {
139 res.SetError(err, cmdkit.ErrNormal)
@@ -122,7 +143,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
143 ctx = context.WithValue(ctx, "ipns-publish-ttl", d)
144 }
145
125 - kname, _ := req.Options["key"].(string)
146 + kname, _ := req.Options[keyOptionName].(string)
147 k, err := keylookup(n, kname)
148 if err != nil {
149 res.SetError(err, cmdkit.ErrNormal)