diff --git a/bfa_client/src/client/bfa_client.go b/bfa_client/src/client/bfa_client.go index 2749e102b3b80e21a6fc462303204ac1a2185921..be426749cb22687f73a065ab5cc29cd04983cecb 100644 --- a/bfa_client/src/client/bfa_client.go +++ b/bfa_client/src/client/bfa_client.go @@ -28,7 +28,6 @@ var ( json bool help bool flags = flag.NewFlagSet("", flag.ExitOnError) - command string description string otherArgs string ) @@ -61,7 +60,8 @@ func updateURL(url string) (updated string) { } func usage(errorCode int) { - _, _ = fmt.Fprintf(os.Stderr, "Uso: %v %v [opciones] %v\n%v\n", os.Args[0], command, otherArgs, description) + util.Require(len(os.Args) > 1, "not enough arguments") + _, _ = fmt.Fprintf(os.Stderr, "Uso: %v %v [opciones] %v\n%v\n", os.Args[0], os.Args[1], otherArgs, description) flags.PrintDefaults() os.Exit(errorCode) } @@ -441,9 +441,10 @@ func main() { "snapshot": snapshot, } validCommands []string + command func() ) - for command := range commands { - validCommands = append(validCommands, command) + for cmd := range commands { + validCommands = append(validCommands, cmd) } sort.Strings(validCommands) //defer func() { @@ -452,12 +453,8 @@ func main() { // } //}() if len(os.Args) > 1 { - command = os.Args[1] + command = commands[os.Args[1]] } - if commands[command] == nil { - log.Printf("Uso: %v <%v> [opciones]\n", path.Base(os.Args[0]), strings.Join(validCommands, "|")) - log.Printf("Para ayuda: %v <command> -h\n", path.Base(os.Args[0])) - os.Exit(1) - } - commands[command]() + util.Ensure(command != nil, "Uso: %v <%v> [opciones]\nPara ayuda: %v <command> -h\n", path.Base(os.Args[0]), strings.Join(validCommands, "|"), path.Base(os.Args[0])) + command() } diff --git a/bfa_client/src/util/util.go b/bfa_client/src/util/util.go index 95696a896cad3552bef8ec76f7365e1e43c1daad..fab25581370439fab2b9825fc585ecab297b7c91 100644 --- a/bfa_client/src/util/util.go +++ b/bfa_client/src/util/util.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" "log" + "os" "runtime" "strconv" ) @@ -19,12 +20,21 @@ func Contains(slice []string, s string) bool { return false } -func Check(err error) { - if err != nil { - panic(err.Error()) +func Error(format string, args ...interface{}) { + _, _ = fmt.Fprintf(os.Stderr, format, args...) + os.Exit(1) +} + +func Ensure(condition bool, format string, args ...interface{}) { + if !condition { + Error(format, args...) } } +func Check(err error) { + Ensure(err == nil, "%v\n", err) +} + func BytesToHex(b []byte) string { return fmt.Sprintf("0x%02x", b) } @@ -33,6 +43,13 @@ func Int64ToHex(n int64) string { return "0x" + strconv.FormatInt(n, 16) } +func Require(condition bool, msg string) { + if !condition { + ptr, _, _, _ := runtime.Caller(1) + log.Panicf("%v in %v", msg, runtime.FuncForPC(ptr).Name()) + } +} + func PrintDebug(prefix, suffix string) { ptr, _, _, _ := runtime.Caller(1) fmt.Printf("%v: %v%v\n", prefix, runtime.FuncForPC(ptr).Name(), suffix) @@ -65,7 +82,5 @@ func IsAddress(address string) bool { } func DieIf(cond bool, format string, args ...interface{}) { - if cond { - log.Fatalf(format, args...) - } + Ensure(!cond, format, args) }