Skip to content
Snippets Groups Projects
Commit 1967183f authored by Miguel Montes's avatar Miguel Montes
Browse files

Cambios en el manejo de errores

Cambios en el procesamiento de la línea de comandos
parent 58ae5c27
No related branches found
No related tags found
No related merge requests found
......@@ -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()
}
......@@ -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)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment