mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
each command use its own options to avoid parameter collision
fix https://github.com/chrislusf/seaweedfs/issues/152
This commit is contained in:
parent
e09f45f5ed
commit
51aac49e82
|
@ -13,15 +13,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
downloadReplication *string
|
d DownloadOptions
|
||||||
downloadDir *string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type DownloadOptions struct {
|
||||||
|
server *string
|
||||||
|
dir *string
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdDownload.Run = runDownload // break init cycle
|
cmdDownload.Run = runDownload // break init cycle
|
||||||
cmdDownload.IsDebug = cmdDownload.Flag.Bool("debug", false, "verbose debug information")
|
d.server = cmdDownload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
|
||||||
server = cmdDownload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
|
d.dir = cmdDownload.Flag.String("dir", ".", "Download the whole folder recursively if specified.")
|
||||||
downloadDir = cmdDownload.Flag.String("dir", ".", "Download the whole folder recursively if specified.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdDownload = &Command{
|
var cmdDownload = &Command{
|
||||||
|
@ -40,7 +43,7 @@ var cmdDownload = &Command{
|
||||||
|
|
||||||
func runDownload(cmd *Command, args []string) bool {
|
func runDownload(cmd *Command, args []string) bool {
|
||||||
for _, fid := range args {
|
for _, fid := range args {
|
||||||
filename, content, e := fetchFileId(*server, fid)
|
filename, content, e := fetchFileId(*d.server, fid)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
fmt.Println("Fetch Error:", e)
|
fmt.Println("Fetch Error:", e)
|
||||||
continue
|
continue
|
||||||
|
@ -51,7 +54,7 @@ func runDownload(cmd *Command, args []string) bool {
|
||||||
if strings.HasSuffix(filename, "-list") {
|
if strings.HasSuffix(filename, "-list") {
|
||||||
filename = filename[0 : len(filename)-len("-list")]
|
filename = filename[0 : len(filename)-len("-list")]
|
||||||
fids := strings.Split(string(content), "\n")
|
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 {
|
if err != nil {
|
||||||
fmt.Println("File Creation Error:", e)
|
fmt.Println("File Creation Error:", e)
|
||||||
continue
|
continue
|
||||||
|
@ -59,7 +62,7 @@ func runDownload(cmd *Command, args []string) bool {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
for _, partId := range fids {
|
for _, partId := range fids {
|
||||||
var n int
|
var n int
|
||||||
_, part, err := fetchFileId(*server, partId)
|
_, part, err := fetchFileId(*d.server, partId)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
n, err = f.Write(part)
|
n, err = f.Write(part)
|
||||||
}
|
}
|
||||||
|
@ -72,7 +75,7 @@ func runDownload(cmd *Command, args []string) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ioutil.WriteFile(path.Join(*downloadDir, filename), content, os.ModePerm)
|
ioutil.WriteFile(path.Join(*d.dir, filename), content, os.ModePerm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -15,15 +15,21 @@ import (
|
||||||
"github.com/chrislusf/seaweedfs/go/storage"
|
"github.com/chrislusf/seaweedfs/go/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
cmdExport.Run = runExport // break init cycle
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultFnFormat = `{{.Mime}}/{{.Id}}:{{.Name}}`
|
defaultFnFormat = `{{.Mime}}/{{.Id}}:{{.Name}}`
|
||||||
timeFormat = "2006-01-02T15:04:05"
|
timeFormat = "2006-01-02T15:04:05"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
export ExportOptions
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExportOptions struct {
|
||||||
|
dir *string
|
||||||
|
collection *string
|
||||||
|
volumeId *int
|
||||||
|
}
|
||||||
|
|
||||||
var cmdExport = &Command{
|
var cmdExport = &Command{
|
||||||
UsageLine: "export -dir=/tmp -volumeId=234 -o=/dir/name.tar -fileNameFormat={{.Name}} -newer='" + timeFormat + "'",
|
UsageLine: "export -dir=/tmp -volumeId=234 -o=/dir/name.tar -fileNameFormat={{.Name}} -newer='" + timeFormat + "'",
|
||||||
Short: "list or export files from one volume data file",
|
Short: "list or export files from one volume data file",
|
||||||
|
@ -34,13 +40,17 @@ var cmdExport = &Command{
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 (
|
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.")
|
|
||||||
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")
|
|
||||||
tarFh *tar.Writer
|
tarFh *tar.Writer
|
||||||
tarHeader tar.Header
|
tarHeader tar.Header
|
||||||
fnTmpl *template.Template
|
fnTmpl *template.Template
|
||||||
|
|
|
@ -11,26 +11,31 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
uploadReplication *string
|
upload UploadOptions
|
||||||
uploadCollection *string
|
|
||||||
uploadDir *string
|
|
||||||
uploadTtl *string
|
|
||||||
include *string
|
|
||||||
uploadSecretKey *string
|
|
||||||
maxMB *int
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type UploadOptions struct {
|
||||||
|
server *string
|
||||||
|
dir *string
|
||||||
|
include *string
|
||||||
|
replication *string
|
||||||
|
collection *string
|
||||||
|
ttl *string
|
||||||
|
maxMB *int
|
||||||
|
secretKey *string
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdUpload.Run = runUpload // break init cycle
|
cmdUpload.Run = runUpload // break init cycle
|
||||||
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
|
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
|
||||||
server = cmdUpload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
|
upload.server = cmdUpload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
|
||||||
uploadDir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
|
upload.dir = 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")
|
upload.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")
|
upload.replication = cmdUpload.Flag.String("replication", "", "replication type")
|
||||||
uploadCollection = cmdUpload.Flag.String("collection", "", "optional collection name")
|
upload.collection = cmdUpload.Flag.String("collection", "", "optional collection name")
|
||||||
uploadTtl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
|
upload.ttl = 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")
|
upload.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.secretKey = cmdUpload.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdUpload = &Command{
|
var cmdUpload = &Command{
|
||||||
|
@ -57,16 +62,16 @@ var cmdUpload = &Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func runUpload(cmd *Command, args []string) bool {
|
func runUpload(cmd *Command, args []string) bool {
|
||||||
secret := security.Secret(*uploadSecretKey)
|
secret := security.Secret(*upload.secretKey)
|
||||||
if len(cmdUpload.Flag.Args()) == 0 {
|
if len(cmdUpload.Flag.Args()) == 0 {
|
||||||
if *uploadDir == "" {
|
if *upload.dir == "" {
|
||||||
return false
|
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 err == nil {
|
||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
if *include != "" {
|
if *upload.include != "" {
|
||||||
if ok, _ := filepath.Match(*include, filepath.Base(path)); !ok {
|
if ok, _ := filepath.Match(*upload.include, filepath.Base(path)); !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,9 +79,9 @@ func runUpload(cmd *Command, args []string) bool {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
results, e := operation.SubmitFiles(*server, parts,
|
results, e := operation.SubmitFiles(*upload.server, parts,
|
||||||
*uploadReplication, *uploadCollection,
|
*upload.replication, *upload.collection,
|
||||||
*uploadTtl, *maxMB, secret)
|
*upload.ttl, *upload.maxMB, secret)
|
||||||
bytes, _ := json.Marshal(results)
|
bytes, _ := json.Marshal(results)
|
||||||
fmt.Println(string(bytes))
|
fmt.Println(string(bytes))
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
@ -93,9 +98,9 @@ func runUpload(cmd *Command, args []string) bool {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
fmt.Println(e.Error())
|
fmt.Println(e.Error())
|
||||||
}
|
}
|
||||||
results, _ := operation.SubmitFiles(*server, parts,
|
results, _ := operation.SubmitFiles(*upload.server, parts,
|
||||||
*uploadReplication, *uploadCollection,
|
*upload.replication, *upload.collection,
|
||||||
*uploadTtl, *maxMB, secret)
|
*upload.ttl, *upload.maxMB, secret)
|
||||||
bytes, _ := json.Marshal(results)
|
bytes, _ := json.Marshal(results)
|
||||||
fmt.Println(string(bytes))
|
fmt.Println(string(bytes))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue