Code cleanup and readability for mc

This commit is contained in:
Harshavardhana
2015-06-16 14:17:01 -07:00
parent 976ceba27c
commit dbb35c185a
9 changed files with 88 additions and 69 deletions
+4 -2
View File
@@ -43,6 +43,8 @@ func (s *CmdTestSuite) TestCatCmd(c *C) {
var sourceURLs []string
sourceURLs = append(sourceURLs, objectPath)
sourceURLs = append(sourceURLs, objectPathServer)
_, err = doCatCmd(sourceURLs)
c.Assert(err, IsNil)
for _, sourceURL := range sourceURLs {
_, err = doCatCmd(sourceURL)
c.Assert(err, IsNil)
}
}
+16 -9
View File
@@ -77,13 +77,6 @@ func runAccessCmd(ctx *cli.Context) {
Error: iodine.New(err, nil),
})
}
targetURLs, err := getExpandedURLs(ctx.Args(), config.Aliases)
if err != nil {
console.Fatals(ErrorMessage{
Message: "Unknown type of URL ",
Error: iodine.New(err, nil),
})
}
acl := bucketACL(ctx.Args().First())
if !acl.isValidBucketACL() {
console.Fatals(ErrorMessage{
@@ -91,8 +84,22 @@ func runAccessCmd(ctx *cli.Context) {
Error: iodine.New(errInvalidACL{acl: acl.String()}, nil),
})
}
targetURLs = targetURLs[1:] // 1 or more target URLs
for _, targetURL := range targetURLs {
for _, arg := range ctx.Args().Tail() {
targetURL, err := getExpandedURL(arg, config.Aliases)
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url),
Error: iodine.New(e, nil),
})
default:
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg),
Error: iodine.New(err, nil),
})
}
}
errorMsg, err := doUpdateAccessCmd(targetURL, acl)
if err != nil {
console.Errors(ErrorMessage{
+43 -38
View File
@@ -78,49 +78,54 @@ func runCatCmd(ctx *cli.Context) {
Error: iodine.New(err, nil),
})
}
// Convert arguments to URLs: expand alias, fix format...
urls, err := getExpandedURLs(ctx.Args(), config.Aliases)
if err != nil {
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL ‘%s’", urls),
Error: iodine.New(err, nil),
})
}
sourceURLs := urls
humanReadable, err := doCatCmd(sourceURLs)
if err != nil {
console.Fatals(ErrorMessage{
Message: humanReadable,
Error: iodine.New(err, nil),
})
for _, arg := range ctx.Args() {
sourceURL, err := getExpandedURL(arg, config.Aliases)
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url),
Error: iodine.New(e, nil),
})
default:
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg),
Error: iodine.New(err, nil),
})
}
}
errorMsg, err := doCatCmd(sourceURL)
if err != nil {
console.Fatals(ErrorMessage{
Message: errorMsg,
Error: iodine.New(err, nil),
})
}
}
}
func doCatCmd(sourceURLs []string) (string, error) {
for _, url := range sourceURLs {
sourceClnt, err := source2Client(url)
if err != nil {
return "Unable to create client: " + url, iodine.New(err, nil)
}
reader, size, err := sourceClnt.GetObject(0, 0)
if err != nil {
return "Unable to retrieve file: " + url, iodine.New(err, nil)
}
defer reader.Close()
_, err = io.CopyN(os.Stdout, reader, int64(size))
if err != nil {
switch e := iodine.ToError(err).(type) {
case *os.PathError:
if e.Err == syscall.EPIPE {
// stdout closed by the user. Gracefully exit.
return "", nil
}
return "Writing data to stdout failed, unexpected problem.. please report this error", iodine.New(err, nil)
default:
return "Reading data from source failed: " + url, iodine.New(err, nil)
func doCatCmd(sourceURL string) (string, error) {
sourceClnt, err := source2Client(sourceURL)
if err != nil {
return "Unable to create client: " + sourceURL, iodine.New(err, nil)
}
reader, size, err := sourceClnt.GetObject(0, 0)
if err != nil {
return "Unable to retrieve file: " + sourceURL, iodine.New(err, nil)
}
defer reader.Close()
_, err = io.CopyN(os.Stdout, reader, int64(size))
if err != nil {
switch e := iodine.ToError(err).(type) {
case *os.PathError:
if e.Err == syscall.EPIPE {
// stdout closed by the user. Gracefully exit.
return "", nil
}
return "Writing data to stdout failed, unexpected problem.. please report this error", iodine.New(err, nil)
default:
return "Reading data from source failed: " + sourceURL, iodine.New(err, nil)
}
}
return "", nil
-14
View File
@@ -101,20 +101,6 @@ func doCopy(cURLs cpURLs, bar *barSend) error {
return nil
}
// args2URLs extracts source and target URLs from command-line args.
func args2URLs(args cli.Args) ([]string, error) {
config, err := getMcConfig()
if err != nil {
return nil, iodine.New(err, nil)
}
// Convert arguments to URLs: expand alias, fix format...
URLs, err := getExpandedURLs(args, config.Aliases)
if err != nil {
return nil, iodine.New(err, nil)
}
return URLs, nil
}
func doCopyInRoutine(cURLs cpURLs, bar *barSend, cpQueue chan bool, errCh chan error, wg *sync.WaitGroup) {
defer wg.Done()
if err := doCopy(cURLs, bar); err != nil {
+3 -2
View File
@@ -75,7 +75,6 @@ EXAMPLES:
// runListCmd - is a handler for mc ls command
func runListCmd(ctx *cli.Context) {
args := ctx.Args()
if globalAliasFlag {
if !ctx.Args().Present() {
args = []string{"."}
@@ -83,6 +82,7 @@ func runListCmd(ctx *cli.Context) {
} else if !ctx.Args().Present() || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "ls", 1) // last argument is exit code
}
if !isMcConfigExist() {
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
@@ -96,6 +96,7 @@ func runListCmd(ctx *cli.Context) {
Error: iodine.New(err, nil),
})
}
for _, arg := range args {
targetURL, err := getExpandedURL(arg, config.Aliases)
if err != nil {
@@ -124,7 +125,7 @@ func runListCmd(ctx *cli.Context) {
}
}
// doListCmd -
// doListCmd list files on target
func doListCmd(targetURL string, recursive bool) error {
clnt, err := target2Client(targetURL)
if err != nil {
+1
View File
@@ -167,6 +167,7 @@ func runSyncCmd(ctx *cli.Context) {
})
}
// extract URLs.
URLs, err := args2URLs(ctx.Args())
if err != nil {
console.Fatals(ErrorMessage{
+1 -1
View File
@@ -86,7 +86,6 @@ func isTargetURLDir(targetURL string) bool {
return true
}
// SINGLE SOURCE - Type A: copy(f, f) -> copy(f, f)
// guessCopyURLType guesses the type of URL. This approach all allows prepareURL
// functions to accurately report failure causes.
func guessCopyURLType(sourceURLs []string, targetURL string) cpURLsType {
@@ -112,6 +111,7 @@ func guessCopyURLType(sourceURLs []string, targetURL string) cpURLsType {
return cpURLsTypeD
}
// SINGLE SOURCE - Type A: copy(f, f) -> copy(f, f)
// prepareCopyURLsTypeA - prepares target and source URLs for copying.
func prepareCopyURLsTypeA(sourceURL string, targetURL string) <-chan cpURLs {
cpURLsCh := make(chan cpURLs, 10000)
+5 -2
View File
@@ -32,8 +32,8 @@ import (
// A: sync(f, f) -> copy(f, f)
// B: sync(f, d) -> copy(f, d/f) -> A
// C: sync(f, []d) -> []copy(f, d/f) -> []A
// D: sync(d1..., d2) -> []copy(d1/f, d2/d1/f) -> []A
// E: sync(d1..., []d2) -> [][]copy(d1/f, d2/d1/f) -> [][]A
// sync(d1..., d2) -> []copy(d1/f, d2/d1/f) -> []A
// sync(d1..., []d2) -> [][]copy(d1/f, d2/d1/f) -> [][]A
//
// * SINGLE SOURCE - INVALID
// =========================
@@ -74,6 +74,7 @@ func guessSyncURLType(sourceURL string, targetURLs []string) syncURLsType {
return syncURLsType(cpURLsTypeA)
}
// prepareSyncURLsTypeA - A: sync(f, f) -> copy(f, f)
func prepareSyncURLsTypeA(sourceURL string, targetURLs []string) <-chan syncURLs {
syncURLsCh := make(chan syncURLs, 10000)
go func() {
@@ -95,6 +96,7 @@ func prepareSyncURLsTypeA(sourceURL string, targetURLs []string) <-chan syncURLs
return syncURLsCh
}
// prepareSyncURLsTypeB - B: sync(f, d) -> copy(f, d/f) -> A
func prepareSyncURLsTypeB(sourceURL string, targetURLs []string) <-chan syncURLs {
syncURLsCh := make(chan syncURLs, 10000)
go func() {
@@ -116,6 +118,7 @@ func prepareSyncURLsTypeB(sourceURL string, targetURLs []string) <-chan syncURLs
return syncURLsCh
}
// prepareSyncURLsTypeC - C: sync(f, []d) -> []copy(f, d/f) -> []A
func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs {
syncURLsCh := make(chan syncURLs, 10000)
go func() {
+15 -1
View File
@@ -23,7 +23,7 @@ import (
"github.com/minio/minio/pkg/iodine"
)
// re-usable constants
// ``...`` recursiveSeparator
const (
recursiveSeparator = "..."
)
@@ -74,3 +74,17 @@ func getExpandedURLs(args []string, aliases map[string]string) (urls []string, e
}
return urls, nil
}
// args2URLs extracts source and target URLs from command-line args.
func args2URLs(args []string) ([]string, error) {
config, err := getMcConfig()
if err != nil {
return nil, iodine.New(err, nil)
}
// Convert arguments to URLs: expand alias, fix format...
URLs, err := getExpandedURLs(args, config.Aliases)
if err != nil {
return nil, iodine.New(err, nil)
}
return URLs, nil
}