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{}
func (e errInvalidArgument) Error() string {
return "invalid argument"
return "Invalid argument"
}
type errUnsupportedScheme struct {
@@ -50,7 +50,7 @@ type errInvalidGlobURL struct {
}
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 {
@@ -58,13 +58,13 @@ type errInvalidAliasName struct {
}
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{}
func (e errInvalidAuth) Error() string {
return "invalid auth keys"
return "Invalid auth keys"
}
type errNoMatchingHost struct{}
@@ -85,12 +85,12 @@ type errAliasExists struct {
}
func (e errAliasExists) Error() string {
return fmt.Sprintf("alias: %s exists", e.name)
return "Alias name: " + e.name + " exists"
}
// errInvalidAuthKeys - invalid authorization keys
type errInvalidAuthKeys struct{}
func (e errInvalidAuthKeys) Error() string {
return "invalid authorization keys"
return "Invalid authorization keys"
}

View File

@@ -99,7 +99,13 @@ func main() {
if theme != "" {
err := console.SetTheme(theme)
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()

View File

@@ -91,35 +91,21 @@ var WhiteTheme = Theme{
// NoColorTheme disables color theme
var NoColorTheme = Theme{}
func isValidTheme(themeName string) bool {
for key := range ThemesDB {
if key == themeName {
return true
}
}
return false
}
// SetTheme sets a color theme
func SetTheme(themeName string) error {
if !isValidTheme(themeName) {
msg := fmt.Sprintf("invalid theme: %s", themeName)
return iodine.New(errors.New(msg), nil)
}
currentTheme = themeName
mutex.Lock()
var (
// 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...) }
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) }
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":
// setThemeNoColor - set theme no color
func setThemeNoColor(themeName string) {
mutex.Lock()
currentTheme = themeName
Fatal = fatalPrint
Fatalln = fatalPrintln
Fatalf = fatalPrintf
@@ -132,7 +118,13 @@ func SetTheme(themeName string) error {
Debug = print
Debugln = println
Debugf = printf
default:
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) }
@@ -145,8 +137,20 @@ func SetTheme(themeName string) error {
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
func SetTheme(themeName string) error {
switch true {
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 nil
}