Skip to content

Commit ca0ae17

Browse files
committed
http(feat): add fallback executor support
This allows _trying_ to execute a command on a daemon, failing, and falling back on another executor.
1 parent ba094a0 commit ca0ae17

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

http/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type client struct {
2727
httpClient *http.Client
2828
ua string
2929
apiPrefix string
30+
fallback cmds.Executor
3031
}
3132

3233
type ClientOpt func(*client)
@@ -43,6 +44,15 @@ func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
4344
}
4445
}
4546

47+
// ClientWithFallback adds a fallback executor to the client.
48+
//
49+
// Note: This may run the PreRun function twice.
50+
func ClientWithFallback(exe cmds.Executor) ClientOpt {
51+
return func(c *client) {
52+
c.fallback = exe
53+
}
54+
}
55+
4656
func NewClient(address string, opts ...ClientOpt) cmds.Executor {
4757
if !strings.HasPrefix(address, "http://") {
4858
address = "http://" + address
@@ -79,6 +89,10 @@ func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.En
7989
res, err := c.send(req)
8090
if err != nil {
8191
if isConnRefused(err) {
92+
if c.fallback != nil {
93+
// XXX: this runs the PreRun twice
94+
return c.fallback.Execute(req, re, env)
95+
}
8296
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`")
8397
}
8498
return err

0 commit comments

Comments
 (0)