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

Make sure to properly exit if requested theme not available, also re-structure console package a bit

This commit is contained in:
Harshavardhana
2015-04-20 13:19:24 -07:00
parent 0c7805e65f
commit af1cb1b478
3 changed files with 67 additions and 57 deletions

View File

@@ -25,7 +25,7 @@ import (
type errInvalidArgument struct{} type errInvalidArgument struct{}
func (e errInvalidArgument) Error() string { func (e errInvalidArgument) Error() string {
return "invalid argument" return "Invalid argument"
} }
type errUnsupportedScheme struct { type errUnsupportedScheme struct {
@@ -50,7 +50,7 @@ type errInvalidGlobURL struct {
} }
func (e errInvalidGlobURL) Error() string { func (e errInvalidGlobURL) Error() string {
return "Error parsing glob'ed URL while comparing " + e.glob + " " + e.request return "Error reading glob URL " + e.glob + " while comparing with " + e.request
} }
type errInvalidAliasName struct { type errInvalidAliasName struct {
@@ -58,13 +58,13 @@ type errInvalidAliasName struct {
} }
func (e errInvalidAliasName) Error() string { func (e errInvalidAliasName) Error() string {
return "Not a valid alias name: " + e.name + " Valid examples are: Area51, Grand-Nagus.." return "Not a valid alias name: " + e.name + " valid examples are: Area51, Grand-Nagus.."
} }
type errInvalidAuth struct{} type errInvalidAuth struct{}
func (e errInvalidAuth) Error() string { func (e errInvalidAuth) Error() string {
return "invalid auth keys" return "Invalid auth keys"
} }
type errNoMatchingHost struct{} type errNoMatchingHost struct{}
@@ -85,12 +85,12 @@ type errAliasExists struct {
} }
func (e errAliasExists) Error() string { func (e errAliasExists) Error() string {
return fmt.Sprintf("alias: %s exists", e.name) return "Alias name: " + e.name + " exists"
} }
// errInvalidAuthKeys - invalid authorization keys // errInvalidAuthKeys - invalid authorization keys
type errInvalidAuthKeys struct{} type errInvalidAuthKeys struct{}
func (e errInvalidAuthKeys) Error() string { func (e errInvalidAuthKeys) Error() string {
return "invalid authorization keys" return "Invalid authorization keys"
} }

View File

@@ -99,7 +99,13 @@ func main() {
if theme != "" { if theme != "" {
err := console.SetTheme(theme) err := console.SetTheme(theme)
if err != nil { if err != nil {
console.Fatalf("mc: Unable to set theme [%s]\n", theme) // SetTheme here back to DefaultTheme, Fatalf will not exit otherwise since the wrappers
// anonymous func() are not assigned yet, so in essence os.Exit(1) in Fatalf is not
// available and wouldn't exit here - call gets transferred to app.RunAndExitOnError()
// On windows without this it leads to nil deference
console.SetTheme(console.GetDefaultTheme())
console.Fatalf("mc: failed to set theme [%s] with following reason: [%s]\n", theme, iodine.ToError(err))
} }
} }
checkConfig() checkConfig()

View File

@@ -91,62 +91,66 @@ var WhiteTheme = Theme{
// NoColorTheme disables color theme // NoColorTheme disables color theme
var NoColorTheme = Theme{} var NoColorTheme = Theme{}
func isValidTheme(themeName string) bool { var (
for key := range ThemesDB { // wrap around standard fmt functions
if key == themeName { print = func(a ...interface{}) { fmt.Print(a...) }
return true println = func(a ...interface{}) { fmt.Println(a...) }
} printf = func(f string, a ...interface{}) { fmt.Printf(f, a...) }
}
return false fatalPrint = func(a ...interface{}) { fmt.Print(a...); os.Exit(1) }
fatalPrintln = func(a ...interface{}) { fmt.Println(a...); os.Exit(1) }
fatalPrintf = func(f string, a ...interface{}) { fmt.Printf(f, a...); os.Exit(1) }
)
// setThemeNoColor - set theme no color
func setThemeNoColor(themeName string) {
mutex.Lock()
currentTheme = themeName
Fatal = fatalPrint
Fatalln = fatalPrintln
Fatalf = fatalPrintf
Error = print
Errorln = println
Errorf = printf
Info = print
Infoln = println
Infof = printf
Debug = print
Debugln = println
Debugf = printf
mutex.Unlock()
}
// setThemeColor - set theme color style
func setThemeColor(themeName string) {
mutex.Lock()
currentTheme = themeName
Fatal = func(a ...interface{}) { ThemesDB[currentTheme].Fatal.Print(a...); os.Exit(1) }
Fatalln = func(a ...interface{}) { ThemesDB[currentTheme].Fatal.Println(a...); os.Exit(1) }
Fatalf = func(f string, a ...interface{}) { ThemesDB[currentTheme].Fatal.Printf(f, a...); os.Exit(1) }
Error = func(a ...interface{}) { ThemesDB[currentTheme].Error.Print(a...) }
Errorln = func(a ...interface{}) { ThemesDB[currentTheme].Error.Println(a...) }
Errorf = func(f string, a ...interface{}) { ThemesDB[currentTheme].Error.Printf(f, a...) }
Info = func(a ...interface{}) { ThemesDB[currentTheme].Info.Print(a...) }
Infoln = func(a ...interface{}) { ThemesDB[currentTheme].Info.Println(a...) }
Infof = func(f string, a ...interface{}) { ThemesDB[currentTheme].Info.Printf(f, a...) }
Debug = func(a ...interface{}) { ThemesDB[currentTheme].Debug.Print(a...) }
Debugln = func(a ...interface{}) { ThemesDB[currentTheme].Debug.Println(a...) }
Debugf = func(f string, a ...interface{}) { ThemesDB[currentTheme].Debug.Printf(f, a...) }
mutex.Unlock()
} }
// SetTheme sets a color theme // SetTheme sets a color theme
func SetTheme(themeName string) error { func SetTheme(themeName string) error {
if !isValidTheme(themeName) { switch true {
msg := fmt.Sprintf("invalid theme: %s", themeName) case themeName == "nocolor":
setThemeNoColor(themeName)
case themeName == "minimal" || themeName == "white":
setThemeColor(themeName)
default:
msg := fmt.Sprintf("Invalid theme: %s", themeName)
return iodine.New(errors.New(msg), nil) return iodine.New(errors.New(msg), nil)
} }
currentTheme = themeName
mutex.Lock()
// wrap around standard fmt functions
print := func(a ...interface{}) { fmt.Print(a...) }
println := func(a ...interface{}) { fmt.Println(a...) }
printf := func(f string, a ...interface{}) { fmt.Printf(f, a...) }
fatalPrint := func(a ...interface{}) { fmt.Print(a...); os.Exit(1) }
fatalPrintln := func(a ...interface{}) { fmt.Println(a...); os.Exit(1) }
fatalPrintf := func(f string, a ...interface{}) { fmt.Printf(f, a...); os.Exit(1) }
switch currentTheme {
case "nocolor":
Fatal = fatalPrint
Fatalln = fatalPrintln
Fatalf = fatalPrintf
Error = print
Errorln = println
Errorf = printf
Info = print
Infoln = println
Infof = printf
Debug = print
Debugln = println
Debugf = printf
default:
Fatal = func(a ...interface{}) { ThemesDB[currentTheme].Fatal.Print(a...); os.Exit(1) }
Fatalln = func(a ...interface{}) { ThemesDB[currentTheme].Fatal.Println(a...); os.Exit(1) }
Fatalf = func(f string, a ...interface{}) { ThemesDB[currentTheme].Fatal.Printf(f, a...); os.Exit(1) }
Error = func(a ...interface{}) { ThemesDB[currentTheme].Error.Print(a...) }
Errorln = func(a ...interface{}) { ThemesDB[currentTheme].Error.Println(a...) }
Errorf = func(f string, a ...interface{}) { ThemesDB[currentTheme].Error.Printf(f, a...) }
Info = func(a ...interface{}) { ThemesDB[currentTheme].Info.Print(a...) }
Infoln = func(a ...interface{}) { ThemesDB[currentTheme].Info.Println(a...) }
Infof = func(f string, a ...interface{}) { ThemesDB[currentTheme].Info.Printf(f, a...) }
Debug = func(a ...interface{}) { ThemesDB[currentTheme].Debug.Print(a...) }
Debugln = func(a ...interface{}) { ThemesDB[currentTheme].Debug.Println(a...) }
Debugf = func(f string, a ...interface{}) { ThemesDB[currentTheme].Debug.Printf(f, a...) }
}
mutex.Unlock()
return nil return nil
} }