Skip to content

Commit

Permalink
add allow-offline to judge publish in offline mode
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kejie Zhang <601172892@qq.com>
  • Loading branch information
kjzz committed Sep 17, 2018
1 parent e71ca7c commit 264a9fe
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions core/commands/name/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ import (
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{
Helptext: cmdkit.HelpText{
Tagline: "Publish IPNS names.",
Expand Down Expand Up @@ -60,16 +75,17 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
},

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{
cmdkit.BoolOption("resolve", "Resolve given path before publishing.").WithDefault(true),
cmdkit.StringOption("lifetime", "t",
cmdkit.BoolOption(resolveOptionName, "Resolve given path before publishing.").WithDefault(true),
cmdkit.StringOption(lifeTimeOptionName, "t",
`Time duration that the record will be valid for. <<default>>
This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are
"ns", "us" (or "µs"), "ms", "s", "m", "h".`).WithDefault("24h"),
cmdkit.StringOption("ttl", "Time duration this record should be cached for (caution: experimental)."),
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.BoolOption(allowOfflineOptionName, "Allow publish in offline mode"),
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) {
n, err := cmdenv.GetNode(env)
Expand All @@ -78,7 +94,12 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
return
}

allowOffline, _ := req.Options[allowOfflineOptionName].(bool)
if !n.OnlineMode() {
if !allowOffline {
res.SetError(ErrAllowOffline,cmdkit.ErrNormal)
return
}
err := n.SetupOfflineRouting()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand All @@ -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() {
res.SetError(errors.New("cannot manually publish while IPNS is mounted"), cmdkit.ErrNormal)
res.SetError(ErrIpnsMount, cmdkit.ErrNormal)
return
}

pstr := req.Arguments[0]

if n.Identity == "" {
res.SetError(errors.New("identity not loaded"), cmdkit.ErrNormal)
res.SetError(ErrIdentityLoad, cmdkit.ErrNormal)
return
}

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)
if err != nil {
res.SetError(fmt.Errorf("error parsing lifetime option: %s", err), cmdkit.ErrNormal)
Expand All @@ -112,7 +133,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
popts.pubValidTime = d

ctx := req.Context
if ttl, found := req.Options["ttl"].(string); found {
if ttl, found := req.Options[ttlOptionName].(string); found {
d, err := time.ParseDuration(ttl)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand All @@ -122,7 +143,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
ctx = context.WithValue(ctx, "ipns-publish-ttl", d)
}

kname, _ := req.Options["key"].(string)
kname, _ := req.Options[keyOptionName].(string)
k, err := keylookup(n, kname)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand Down

0 comments on commit 264a9fe

Please sign in to comment.