each command use its own options to avoid parameter collision

fix https://github.com/chrislusf/seaweedfs/issues/152
This commit is contained in:
chrislusf 2015-06-01 19:25:01 -07:00
parent e09f45f5ed
commit 51aac49e82
3 changed files with 63 additions and 45 deletions

View file

@ -13,15 +13,18 @@ import (
)
var (
downloadReplication *string
downloadDir *string
d DownloadOptions
)
type DownloadOptions struct {
server *string
dir *string
}
func init() {
cmdDownload.Run = runDownload // break init cycle
cmdDownload.IsDebug = cmdDownload.Flag.Bool("debug", false, "verbose debug information")
server = cmdDownload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
downloadDir = cmdDownload.Flag.String("dir", ".", "Download the whole folder recursively if specified.")
d.server = cmdDownload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
d.dir = cmdDownload.Flag.String("dir", ".", "Download the whole folder recursively if specified.")
}
var cmdDownload = &Command{
@ -40,7 +43,7 @@ var cmdDownload = &Command{
func runDownload(cmd *Command, args []string) bool {
for _, fid := range args {
filename, content, e := fetchFileId(*server, fid)
filename, content, e := fetchFileId(*d.server, fid)
if e != nil {
fmt.Println("Fetch Error:", e)
continue
@ -51,7 +54,7 @@ func runDownload(cmd *Command, args []string) bool {
if strings.HasSuffix(filename, "-list") {
filename = filename[0 : len(filename)-len("-list")]
fids := strings.Split(string(content), "\n")
f, err := os.OpenFile(path.Join(*downloadDir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
f, err := os.OpenFile(path.Join(*d.dir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
fmt.Println("File Creation Error:", e)
continue
@ -59,7 +62,7 @@ func runDownload(cmd *Command, args []string) bool {
defer f.Close()
for _, partId := range fids {
var n int
_, part, err := fetchFileId(*server, partId)
_, part, err := fetchFileId(*d.server, partId)
if err == nil {
n, err = f.Write(part)
}
@ -72,7 +75,7 @@ func runDownload(cmd *Command, args []string) bool {
}
}
} else {
ioutil.WriteFile(path.Join(*downloadDir, filename), content, os.ModePerm)
ioutil.WriteFile(path.Join(*d.dir, filename), content, os.ModePerm)
}
}
return true

View file

@ -15,15 +15,21 @@ import (
"github.com/chrislusf/seaweedfs/go/storage"
)
func init() {
cmdExport.Run = runExport // break init cycle
}
const (
defaultFnFormat = `{{.Mime}}/{{.Id}}:{{.Name}}`
timeFormat = "2006-01-02T15:04:05"
)
var (
export ExportOptions
)
type ExportOptions struct {
dir *string
collection *string
volumeId *int
}
var cmdExport = &Command{
UsageLine: "export -dir=/tmp -volumeId=234 -o=/dir/name.tar -fileNameFormat={{.Name}} -newer='" + timeFormat + "'",
Short: "list or export files from one volume data file",
@ -34,13 +40,17 @@ var cmdExport = &Command{
`,
}
var (
exportVolumePath = cmdExport.Flag.String("dir", ".", "input data directory to store volume data files")
exportCollection = cmdExport.Flag.String("collection", "", "the volume collection name")
exportVolumeId = cmdExport.Flag.Int("volumeId", -1, "a volume id. The volume .dat and .idx files should already exist in the dir.")
func init() {
cmdExport.Run = runExport // break init cycle
export.dir = cmdExport.Flag.String("dir", ".", "input data directory to store volume data files")
export.collection = cmdExport.Flag.String("collection", "", "the volume collection name")
export.volumeId = cmdExport.Flag.Int("volumeId", -1, "a volume id. The volume .dat and .idx files should already exist in the dir.")
dest = cmdExport.Flag.String("o", "", "output tar file name, must ends with .tar, or just a \"-\" for stdout")
format = cmdExport.Flag.String("fileNameFormat", defaultFnFormat, "filename format, default to {{.Mime}}/{{.Id}}:{{.Name}}")
newer = cmdExport.Flag.String("newer", "", "export only files newer than this time, default is all files. Must be specified in RFC3339 without timezone")
}
var (
tarFh *tar.Writer
tarHeader tar.Header
fnTmpl *template.Template

View file

@ -11,26 +11,31 @@ import (
)
var (
uploadReplication *string
uploadCollection *string
uploadDir *string
uploadTtl *string
include *string
uploadSecretKey *string
maxMB *int
upload UploadOptions
)
type UploadOptions struct {
server *string
dir *string
include *string
replication *string
collection *string
ttl *string
maxMB *int
secretKey *string
}
func init() {
cmdUpload.Run = runUpload // break init cycle
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
server = cmdUpload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
uploadDir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
include = cmdUpload.Flag.String("include", "", "pattens of files to upload, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
uploadReplication = cmdUpload.Flag.String("replication", "", "replication type")
uploadCollection = cmdUpload.Flag.String("collection", "", "optional collection name")
uploadTtl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
maxMB = cmdUpload.Flag.Int("maxMB", 0, "split files larger than the limit")
uploadSecretKey = cmdUpload.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
upload.server = cmdUpload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
upload.dir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
upload.include = cmdUpload.Flag.String("include", "", "pattens of files to upload, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
upload.replication = cmdUpload.Flag.String("replication", "", "replication type")
upload.collection = cmdUpload.Flag.String("collection", "", "optional collection name")
upload.ttl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
upload.maxMB = cmdUpload.Flag.Int("maxMB", 0, "split files larger than the limit")
upload.secretKey = cmdUpload.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
}
var cmdUpload = &Command{
@ -57,16 +62,16 @@ var cmdUpload = &Command{
}
func runUpload(cmd *Command, args []string) bool {
secret := security.Secret(*uploadSecretKey)
secret := security.Secret(*upload.secretKey)
if len(cmdUpload.Flag.Args()) == 0 {
if *uploadDir == "" {
if *upload.dir == "" {
return false
}
filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
filepath.Walk(*upload.dir, func(path string, info os.FileInfo, err error) error {
if err == nil {
if !info.IsDir() {
if *include != "" {
if ok, _ := filepath.Match(*include, filepath.Base(path)); !ok {
if *upload.include != "" {
if ok, _ := filepath.Match(*upload.include, filepath.Base(path)); !ok {
return nil
}
}
@ -74,9 +79,9 @@ func runUpload(cmd *Command, args []string) bool {
if e != nil {
return e
}
results, e := operation.SubmitFiles(*server, parts,
*uploadReplication, *uploadCollection,
*uploadTtl, *maxMB, secret)
results, e := operation.SubmitFiles(*upload.server, parts,
*upload.replication, *upload.collection,
*upload.ttl, *upload.maxMB, secret)
bytes, _ := json.Marshal(results)
fmt.Println(string(bytes))
if e != nil {
@ -93,9 +98,9 @@ func runUpload(cmd *Command, args []string) bool {
if e != nil {
fmt.Println(e.Error())
}
results, _ := operation.SubmitFiles(*server, parts,
*uploadReplication, *uploadCollection,
*uploadTtl, *maxMB, secret)
results, _ := operation.SubmitFiles(*upload.server, parts,
*upload.replication, *upload.collection,
*upload.ttl, *upload.maxMB, secret)
bytes, _ := json.Marshal(results)
fmt.Println(string(bytes))
}