forked from vdemeester/docker-volume-ipfs
/
main.go
89 lines (74 loc) · 1.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"github.com/docker/go-plugins-helpers/volume"
)
const ipfsID = "_ipfs"
var (
defaultDir = filepath.Join(volume.DefaultDockerRootDirectory, ipfsID)
// ipfs fuse mountpoint
ipfsMountPoint = flag.String("mount", "/ipfs", "ipfs mount point")
)
func main() {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, os.Kill)
// var Usage = func() {
// fmt.Fprintf(os.Stderr, "Usage %s [options]\n", os.Args[0])
// flag.PrintDefaults()
// }
flag.Parse()
_, err := os.Lstat(*ipfsMountPoint)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n%s does not exists, can't start..\n Please use ipfs command line to mount it\n", err, *ipfsMountPoint)
os.Exit(1)
}
d := newIPFSDriver(*ipfsMountPoint)
h := volume.NewHandler(d)
go func() {
if err := h.ServeUnix("root", "ipfs"); err != nil {
fmt.Println(err)
}
}()
cmd := startIPFSDaemon()
cmd.Wait()
}
func startIPFSDaemon() *exec.Cmd {
cmd := exec.Command("ipfs", "daemon", "--mount")
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for IPFS", err)
os.Exit(1)
}
scannerOut := bufio.NewScanner(stdout)
go func() {
for scannerOut.Scan() {
fmt.Printf("IPFS > %s\n", scannerOut.Text())
}
}()
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StderrPipe for IPFS", err)
os.Exit(1)
}
scannerErr := bufio.NewScanner(stderr)
go func() {
for scannerErr.Scan() {
fmt.Printf("IPFS > %s\n", scannerErr.Text())
}
}()
err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting IPFS", err)
os.Exit(1)
}
return cmd
}