1
0
mirror of https://github.com/minio/mc.git synced 2025-11-12 01:02:26 +03:00

Implement console.Fatals, Errors, Infos, Prints for structure input and printing

This commit is contained in:
Harshavardhana
2015-05-26 17:56:36 -07:00
parent 3e24f7ae21
commit 25f847b3b6
11 changed files with 156 additions and 85 deletions

View File

@@ -32,14 +32,14 @@ func runAccessCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})
}
config, err := getMcConfig()
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "loading config file failed",
Error: iodine.New(err, nil),
})
@@ -47,14 +47,14 @@ func runAccessCmd(ctx *cli.Context) {
targetURLConfigMap := make(map[string]*hostConfig)
targetURLs, err := getExpandedURLs(ctx.Args(), config.Aliases)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Unknown type of URL ",
Error: iodine.New(err, nil),
})
}
acl := bucketACL(ctx.Args().First())
if !acl.isValidBucketACL() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Valid types are [private, public, readonly].",
Error: iodine.New(errors.New("Invalid ACL Type "+acl.String()+""), nil),
})
@@ -63,7 +63,7 @@ func runAccessCmd(ctx *cli.Context) {
for _, targetURL := range targetURLs {
targetConfig, err := getHostConfig(targetURL)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Unable to read configuration for host " + "" + targetURL + "",
Error: iodine.New(err, nil),
})
@@ -73,7 +73,7 @@ func runAccessCmd(ctx *cli.Context) {
for targetURL, targetConfig := range targetURLConfigMap {
errorMsg, err := doUpdateAccessCmd(targetURL, acl.String(), targetConfig)
if err != nil {
console.Errorln(ErrorMessage{
console.Errors(ErrorMessage{
Message: errorMsg,
Error: iodine.New(err, nil),
})

View File

@@ -32,14 +32,14 @@ func runCatCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})
}
config, err := getMcConfig()
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read config file %s", mustGetMcConfigPath()),
Error: iodine.New(err, nil),
})
@@ -48,7 +48,7 @@ func runCatCmd(ctx *cli.Context) {
// Convert arguments to URLs: expand alias, fix format...
urls, err := getExpandedURLs(ctx.Args(), config.Aliases)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL %s", urls),
Error: iodine.New(err, nil),
})
@@ -57,14 +57,14 @@ func runCatCmd(ctx *cli.Context) {
sourceURLs := urls
sourceURLConfigMap, err := getHostConfigs(sourceURLs)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read host configuration for %s from config file %s", sourceURLs, mustGetMcConfigPath()),
Error: iodine.New(err, nil),
})
}
humanReadable, err := doCatCmd(sourceURLConfigMap)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: humanReadable,
Error: iodine.New(err, nil),
})

View File

@@ -34,14 +34,14 @@ func runConfigCmd(ctx *cli.Context) {
arg := ctx.Args().First()
tailArgs := ctx.Args().Tail()
if len(tailArgs) > 2 {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Incorrect number of arguments, please use \"mc config help\"",
Error: iodine.New(errInvalidArgument{}, nil),
})
}
msg, err := doConfig(arg, tailArgs)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: msg,
Error: err,
})

View File

@@ -125,7 +125,7 @@ func runCopyCmd(ctx *cli.Context) {
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})
@@ -134,7 +134,7 @@ func runCopyCmd(ctx *cli.Context) {
// extract URLs.
URLs, err := args2URLs(ctx.Args())
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown URL types: %s", URLs),
Error: iodine.New(err, nil),
})
@@ -153,7 +153,7 @@ func runCopyCmd(ctx *cli.Context) {
for err := range doCopyCmd(sourceURLs, targetURL, bar) {
if err != nil {
console.Errorln(ErrorMessage{
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
})

View File

@@ -31,14 +31,14 @@ func runDiffCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})
}
config, err := getMcConfig()
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read config file %s", mustGetMcConfigPath()),
Error: err,
})
@@ -51,12 +51,12 @@ func runDiffCmd(ctx *cli.Context) {
if err != nil {
switch iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL %s", firstURL),
Error: err,
})
default:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to parse argument %s", firstURL),
Error: err,
})
@@ -65,7 +65,7 @@ func runDiffCmd(ctx *cli.Context) {
_, err = getHostConfig(firstURL)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read host configuration for %s from config file %s", firstURL, mustGetMcConfigPath()),
Error: err,
})
@@ -73,7 +73,7 @@ func runDiffCmd(ctx *cli.Context) {
_, err = getHostConfig(secondURL)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read host configuration for %s from config file %s", secondURL, mustGetMcConfigPath()),
Error: err,
})
@@ -83,12 +83,12 @@ func runDiffCmd(ctx *cli.Context) {
if err != nil {
switch iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL %s", secondURL),
Error: err,
})
default:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to parse argument %s", secondURL),
Error: err,
})
@@ -98,7 +98,7 @@ func runDiffCmd(ctx *cli.Context) {
newFirstURL := stripRecursiveURL(firstURL)
for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) {
if diff.err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: diff.message,
Error: diff.err,
})

View File

@@ -31,14 +31,14 @@ func runListCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "ls", 1) // last argument is exit code
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})
}
config, err := getMcConfig()
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read config file %s", mustGetMcConfigPath()),
Error: iodine.New(err, nil),
})
@@ -49,12 +49,12 @@ func runListCmd(ctx *cli.Context) {
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL %s", e.url),
Error: iodine.New(e, nil),
})
default:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to parse argument %s", arg),
Error: iodine.New(err, nil),
})
@@ -62,7 +62,7 @@ func runListCmd(ctx *cli.Context) {
}
targetConfig, err := getHostConfig(targetURL)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read host configuration for %s from config file %s", targetURL, mustGetMcConfigPath()),
Error: iodine.New(err, nil),
})
@@ -74,7 +74,7 @@ func runListCmd(ctx *cli.Context) {
newTargetURL := stripRecursiveURL(targetURL)
err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL))
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Failed to list %s", targetURL),
Error: iodine.New(err, nil),
})

View File

@@ -33,14 +33,14 @@ func runMakeBucketCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "mb", 1) // last argument is exit code
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: errors.New("\"mc\" is not configured"),
})
}
config, err := getMcConfig()
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Unable to read config file " + mustGetMcConfigPath() + "",
Error: err,
})
@@ -51,12 +51,12 @@ func runMakeBucketCmd(ctx *cli.Context) {
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown type of URL %s", e.url),
Error: e,
})
default:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to parse argument %s", arg),
Error: err,
})
@@ -64,7 +64,7 @@ func runMakeBucketCmd(ctx *cli.Context) {
}
targetConfig, err := getHostConfig(targetURL)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read host configuration for %s from config file %s", targetURL, mustGetMcConfigPath()),
Error: err,
})
@@ -74,7 +74,7 @@ func runMakeBucketCmd(ctx *cli.Context) {
for targetURL, targetConfig := range targetURLConfigMap {
errorMsg, err := doMakeBucketCmd(targetURL, targetConfig)
if err != nil {
console.Errorln(ErrorMessage{
console.Errors(ErrorMessage{
Message: errorMsg,
Error: err,
})

View File

@@ -33,7 +33,7 @@ func runSyncCmd(ctx *cli.Context) {
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})
@@ -41,7 +41,7 @@ func runSyncCmd(ctx *cli.Context) {
URLs, err := args2URLs(ctx.Args())
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown URL types found: %s", URLs),
Error: iodine.New(err, nil),
})
@@ -76,7 +76,7 @@ func runSyncCmd(ctx *cli.Context) {
for syncURLs := range prepareSyncURLs(sourceURL, targetURLs) {
if syncURLs.Error != nil {
console.Errorln(ErrorMessage{
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(syncURLs.Error, nil),
})
@@ -88,7 +88,7 @@ func runSyncCmd(ctx *cli.Context) {
defer wg.Done()
srcConfig, err := getHostConfig(syncURLs.SourceContent.Name)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
})
@@ -96,14 +96,14 @@ func runSyncCmd(ctx *cli.Context) {
}
tgtConfig, err := getHostConfig(syncURLs.TargetContent.Name)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
})
return
}
if err := doCopy(syncURLs.SourceContent.Name, srcConfig, syncURLs.TargetContent.Name, tgtConfig, bar); err != nil {
console.Errorln(ErrorMessage{
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
})

View File

@@ -28,7 +28,7 @@ import (
)
const (
mcUpdateURL = "http://dl.minio.io:9000/updates/2015/Apr" + "/mc" + "." + runtime.GOOS + "." + runtime.GOARCH
mcUpdateURL = "http://dl.minio.io:9000/updates/2015/June/" + "mc" + "." + runtime.GOOS + "." + runtime.GOARCH
)
func doUpdateCheck(config *hostConfig) (string, error) {
@@ -38,7 +38,7 @@ func doUpdateCheck(config *hostConfig) (string, error) {
}
latest, err := clnt.Stat()
if err != nil {
return "No update available at this time", nil
return "No new update available at this time", nil
}
current, _ := time.Parse(time.RFC3339Nano, BuildDate)
if current.IsZero() {
@@ -58,21 +58,21 @@ func runUpdateCmd(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code
}
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})
}
hostConfig, err := getHostConfig(mcUpdateURL)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read configuration for host %s", mcUpdateURL),
Error: iodine.New(err, nil),
})
}
msg, err := doUpdateCheck(hostConfig)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: msg,
Error: iodine.New(err, nil),
})

10
main.go
View File

@@ -35,7 +35,7 @@ import (
func checkConfig() {
_, err := user.Current()
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Unable to determine current user",
Error: err,
})
@@ -49,7 +49,7 @@ func checkConfig() {
// Ensures config file is sane
_, err = getMcConfig()
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unable to read config file: %s", mustGetMcConfigPath()),
Error: err,
})
@@ -120,14 +120,14 @@ func main() {
themeName := ctx.GlobalString("theme")
switch {
case console.IsValidTheme(themeName) != true:
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Please choose from this list: %s.", console.GetThemeNames()),
Error: fmt.Errorf("Theme %s is not supported.", themeName),
})
default:
err := console.SetTheme(themeName)
if err != nil {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Failed to set theme %s.", themeName),
Error: err,
})
@@ -138,7 +138,7 @@ func main() {
}
app.After = func(ctx *cli.Context) error {
if !isMcConfigExist() {
console.Fatalln(ErrorMessage{
console.Fatals(ErrorMessage{
Message: "Please run \"mc config generate\"",
Error: iodine.New(errors.New("\"mc\" is not configured"), nil),
})

View File

@@ -123,30 +123,58 @@ var (
}
}
// Fatal prints a error message and exits
Fatal = func(data ...interface{}) {
defer os.Exit(1)
// Prints prints a structured message with a newline
Prints = func(data ...interface{}) {
for i := 0; i < len(data); i++ {
err := readErrorFromdata(data[i])
if err != nil {
if NoJSONPrint {
print(themesDB[currThemeName].Error, data[i])
if !NoDebugPrint {
print(themesDB[currThemeName].Error, err)
}
println(themesDB[currThemeName].Info, data[i])
return
}
infoBytes, _ := json.Marshal(&data[i])
println(themesDB[currThemeName].JSON, string(infoBytes))
}
}
// Fatal print a error message and exit
Fatal = func(data ...interface{}) {
defer os.Exit(1)
if NoJSONPrint {
print(themesDB[currThemeName].Error, data...)
return
}
for i := 0; i < len(data); i++ {
errorMessageBytes, _ := json.Marshal(&data[i])
print(themesDB[currThemeName].JSON, string(errorMessageBytes))
}
}
// Fatalf print a error message with a format specified and exit
Fatalf = func(f string, data ...interface{}) {
defer os.Exit(1)
if NoJSONPrint {
printf(themesDB[currThemeName].Error, f, data...)
return
}
for i := 0; i < len(data); i++ {
errorMessageBytes, _ := json.Marshal(&data[i])
printf(themesDB[currThemeName].JSON, "", string(errorMessageBytes))
}
}
// Fatalf is undefined since under the context of typed messages formatting
// is already done before caller invokes console.Fatal*
// Fatalln prints a error message with a new line and exits
// Fatalln print a error message with a new line and exit
Fatalln = func(data ...interface{}) {
defer os.Exit(1)
if NoJSONPrint {
println(themesDB[currThemeName].Error, data...)
return
}
for i := 0; i < len(data); i++ {
errorMessageBytes, _ := json.Marshal(&data[i])
println(themesDB[currThemeName].JSON, string(errorMessageBytes))
}
}
// Fatals print a error structure with a new line and exit
Fatals = func(data ...interface{}) {
defer os.Exit(1)
for i := 0; i < len(data); i++ {
err := readErrorFromdata(data[i])
@@ -154,7 +182,7 @@ var (
if NoJSONPrint {
println(themesDB[currThemeName].Error, data[i])
if !NoDebugPrint {
println(themesDB[currThemeName].Error, err)
print(themesDB[currThemeName].Error, err)
}
return
}
@@ -166,34 +194,49 @@ var (
// Error prints a error message
Error = func(data ...interface{}) {
for i := 0; i < len(data); i++ {
err := readErrorFromdata(data[i])
if err != nil {
if NoJSONPrint {
print(themesDB[currThemeName].Error, data[i])
if !NoDebugPrint {
print(themesDB[currThemeName].Error, err)
}
print(themesDB[currThemeName].Error, data...)
return
}
for i := 0; i < len(data); i++ {
errorMessageBytes, _ := json.Marshal(&data[i])
print(themesDB[currThemeName].JSON, string(errorMessageBytes))
}
}
}
// Errorf is undefined since under the context of typed messages formatting
// is already done before caller invokes console.Error*
// Errorf print a error message with a format specified
Errorf = func(f string, data ...interface{}) {
if NoJSONPrint {
printf(themesDB[currThemeName].Error, f, data...)
return
}
for i := 0; i < len(data); i++ {
errorMessageBytes, _ := json.Marshal(&data[i])
printf(themesDB[currThemeName].JSON, "", string(errorMessageBytes))
}
}
// Errorln prints a error message with a new line
Errorln = func(data ...interface{}) {
if NoJSONPrint {
println(themesDB[currThemeName].Error, data...)
return
}
for i := 0; i < len(data); i++ {
errorMessageBytes, _ := json.Marshal(&data[i])
println(themesDB[currThemeName].JSON, string(errorMessageBytes))
}
}
// Errors print a error structure with a new line
Errors = func(data ...interface{}) {
for i := 0; i < len(data); i++ {
err := readErrorFromdata(data[i])
if err != nil {
if NoJSONPrint {
println(themesDB[currThemeName].Error, data[i])
if !NoDebugPrint {
println(themesDB[currThemeName].Error, err)
print(themesDB[currThemeName].Error, err)
}
return
}
@@ -239,6 +282,18 @@ var (
}
}
// Infos print a informational structured message
Infos = func(data ...interface{}) {
for i := 0; i < len(data); i++ {
if NoJSONPrint {
println(themesDB[currThemeName].Info, data[i])
return
}
infoBytes, _ := json.Marshal(&data[i])
println(themesDB[currThemeName].JSON, string(infoBytes))
}
}
// Debug prints a debug message without a new line
// Debug prints a debug message
Debug = func(a ...interface{}) {
@@ -328,6 +383,22 @@ var (
c.Printf(f, a...)
color.Output = output
mutex.Unlock()
case themesDB[currThemeName].Fatal:
mutex.Lock()
output := color.Output
color.Output = stderrColoredOutput
c.Print(ProgramName() + ": <FATAL> ")
c.Printf(f, a...)
color.Output = output
mutex.Unlock()
case themesDB[currThemeName].Error:
mutex.Lock()
output := color.Output
color.Output = stderrColoredOutput
c.Print(ProgramName() + ": <ERROR> ")
c.Printf(f, a...)
color.Output = output
mutex.Unlock()
case themesDB[currThemeName].Info:
mutex.Lock()
c.Print(ProgramName() + ": ")