提交 264a9fee 作者: Kejie Zhang

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

License: MIT
Signed-off-by: 's avatarKejie Zhang <601172892@qq.com>
上级 e71ca7c5
...@@ -19,6 +19,21 @@ import ( ...@@ -19,6 +19,21 @@ import (
path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path" path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
) )
var (
ErrAllowOffline = errors.New("cannot publish in offline mode by default,using `--allow-offline` to publish again")
ErrIpnsMount = errors.New("cannot manually publish while IPNS is mounted")
ErrIdentityLoad = errors.New("identity not loaded")
)
const (
ipfsPathOptionName = "ipfs-path"
resolveOptionName = "resolve"
allowOfflineOptionName = "allow-offline"
lifeTimeOptionName = "lifetime"
ttlOptionName = "ttl"
keyOptionName = "key"
)
var PublishCmd = &cmds.Command{ var PublishCmd = &cmds.Command{
Helptext: cmdkit.HelpText{ Helptext: cmdkit.HelpText{
Tagline: "Publish IPNS names.", Tagline: "Publish IPNS names.",
...@@ -60,16 +75,17 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by ...@@ -60,16 +75,17 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
}, },
Arguments: []cmdkit.Argument{ Arguments: []cmdkit.Argument{
cmdkit.StringArg("ipfs-path", true, false, "ipfs path of the object to be published.").EnableStdin(), cmdkit.StringArg(ipfsPathOptionName, true, false, "ipfs path of the object to be published.").EnableStdin(),
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption("resolve", "Resolve given path before publishing.").WithDefault(true), cmdkit.BoolOption(resolveOptionName, "Resolve given path before publishing.").WithDefault(true),
cmdkit.StringOption("lifetime", "t", cmdkit.StringOption(lifeTimeOptionName, "t",
`Time duration that the record will be valid for. <<default>> `Time duration that the record will be valid for. <<default>>
This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are
"ns", "us" (or "µs"), "ms", "s", "m", "h".`).WithDefault("24h"), "ns", "us" (or "µs"), "ms", "s", "m", "h".`).WithDefault("24h"),
cmdkit.StringOption("ttl", "Time duration this record should be cached for (caution: experimental)."), cmdkit.BoolOption(allowOfflineOptionName, "Allow publish in offline mode"),
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"), cmdkit.StringOption(ttlOptionName, "Time duration this record should be cached for (caution: experimental)."),
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"),
}, },
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
n, err := cmdenv.GetNode(env) n, err := cmdenv.GetNode(env)
...@@ -78,7 +94,12 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by ...@@ -78,7 +94,12 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
return return
} }
allowOffline, _ := req.Options[allowOfflineOptionName].(bool)
if !n.OnlineMode() { if !n.OnlineMode() {
if !allowOffline {
res.SetError(ErrAllowOffline,cmdkit.ErrNormal)
return
}
err := n.SetupOfflineRouting() err := n.SetupOfflineRouting()
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
...@@ -87,22 +108,22 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by ...@@ -87,22 +108,22 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
} }
if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() { if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
res.SetError(errors.New("cannot manually publish while IPNS is mounted"), cmdkit.ErrNormal) res.SetError(ErrIpnsMount, cmdkit.ErrNormal)
return return
} }
pstr := req.Arguments[0] pstr := req.Arguments[0]
if n.Identity == "" { if n.Identity == "" {
res.SetError(errors.New("identity not loaded"), cmdkit.ErrNormal) res.SetError(ErrIdentityLoad, cmdkit.ErrNormal)
return return
} }
popts := new(publishOpts) popts := new(publishOpts)
popts.verifyExists, _ = req.Options["resolve"].(bool) popts.verifyExists, _ = req.Options[resolveOptionName].(bool)
validtime, _ := req.Options["lifetime"].(string) validtime, _ := req.Options[lifeTimeOptionName].(string)
d, err := time.ParseDuration(validtime) d, err := time.ParseDuration(validtime)
if err != nil { if err != nil {
res.SetError(fmt.Errorf("error parsing lifetime option: %s", err), cmdkit.ErrNormal) 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 ...@@ -112,7 +133,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
popts.pubValidTime = d popts.pubValidTime = d
ctx := req.Context ctx := req.Context
if ttl, found := req.Options["ttl"].(string); found { if ttl, found := req.Options[ttlOptionName].(string); found {
d, err := time.ParseDuration(ttl) d, err := time.ParseDuration(ttl)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
...@@ -122,7 +143,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by ...@@ -122,7 +143,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
ctx = context.WithValue(ctx, "ipns-publish-ttl", d) ctx = context.WithValue(ctx, "ipns-publish-ttl", d)
} }
kname, _ := req.Options["key"].(string) kname, _ := req.Options[keyOptionName].(string)
k, err := keylookup(n, kname) k, err := keylookup(n, kname)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论