Skip to content

Commit aced0fd

Browse files
Jorropohacdias
authored andcommitted
fix: allow daemon to start correctly if the API is null
Fixes: #10056
1 parent 01cc5ea commit aced0fd

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

cmd/ipfs/daemon.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,11 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
727727
return nil, fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err)
728728
}
729729

730-
if err := node.Repo.SetAPIAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr())); err != nil {
731-
return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %w", err)
730+
if len(listeners) > 0 {
731+
// Only add an api file if the API is running.
732+
if err := node.Repo.SetAPIAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr())); err != nil {
733+
return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %w", err)
734+
}
732735
}
733736

734737
errc := make(chan error)

test/cli/daemon_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ipfs/kubo/test/cli/harness"
7+
)
8+
9+
func TestDaemon(t *testing.T) {
10+
t.Parallel()
11+
12+
t.Run("daemon starts if api is set to null", func(t *testing.T) {
13+
t.Parallel()
14+
node := harness.NewT(t).NewNode().Init()
15+
node.SetIPFSConfig("API", nil)
16+
node.IPFS("daemon") // can't use .StartDaemon because it do a .WaitOnAPI
17+
})
18+
}

test/cli/harness/ipfs.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,20 @@ func (n *Node) SetIPFSConfig(key string, val interface{}, flags ...string) {
3838
n.IPFS(args...)
3939

4040
// validate the config was set correctly
41-
var newVal string
42-
n.GetIPFSConfig(key, &newVal)
43-
if val != newVal {
41+
42+
// Create a new value which is a pointer to the same type as the source.
43+
var newVal any
44+
if val != nil {
45+
// If it is not nil grab the type with reflect.
46+
newVal = reflect.New(reflect.TypeOf(val)).Interface()
47+
} else {
48+
// else just set a pointer to an any.
49+
var anything any
50+
newVal = &anything
51+
}
52+
n.GetIPFSConfig(key, newVal)
53+
// dereference newVal using reflect to load the resulting value
54+
if !reflect.DeepEqual(val, reflect.ValueOf(newVal).Elem().Interface()) {
4455
log.Panicf("key '%s' did not retain value '%s' after it was set, got '%s'", key, val, newVal)
4556
}
4657
}

0 commit comments

Comments
 (0)