Merge pull request #461 from harshavardhana/pr_out_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:22:23 -07:00
3 changed files with 67 additions and 57 deletions
+6 -6
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"
}
+7 -1
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()
+54 -50
View File
@@ -91,62 +91,66 @@ 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
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...) }
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
func SetTheme(themeName string) error {
if !isValidTheme(themeName) {
msg := fmt.Sprintf("invalid theme: %s", themeName)
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)
}
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
}