@@ -4,11 +4,14 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"errors"
7
+ "os"
8
+ "path/filepath"
7
9
"sync"
8
10
"time"
9
11
10
12
"github.com/blang/semver"
11
13
shell "github.com/ipfs/go-ipfs-api"
14
+ ma "github.com/multiformats/go-multiaddr"
12
15
"github.com/patrickmn/go-cache"
13
16
"github.com/sahib/brig/repo/setup"
14
17
log "github.com/sirupsen/logrus"
@@ -74,8 +77,26 @@ func WithNoLogging() Option {
74
77
}
75
78
}
76
79
80
+ func toMultiAddr (ipfsPathOrURL string ) (ma.Multiaddr , error ) {
81
+ if ! filepath .IsAbs (ipfsPathOrURL ) {
82
+ // multiaddr always start with a slash,
83
+ // this branch affects only file paths.
84
+ var err error
85
+ ipfsPathOrURL , err = filepath .Abs (ipfsPathOrURL )
86
+ if err != nil {
87
+ return nil , err
88
+ }
89
+ }
90
+
91
+ if _ , err := os .Stat (ipfsPathOrURL ); err == nil {
92
+ return setup .GetAPIAddrForPath (ipfsPathOrURL )
93
+ }
94
+
95
+ return ma .NewMultiaddr (ipfsPathOrURL )
96
+ }
97
+
77
98
// NewNode returns a new http based IPFS backend.
78
- func NewNode (ipfsPath , fingerprint string , opts ... Option ) (* Node , error ) {
99
+ func NewNode (ipfsPathOrURL string , fingerprint string , opts ... Option ) (* Node , error ) {
79
100
nd := & Node {
80
101
allowNetOps : true ,
81
102
fingerprint : fingerprint ,
@@ -88,16 +109,16 @@ func NewNode(ipfsPath, fingerprint string, opts ...Option) (*Node, error) {
88
109
opt (nd )
89
110
}
90
111
91
- addr , err := setup . GetAPIAddrForPath ( ipfsPath )
112
+ m , err := toMultiAddr ( ipfsPathOrURL )
92
113
if err != nil {
93
114
return nil , err
94
115
}
95
116
96
117
if ! nd .quiet {
97
- log .Infof ("Connecting to IPFS HTTP API at %s" , addr )
118
+ log .Infof ("Connecting to IPFS HTTP API at %s" , m . String () )
98
119
}
99
120
100
- nd .sh = shell .NewShell (addr )
121
+ nd .sh = shell .NewShell (m . String () )
101
122
102
123
versionString , _ , err := nd .sh .Version ()
103
124
if err != nil && ! nd .quiet {
0 commit comments