This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
144 lines (113 loc) · 2.45 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/MatthiasWinkelmann/redis-fs/redisfs"
"github.com/codegangsta/cli"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
)
var App *cli.App
// app name
var Name = "redis-fs"
// app version
var Version = "0.2.1"
// redis host name
var HostFlag = cli.StringFlag{
Name: "host",
Value: "localhost",
Usage: "set redis host name",
}
// redis port number
var PortFlag = cli.IntFlag{
Name: "port",
Value: 6379,
Usage: "set redis port number",
}
// redis database number
var DbFlag = cli.IntFlag{
Name: "db",
Value: 0,
Usage: "set redis database",
}
// redis password
var AuthFlag = cli.StringFlag{
Name: "auth",
Usage: "set redis password",
}
// redis key separator
var SepFlag = cli.StringFlag{
Name: "sep",
Value: ":",
Usage: "set redis key separator",
}
// fuse options
var AllowOther = cli.BoolFlag{
Name: "allow-other",
Usage: "allow other users to access the mount point",
}
// help message template
var AppHelpTemplate = "" +
"\n" +
" \u001b[36m{{.Name}}\u001b[39m \u001b[33m{{.Version}}\u001b[39m\n" +
" $ mkdir /tmp/redis && {{.Name}} /tmp/redis\n" +
"\n" +
" {{range .Flags}}{{.}}\n" +
" {{end}}\n"
func main() {
cli.AppHelpTemplate = AppHelpTemplate
App = cli.NewApp()
App.Name = Name
App.Version = Version
App.Flags = []cli.Flag{
HostFlag,
PortFlag,
DbFlag,
AuthFlag,
SepFlag,
AllowOther,
}
App.Action = run
App.Run(os.Args)
}
func run(ctx *cli.Context) {
if len(ctx.Args()) == 0 {
cli.ShowAppHelp(ctx)
return
}
server, err := mount(ctx)
if err != nil {
fmt.Printf("\n \u001b[35m%s\u001b[39m: %s\n\n", "Error", err)
return
}
server.Serve()
}
func mount(ctx *cli.Context) (*fuse.Server, error) {
mnt, err := filepath.Abs(ctx.Args().Get(0))
if err != nil {
return nil, err
}
fs := &redisfs.RedisFs{
FileSystem: pathfs.NewDefaultFileSystem(),
Host: ctx.String("host"),
Port: ctx.Int("port"),
Db: ctx.Int("db"),
Auth: ctx.String("auth"),
Dirs: make(map[string][]string),
Sep: ctx.String("sep"),
}
fs.Init()
mountOpts := fuse.MountOptions{
AllowOther: ctx.Bool("allow-other"),
EnableLocks: false,
}
nfs := pathfs.NewPathNodeFs(fs, nil)
conn := nodefs.NewFileSystemConnector(nfs.Root(), nil)
server, err := fuse.NewServer(conn.RawFS(), mnt, &mountOpts)
if err != nil {
return nil, err
}
return server, nil
}