From ca0ae17464a21c55a43630d84f69e8bf7528063d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 13 Jun 2019 16:55:34 -0700 Subject: [PATCH] http(feat): add fallback executor support This allows _trying_ to execute a command on a daemon, failing, and falling back on another executor. --- http/client.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/http/client.go b/http/client.go index e17256e..3ac2925 100644 --- a/http/client.go +++ b/http/client.go @@ -27,6 +27,7 @@ type client struct { httpClient *http.Client ua string apiPrefix string + fallback cmds.Executor } type ClientOpt func(*client) @@ -43,6 +44,15 @@ func ClientWithAPIPrefix(apiPrefix string) ClientOpt { } } +// ClientWithFallback adds a fallback executor to the client. +// +// Note: This may run the PreRun function twice. +func ClientWithFallback(exe cmds.Executor) ClientOpt { + return func(c *client) { + c.fallback = exe + } +} + func NewClient(address string, opts ...ClientOpt) cmds.Executor { if !strings.HasPrefix(address, "http://") { address = "http://" + address @@ -79,6 +89,10 @@ func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.En res, err := c.send(req) if err != nil { if isConnRefused(err) { + if c.fallback != nil { + // XXX: this runs the PreRun twice + return c.fallback.Execute(req, re, env) + } err = fmt.Errorf("cannot connect to the api. Is the daemon running? To run as a standalone CLI command remove the api file in `$IPFS_PATH/api`") } return err -- GitLab