1
0
mirror of https://github.com/minio/mc.git synced 2025-11-14 23:42:27 +03:00

Rename ifFatal and ifError as Fatal, Error

This commit is contained in:
Harshavardhana
2015-08-03 21:46:01 -07:00
parent faf415d08f
commit e6fd52f026
19 changed files with 104 additions and 69 deletions

View File

@@ -64,14 +64,14 @@ func runAccessCmd(ctx *cli.Context) {
config := mustGetMcConfig()
acl := bucketACL(ctx.Args().First())
if !acl.isValidBucketACL() {
ifFatal(probe.New(errInvalidACL{acl: acl.String()}))
Fatal(probe.New(errInvalidACL{acl: acl.String()}))
}
for _, arg := range ctx.Args().Tail() {
targetURL, err := getExpandedURL(arg, config.Aliases)
ifFatal(err)
Fatal(err)
err = doUpdateAccessCmd(targetURL, acl)
ifFatal(err)
Fatal(err)
}
}

View File

@@ -67,8 +67,8 @@ func runCatCmd(ctx *cli.Context) {
// Convert arguments to URLs: expand alias, fix format...
for _, arg := range ctx.Args() {
sourceURL, err := getExpandedURL(arg, config.Aliases)
ifFatal(err)
ifFatal(doCatCmd(sourceURL))
Fatal(err)
Fatal(doCatCmd(sourceURL))
}
}

View File

@@ -67,7 +67,7 @@ func runConfigCmd(ctx *cli.Context) {
console.Fatalf("Incorrect number of arguments, please read \"mc config help\". %s", errInvalidArgument{})
}
msg, err := doConfig(arg, tailArgs)
ifFatal(err)
Fatal(err)
console.Infoln(msg)
}

View File

@@ -66,7 +66,7 @@ func getMcConfigDir() (string, *probe.Error) {
// mustGetMcConfigDir - construct minio client config folder or fail
func mustGetMcConfigDir() (configDir string) {
configDir, err := getMcConfigDir()
ifFatal(err)
Fatal(err)
return configDir
}
@@ -94,7 +94,7 @@ func getMcConfigPath() (string, *probe.Error) {
// mustGetMcConfigPath - similar to getMcConfigPath, ignores errors
func mustGetMcConfigPath() string {
path, err := getMcConfigPath()
ifFatal(err)
Fatal(err)
return path
}
@@ -132,7 +132,7 @@ func getMcConfig() (*configV1, *probe.Error) {
// mustGetMcConfig - reads configuration file and returns configs, exits on error
func mustGetMcConfig() *configV1 {
config, err := getMcConfig()
ifFatal(err)
Fatal(err)
return config
}
@@ -175,9 +175,9 @@ func migrateConfigV1ToV101() {
}
conf := newConfigV1()
config, err := quick.New(conf)
ifFatal(err)
Fatal(err)
err = config.Load(mustGetMcConfigPath())
ifFatal(err)
Fatal(err)
conf = config.Data().(*configV1)
// version is the same return
@@ -206,9 +206,9 @@ func migrateConfigV1ToV101() {
{
newConfig, err := quick.New(conf)
ifFatal(err)
Fatal(err)
err = newConfig.Save(mustGetMcConfigPath())
ifFatal(err)
Fatal(err)
}
console.Infof("Successfully migrated %s from version: %s to version: %s\n", mustGetMcConfigPath(), mcPreviousConfigVersion, mcCurrentConfigVersion)
@@ -286,6 +286,6 @@ func newConfig() (config quick.Config, err *probe.Error) {
// mustNewConfig instantiates a new config handler, exists upon error
func mustNewConfig() quick.Config {
config, err := newConfig()
ifFatal(err)
Fatal(err)
return config
}

View File

@@ -154,7 +154,7 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) {
break
}
if cpURLs.Error != nil {
ifError(cpURLs.Error)
Error(cpURLs.Error)
break
}
@@ -221,7 +221,7 @@ func doCopyCmdSession(session *sessionV2) {
session.Header.LastCopied = cpURLs.SourceContent.Name
} else {
console.Println()
ifError(cpURLs.Error)
Error(cpURLs.Error)
}
case <-trapCh: // Receive interrupt notification.
session.Close()

View File

@@ -108,12 +108,12 @@ func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) {
}
srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
Fatal(err)
if srcContent.Type.IsDir() {
console.Fatalf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{})
}
if !srcContent.Type.IsRegular() {
ifFatal(probe.New(errSourceIsNotFile{URL: srcURL}))
Fatal(probe.New(errSourceIsNotFile{URL: srcURL}))
}
}
@@ -124,19 +124,19 @@ func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) {
}
srcURL := srcURLs[0]
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
Fatal(err)
if srcContent.Type.IsDir() {
console.Fatalf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{})
}
if !srcContent.Type.IsRegular() {
ifFatal(probe.New(errSourceIsNotFile{URL: srcURL}))
Fatal(probe.New(errSourceIsNotFile{URL: srcURL}))
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
Fatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}
@@ -149,16 +149,16 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) {
srcURL := srcURLs[0]
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
Fatal(err)
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
ifFatal(probe.New(errSourceIsNotDir{URL: srcURL}))
Fatal(probe.New(errSourceIsNotDir{URL: srcURL}))
}
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
Fatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}
@@ -169,18 +169,18 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) {
if isURLRecursive(srcURL) {
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
Fatal(err)
if !srcContent.Type.IsDir() { // Ellipses is supported only for folders.
ifFatal(probe.New(errSourceIsNotDir{URL: srcURL}))
Fatal(probe.New(errSourceIsNotDir{URL: srcURL}))
}
} else { // Regular URL.
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
Fatal(err)
if srcContent.Type.IsDir() {
console.Fatalf("Source %s is a folder. Use %s... argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{})
}
if !srcContent.Type.IsRegular() {
ifFatal(probe.New(errSourceIsNotFile{URL: srcURL}))
Fatal(probe.New(errSourceIsNotFile{URL: srcURL}))
}
}
}
@@ -188,7 +188,7 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) {
// Target exist?.
if err == nil {
if !tgtContent.Type.IsDir() {
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
Fatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}

View File

@@ -63,9 +63,9 @@ func runDiffCmd(ctx *cli.Context) {
var err *probe.Error
firstURL, err = getExpandedURL(firstURL, config.Aliases)
ifFatal(err)
Fatal(err)
secondURL, err = getExpandedURL(secondURL, config.Aliases)
ifFatal(err)
Fatal(err)
if isURLRecursive(secondURL) {
console.Fatalf("Second URL cannot be recursive. %s\n", errInvalidArgument{})
}

View File

@@ -1,3 +1,19 @@
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
*
* 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 main
import (
@@ -5,27 +21,41 @@ import (
"github.com/minio/minio/pkg/probe"
)
func ifFatal(err *probe.Error) {
switch err {
case nil:
// nothing to print
default:
if !globalDebugFlag {
console.Fatalln(err.ToError())
// Fatal wrapper function which takes error and selectively prints stack frames if available on debug
func Fatal(err error) {
if err == nil {
return
}
switch e := err.(type) {
case *probe.Error:
if e == nil {
return
}
if !globalDebugFlag {
console.Fatalln(e.ToError())
}
console.Fatalln(err)
default:
console.Fatalln(err)
}
}
func ifError(err *probe.Error) {
switch err {
case nil:
// nothing to print
default:
// Error synonymous with Fatal but doesn't exit on error != nil
func Error(err error) {
if err == nil {
return
}
switch e := err.(type) {
case *probe.Error:
if e == nil {
return
}
if !globalDebugFlag {
console.Errorln(err.ToError())
console.Errorln(e.ToError())
return
}
console.Errorln(err)
default:
console.Errorln(err)
}
}

View File

@@ -111,14 +111,14 @@ func firstTimeRun() {
console.Fatalf("Unable to create mc config folder. %s\n", err)
}
config, err := newConfig()
ifFatal(err)
Fatal(err)
err = writeConfig(config)
ifFatal(err)
Fatal(err)
console.Infoln("Configuration written to [" + mustGetMcConfigPath() + "]. Please update your access credentials.")
}
if !isSessionDirExists() {
err := createSessionDir()
ifFatal(err)
Fatal(err)
}
checkGolangVersion()
}

View File

@@ -83,10 +83,10 @@ func runListCmd(ctx *cli.Context) {
config := mustGetMcConfig()
for _, arg := range args {
targetURL, err := getExpandedURL(arg, config.Aliases)
ifFatal(err)
Fatal(err)
// if recursive strip off the "..."
newTargetURL := stripRecursiveURL(targetURL)
ifFatal(doListCmd(newTargetURL, isURLRecursive(targetURL)))
Fatal(doListCmd(newTargetURL, isURLRecursive(targetURL)))
}
}

4
ls.go
View File

@@ -79,11 +79,11 @@ func doList(clnt client.Client, recursive bool) *probe.Error {
switch contentCh.Err.ToError().(type) {
// handle this specifically for filesystem
case client.ISBrokenSymlink:
ifError(contentCh.Err)
Error(contentCh.Err)
continue
}
if os.IsNotExist(contentCh.Err.ToError()) || os.IsPermission(contentCh.Err.ToError()) {
ifError(contentCh.Err)
Error(contentCh.Err)
continue
}
err = contentCh.Err

View File

@@ -38,7 +38,7 @@ func checkConfig() {
// Ensures config file is sane
{
_, err := getMcConfig()
ifFatal(err)
Fatal(err)
}
}

View File

@@ -60,8 +60,10 @@ func runMakeBucketCmd(ctx *cli.Context) {
config := mustGetMcConfig()
for _, arg := range ctx.Args() {
targetURL, err := getExpandedURL(arg, config.Aliases)
ifFatal(err)
ifFatal(doMakeBucketCmd(targetURL))
Fatal(err)
err = doMakeBucketCmd(targetURL)
Fatal(err)
console.Infoln("Bucket created successfully : " + targetURL)
}
}

View File

@@ -152,7 +152,7 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) {
break
}
if sURLs.Error != nil {
ifError(sURLs.Error)
Error(sURLs.Error)
break
}
if sURLs.isEmpty() {
@@ -221,7 +221,7 @@ func doMirrorCmdSession(session *sessionV2) {
session.Header.LastCopied = sURLs.SourceContent.Name
} else {
console.Println()
ifError(sURLs.Error)
Error(sURLs.Error)
}
case <-trapCh: // Receive interrupt notification.
session.Close()

View File

@@ -57,7 +57,7 @@ func checkMirrorSyntax(ctx *cli.Context) {
// extract URLs.
URLs, err := args2URLs(ctx.Args())
ifFatal(err)
Fatal(err)
srcURL := URLs[0]
tgtURLs := URLs[1:]
@@ -66,7 +66,7 @@ func checkMirrorSyntax(ctx *cli.Context) {
// Source cannot be a folder (except when recursive)
if !isURLRecursive(srcURL) {
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
Fatal(err)
if !srcContent.Type.IsRegular() {
if srcContent.Type.IsDir() {
@@ -93,16 +93,16 @@ func checkMirrorSyntax(ctx *cli.Context) {
}
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
ifFatal(err)
Fatal(err)
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
ifFatal(probe.New(errSourceIsNotDir{URL: srcURL}))
Fatal(probe.New(errSourceIsNotDir{URL: srcURL}))
}
for _, tgtURL := range tgtURLs {
_, content, err := url2Stat(tgtURL)
if err == nil {
if !content.Type.IsDir() {
ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
Fatal(probe.New(errTargetIsNotDir{URL: tgtURL}))
}
}
}

View File

@@ -118,9 +118,12 @@ func (c *s3Client) PutObject(size int64, data io.Reader) *probe.Error {
bucket, object := c.url2BucketAndObject()
err := c.api.PutObject(bucket, object, "application/octet-stream", size, data)
if err != nil {
if minio.ToErrorResponse(err).Code == "MethodNotAllowed" {
errResponse := minio.ToErrorResponse(err)
if errResponse != nil {
if errResponse.Code == "MethodNotAllowed" {
return probe.New(ObjectAlreadyExists{Object: object})
}
}
return probe.New(err)
}
return nil

View File

@@ -85,7 +85,7 @@ func clearSession(sid string) {
if sid == "all" {
for _, sid := range getSessionIDs() {
session, err := loadSessionV2(sid)
ifFatal(err)
Fatal(err)
session.Delete()
}
return
@@ -96,7 +96,7 @@ func clearSession(sid string) {
}
session, err := loadSessionV2(sid)
ifFatal(err)
Fatal(err)
if session != nil {
session.Delete()
}
@@ -119,13 +119,13 @@ func runSessionCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
if !isSessionDirExists() {
ifFatal(createSessionDir())
Fatal(createSessionDir())
}
switch strings.TrimSpace(ctx.Args().First()) {
// list resumable sessions
case "list":
err := listSessions()
ifFatal(err)
Fatal(err)
case "resume":
if len(ctx.Args().Tail()) != 1 {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
@@ -140,7 +140,7 @@ func runSessionCmd(ctx *cli.Context) {
console.Fatalln(errInvalidSessionID{id: sid})
}
s, err := loadSessionV2(sid)
ifFatal(err)
Fatal(err)
// extra check for testing purposes
if s == nil {
return

View File

@@ -104,7 +104,7 @@ func runUpdateCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code
}
msg, err := doUpdateCheck()
ifFatal(err)
Fatal(err)
// no msg do not print one
if msg != "" {
console.Infoln(msg)