Skip to content

Commit a3de68e

Browse files
committed
cmd: allow specifiyng a ipfs-url instead of a ipfs-path (--ipfs-path-or-url)
1 parent 39e9d60 commit a3de68e

File tree

20 files changed

+110
-123
lines changed

20 files changed

+110
-123
lines changed

backend/backend.go

-12
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ type Backend interface {
3535
eventsBackend.Backend
3636
}
3737

38-
// InitByName creates a new backend structure at `path` for the backend `name`
39-
func InitByName(name, path string, port int) error {
40-
switch name {
41-
case "httpipfs":
42-
return httpipfs.Init(path)
43-
case "mock":
44-
return nil
45-
}
46-
47-
return ErrNoSuchBackend
48-
}
49-
5038
// ForwardLogByName will forward the logs of the backend `name` to `w`.
5139
func ForwardLogByName(name string, w io.Writer) error {
5240
switch name {

backend/httpipfs/init.go

-8
This file was deleted.

backend/httpipfs/shell.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"os"
8+
"path/filepath"
79
"sync"
810
"time"
911

1012
"github.com/blang/semver"
1113
shell "github.com/ipfs/go-ipfs-api"
14+
ma "github.com/multiformats/go-multiaddr"
1215
"github.com/patrickmn/go-cache"
1316
"github.com/sahib/brig/repo/setup"
1417
log "github.com/sirupsen/logrus"
@@ -74,8 +77,26 @@ func WithNoLogging() Option {
7477
}
7578
}
7679

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+
7798
// 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) {
79100
nd := &Node{
80101
allowNetOps: true,
81102
fingerprint: fingerprint,
@@ -88,16 +109,16 @@ func NewNode(ipfsPath, fingerprint string, opts ...Option) (*Node, error) {
88109
opt(nd)
89110
}
90111

91-
addr, err := setup.GetAPIAddrForPath(ipfsPath)
112+
m, err := toMultiAddr(ipfsPathOrURL)
92113
if err != nil {
93114
return nil, err
94115
}
95116

96117
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())
98119
}
99120

100-
nd.sh = shell.NewShell(addr)
121+
nd.sh = shell.NewShell(m.String())
101122

102123
versionString, _, err := nd.sh.Version()
103124
if err != nil && !nd.quiet {

bench/bench.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,10 @@ func newFuseWriteOrReadBench(ipfsPath string, isWrite bool) (Bench, error) {
381381
unixSocket := "unix:" + filepath.Join(tmpDir, "socket")
382382

383383
proc, err := fusetest.LaunchAsProcess(fusetest.Options{
384-
MountPath: filepath.Join(tmpDir, "mount"),
385-
CatfsPath: filepath.Join(tmpDir, "catfs"),
386-
IpfsPath: ipfsPath,
387-
URL: unixSocket,
384+
MountPath: filepath.Join(tmpDir, "mount"),
385+
CatfsPath: filepath.Join(tmpDir, "catfs"),
386+
IpfsPathOrURL: ipfsPath,
387+
URL: unixSocket,
388388
})
389389

390390
if err != nil {

client/clienttest/daemon.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ func StartDaemon(name, backendName, ipfsPath string) (*server.Server, error) {
3434
}
3535

3636
if backendName == "httpipfs" {
37-
if err := repo.OverwriteConfigKey(repoPath, "daemon.ipfs_path", ipfsPath); err != nil {
37+
if err := repo.OverwriteConfigKey(
38+
repoPath,
39+
"daemon.ipfs_path_or_url",
40+
ipfsPath,
41+
); err != nil {
3842
return nil, err
3943
}
4044
}

client/fs_cmds.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,13 @@ func (cl *Client) CatOnClient(path string, offline bool, w io.Writer) error {
246246
return err
247247
}
248248

249-
ipfsPath, err := cl.ConfigGet("daemon.ipfs_path")
249+
ipfsPathOrURL, err := cl.ConfigGet("daemon.ipfs_path_or_url")
250250
if err != nil {
251251
return err
252252
}
253253

254-
if ipfsPath == "" {
255-
return fmt.Errorf("no ipfs-path found - is this repo using IPFS?")
254+
if ipfsPathOrURL == "" {
255+
return fmt.Errorf("no IPFS path or URL found - is this repo using IPFS?")
256256
}
257257

258258
if offline {
@@ -267,7 +267,7 @@ func (cl *Client) CatOnClient(path string, offline bool, w io.Writer) error {
267267
}
268268

269269
nd, err := httpipfs.NewNode(
270-
ipfsPath,
270+
ipfsPathOrURL,
271271
"",
272272
httpipfs.WithNoLogging(),
273273
)

cmd/debug.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func handleDebugFuseMock(ctx *cli.Context) error {
137137
opts := fusetest.Options{
138138
CatfsPath: ctx.String("catfs-path"),
139139
MountPath: ctx.String("mount-path"),
140-
IpfsPath: ctx.String("ipfs-path"),
140+
IpfsPathOrURL: ctx.String("ipfs-path-or-url"),
141141
URL: ctx.String("url"),
142142
MountReadOnly: ctx.Bool("mount-ro"),
143143
MountOffline: ctx.Bool("mount-offline"),

cmd/help.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ var helpTexts = map[string]helpEntry{
8282
Usage: "Do not display the super pretty logo on init.",
8383
},
8484
cli.StringFlag{
85-
Name: "ipfs-path,P",
86-
Usage: "Specify an explicit path to an IPFS repository. Useful if you have more than one.",
85+
Name: "ipfs-path-or-url",
86+
Usage: "Specify an explicit path or URL to an IPFS repository. Useful if you have more than one.",
8787
Value: "",
8888
},
8989
cli.BoolFlag{
@@ -1535,7 +1535,7 @@ EXAMPLES:
15351535
Value: "unix:/tmp/fuse-mock.socket",
15361536
},
15371537
cli.StringFlag{
1538-
Name: "ipfs-path,i",
1538+
Name: "ipfs-path-or-url,i",
15391539
Usage: "Path to IPFS, if you want to use it. Empty for memory only.",
15401540
Value: "",
15411541
},

cmd/init.go

+7-48
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,22 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"path/filepath"
6-
"strconv"
7-
"strings"
8-
94
e "github.com/pkg/errors"
10-
"github.com/sahib/brig/backend"
115
"github.com/sahib/brig/repo"
12-
"github.com/sahib/brig/repo/setup"
136
"github.com/urfave/cli"
147
)
158

169
// Init creates a new brig repository at `basePath` with specified options.
17-
func Init(ctx *cli.Context, ipfsPath string, opts repo.InitOptions) error {
10+
func Init(ctx *cli.Context, ipfsPathOrURL string, opts repo.InitOptions) error {
1811
if err := repo.Init(opts); err != nil {
1912
return e.Wrapf(err, "repo-init")
2013
}
2114

22-
apiAddr, err := setup.GetAPIAddrForPath(ipfsPath)
23-
if err != nil {
24-
return e.Wrapf(err, "no config - is »%s« an IPFS repo?", apiAddr)
25-
}
26-
27-
splitAPIAddr := strings.Split(string(apiAddr), "/")
28-
if len(splitAPIAddr) == 0 {
29-
return fmt.Errorf(
30-
"failed to read IPFS api port to connect to (at %s): %v",
31-
ipfsPath,
32-
err,
33-
)
34-
}
35-
36-
ipfsPort, err := strconv.Atoi(splitAPIAddr[len(splitAPIAddr)-1])
37-
if err != nil {
38-
return fmt.Errorf(
39-
"failed to convert api port to string (at %s): %v",
40-
ipfsPath,
41-
err,
42-
)
43-
}
44-
45-
if err := repo.OverwriteConfigKey(
15+
// Remember the ipsf connection details,
16+
// so we can start it later.
17+
return repo.OverwriteConfigKey(
4618
opts.BaseFolder,
47-
"daemon.ipfs_path",
48-
ipfsPath,
49-
); err != nil {
50-
return err
51-
}
52-
53-
backendPath := filepath.Join(opts.BaseFolder, "data", opts.BackendName)
54-
if err := backend.InitByName(
55-
opts.BackendName,
56-
backendPath,
57-
ipfsPort,
58-
); err != nil {
59-
return e.Wrapf(err, "backend-init")
60-
}
61-
62-
return nil
19+
"daemon.ipfs_path_or_url",
20+
ipfsPathOrURL,
21+
)
6322
}

cmd/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func RunCmdline(args []string) int {
140140
app.Flags = []cli.Flag{
141141
cli.StringFlag{
142142
Name: "url,u",
143-
Usage: "URL on where to reach the brig daemon. Leave empty to allow guessing.",
143+
Usage: "URL on where to reach the brig daemon.",
144144
EnvVar: "BRIG_URL",
145145
Value: defaults.DaemonDefaultURL(),
146146
},

cmd/repo_handlers.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ Have a nice day.
101101
return ctl.MakeCommit("added initial README.md")
102102
}
103103

104+
func isMultiAddr(ipfsPathOrURL string) bool {
105+
_, err := os.Stat(ipfsPathOrURL)
106+
return err != nil
107+
}
108+
104109
func handleInit(ctx *cli.Context) error {
105110
if len(ctx.Args()) == 0 {
106111
return fmt.Errorf("Please specify a name for the owner of this repository")
@@ -146,18 +151,28 @@ func handleInit(ctx *cli.Context) error {
146151
return fmt.Errorf("`%s` already exists and is not empty; refusing to do init", folder)
147152
}
148153

149-
ipfsPath := ctx.String("ipfs-path")
154+
ipfsPathOrURL := ctx.String("ipfs-path-or-url")
150155
doIpfsSetup := !ctx.Bool("no-ipfs-setup")
151156
doIpfsConfig := !ctx.Bool("no-ipfs-config")
152157
doExtraIpfsConfig := !ctx.Bool("no-ipfs-optimization")
153158

159+
ipfsRepoPath := ipfsPathOrURL
160+
isMa := isMultiAddr(ipfsPathOrURL)
161+
if isMa {
162+
// NOTE: If we're connecting over a multiaddr,
163+
// then we should not setup an ipfs repo.
164+
// Assumption is that it exists already.
165+
doIpfsSetup = false
166+
ipfsRepoPath = ""
167+
}
168+
154169
if backend == "httpipfs" {
155170
if _, err := setup.IPFS(setup.Options{
156171
LogWriter: os.Stdout,
157172
Setup: doIpfsSetup,
158173
SetDefaultConfig: doIpfsConfig,
159174
SetExtraConfig: doExtraIpfsConfig,
160-
IpfsPath: ipfsPath,
175+
IpfsPath: ipfsRepoPath,
161176
}); err != nil {
162177
return err
163178
}
@@ -170,7 +185,7 @@ func handleInit(ctx *cli.Context) error {
170185

171186
if err := Init(
172187
ctx,
173-
ipfsPath,
188+
ipfsPathOrURL,
174189
repo.InitOptions{
175190
BaseFolder: folder,
176191
Owner: owner,
@@ -401,7 +416,7 @@ func handleDaemonLaunch(ctx *cli.Context) error {
401416
if err != nil {
402417
log.Warningf("failed to read config at %v: %v", repoPath, err)
403418
} else {
404-
ipfsPath = cfg.String("daemon.ipfs_path")
419+
ipfsPath = cfg.String("daemon.ipfs_path_or_url")
405420
}
406421

407422
if _, err := setup.IPFS(setup.Options{

cmd/util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func guessRepoFolder(ctx *cli.Context) (string, error) {
8585
var lastError error
8686
for _, guessLocation := range guessLocations {
8787
repoFolder := mustAbsPath(guessLocation)
88-
if _, err := os.Stat(filepath.Join(repoFolder, "OWNER")); err != nil {
88+
if _, err := os.Stat(filepath.Join(repoFolder, "config.yml")); err != nil {
8989
lastError = err
9090
continue
9191
}
@@ -242,7 +242,7 @@ func startDaemon(ctx *cli.Context, repoPath, daemonURL string) (*client.Client,
242242
return ctl, nil
243243
}
244244

245-
return nil, fmt.Errorf("Daemon could not be started or took to long. Wrong password maybe?")
245+
return nil, fmt.Errorf("Daemon could not be started or took to long.")
246246
}
247247

248248
func isDaemonRunning(ctx *cli.Context) (bool, error) {

defaults/defaults_v0.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ var DefaultsV0 = config.DefaultMapping{
4444
Docs: "URL of the daemon process.",
4545
Validator: urlValidator,
4646
},
47-
"ipfs_path": config.DefaultEntry{
47+
"ipfs_path_or_url": config.DefaultEntry{
4848
Default: "",
4949
NeedsRestart: true,
50-
Docs: "Path to the IPFS repository you want to use.",
50+
Docs: "URL or path to the IPFS repository you want to use.",
5151
},
5252
"enable_pprof": config.DefaultEntry{
5353
Default: true,

fuse/fusetest/helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func LaunchAsProcess(opts Options) (*os.Process, error) {
2727
"fusemock",
2828
"--mount-path", opts.MountPath,
2929
"--catfs-path", opts.CatfsPath,
30-
"--ipfs-path", opts.IpfsPath,
30+
"--ipfs-path-or-url", opts.IpfsPathOrURL,
3131
"--url", opts.URL,
3232
}
3333

fuse/fusetest/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ type Options struct {
122122

123123
// IpfsPath tells us which IPFS repo to use.
124124
// If empty, use the mock backend.
125-
IpfsPath string
125+
IpfsPathOrURL string
126126

127127
// URL defines where the server can be reached.
128128
URL string
@@ -150,8 +150,8 @@ func Launch(opts Options) error {
150150
}
151151

152152
var backend catfs.FsBackend
153-
if opts.IpfsPath != "" {
154-
backend, err = httpipfs.NewNode(opts.IpfsPath, "")
153+
if opts.IpfsPathOrURL != "" {
154+
backend, err = httpipfs.NewNode(opts.IpfsPathOrURL, "")
155155
} else {
156156
backend = catfs.NewMemFsBackend()
157157
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ require (
3636
github.com/mattn/go-isatty v0.0.12
3737
github.com/mitchellh/go-homedir v1.1.0
3838
github.com/mr-tron/base58 v1.2.0
39-
github.com/multiformats/go-multiaddr v0.3.1 // indirect
39+
github.com/multiformats/go-multiaddr v0.3.1
4040
github.com/multiformats/go-multihash v0.0.14
4141
github.com/nbutton23/zxcvbn-go v0.0.0-20201221231540-e56b841a3c88
4242
github.com/patrickmn/go-cache v2.1.0+incompatible

0 commit comments

Comments
 (0)