forked from kahing/goofys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
299 lines (252 loc) · 6.81 KB
/
flags.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2015 Ka-Hing Cheung
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"os"
"strings"
"time"
"github.com/codegangsta/cli"
)
// Set up custom help text for goofys; in particular the usage section.
func init() {
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} {{if .Flags}}[global options]{{end}} bucket[:prefix] mountpoint
{{if .Version}}
VERSION:
{{.Version}}
{{end}}{{if len .Authors}}
AUTHOR(S):
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{end}}{{if .Flags}}
GLOBAL OPTIONS:
{{range .Flags}}{{.}}
{{end}}{{end}}{{if .Copyright }}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
}
func NewApp() (app *cli.App) {
uid, gid := MyUserAndGroup()
app = &cli.App{
Name: "goofys",
Version: "0.0.6",
Usage: "Mount an S3 bucket locally",
HideHelp: true,
Writer: os.Stderr,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "Print this help text and exit successfuly.",
},
/////////////////////////
// File system
/////////////////////////
cli.StringSliceFlag{
Name: "o",
Usage: "Additional system-specific mount options. Be careful!",
},
cli.IntFlag{
Name: "dir-mode",
Value: 0755,
Usage: "Permissions bits for directories. (default: 0755)",
},
cli.IntFlag{
Name: "file-mode",
Value: 0644,
Usage: "Permission bits for files (default: 0644)",
},
cli.IntFlag{
Name: "uid",
Value: uid,
Usage: "UID owner of all inodes.",
},
cli.IntFlag{
Name: "gid",
Value: gid,
Usage: "GID owner of all inodes.",
},
/////////////////////////
// S3
/////////////////////////
cli.StringFlag{
Name: "endpoint",
Value: "",
Usage: "The non-AWS endpoint to connect to." +
" Possible values: http://127.0.0.1:8081/",
},
cli.StringFlag{
Name: "region",
Value: "us-west-2",
Usage: "The non-AWS endpoint to connect to." +
" Possible values: us-east-1, us-west-1, us-west-2, eu-west-1, " +
"eu-central-1, ap-southeast-1, ap-southeast-2, ap-northeast-1, " +
"sa-east-1, cn-north-1",
},
cli.StringFlag{
Name: "storage-class",
Value: "STANDARD",
Usage: "The type of storage to use when writing objects." +
" Possible values: REDUCED_REDUNDANCY, STANDARD, STANDARD_IA.",
},
cli.BoolFlag{
Name: "use-path-request",
Usage: "Use a path-style request instead of virtual host-style." +
" (deprecated, always on)",
},
cli.StringFlag{
Name: "profile",
Usage: "Use a named profile from $HOME/.aws/credentials instead of \"default\"",
},
cli.BoolFlag{
Name: "use-content-type",
Usage: "Set Content-Type according to file extension and /etc/mime.types (default: off)",
},
/////////////////////////
// Tuning
/////////////////////////
cli.DurationFlag{
Name: "stat-cache-ttl",
Value: time.Minute,
Usage: "How long to cache StatObject results and inode attributes.",
},
cli.DurationFlag{
Name: "type-cache-ttl",
Value: time.Minute,
Usage: "How long to cache name -> file/dir mappings in directory " +
"inodes.",
},
/////////////////////////
// Debugging
/////////////////////////
cli.BoolFlag{
Name: "debug_fuse",
Usage: "Enable fuse-related debugging output.",
},
cli.BoolFlag{
Name: "debug_s3",
Usage: "Enable S3-related debugging output.",
},
cli.BoolFlag{
Name: "f",
Usage: "Run goofys in foreground.",
},
},
}
return
}
type FlagStorage struct {
// File system
MountOptions map[string]string
DirMode os.FileMode
FileMode os.FileMode
Uid uint32
Gid uint32
// S3
Endpoint string
Region string
StorageClass string
UsePathRequest bool
Profile string
UseContentType bool
// Tuning
StatCacheTTL time.Duration
TypeCacheTTL time.Duration
// Debugging
DebugFuse bool
DebugS3 bool
Foreground bool
}
func parseOptions(m map[string]string, s string) {
// NOTE(jacobsa): The man pages don't define how escaping works, and as far
// as I can tell there is no way to properly escape or quote a comma in the
// options list for an fstab entry. So put our fingers in our ears and hope
// that nobody needs a comma.
for _, p := range strings.Split(s, ",") {
var name string
var value string
// Split on the first equals sign.
if equalsIndex := strings.IndexByte(p, '='); equalsIndex != -1 {
name = p[:equalsIndex]
value = p[equalsIndex+1:]
} else {
name = p
}
m[name] = value
}
return
}
// Add the flags accepted by run to the supplied flag set, returning the
// variables into which the flags will parse.
func PopulateFlags(c *cli.Context) (flags *FlagStorage) {
flags = &FlagStorage{
// File system
MountOptions: make(map[string]string),
DirMode: os.FileMode(c.Int("dir-mode")),
FileMode: os.FileMode(c.Int("file-mode")),
Uid: uint32(c.Int("uid")),
Gid: uint32(c.Int("gid")),
// Tuning,
StatCacheTTL: c.Duration("stat-cache-ttl"),
TypeCacheTTL: c.Duration("type-cache-ttl"),
// S3
Endpoint: c.String("endpoint"),
Region: c.String("region"),
StorageClass: c.String("storage-class"),
UsePathRequest: c.Bool("use-path-request"),
Profile: c.String("profile"),
UseContentType: c.Bool("use-content-type"),
// Debugging,
DebugFuse: c.Bool("debug_fuse"),
DebugS3: c.Bool("debug_s3"),
Foreground: c.Bool("f"),
}
// Handle the repeated "-o" flag.
for _, o := range c.StringSlice("o") {
parseOptions(flags.MountOptions, o)
}
return
}
func MassageMountFlags(args []string) (ret []string) {
if len(args) == 5 && args[3] == "-o" {
// looks like it's coming from fstab!
mountOptions := ""
ret = append(ret, args[0])
for _, p := range strings.Split(args[4], ",") {
if strings.HasPrefix(p, "-") {
ret = append(ret, p)
} else {
mountOptions += p
mountOptions += ","
}
}
if len(mountOptions) != 0 {
// remove trailing ,
mountOptions = mountOptions[:len(mountOptions)-1]
ret = append(ret, "-o")
ret = append(ret, mountOptions)
}
ret = append(ret, args[1])
ret = append(ret, args[2])
} else {
return args
}
return
}