From cac2ca1b17ef21ac00a97e68d3de458e6fcc154c Mon Sep 17 00:00:00 2001
From: Miguel Montes <miguel.montes@gmail.com>
Date: Thu, 13 Dec 2018 19:08:40 -0300
Subject: [PATCH] =?UTF-8?q?Agregado=20formateo=20de=20salida=20de=20sealer?=
 =?UTF-8?q?s=20para=20facilitar=20integraci=C3=B3n=20con=20telegraf?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 bfa_client/src/client/bfa_client.go | 85 ++++++++++++++++-------------
 1 file changed, 47 insertions(+), 38 deletions(-)

diff --git a/bfa_client/src/client/bfa_client.go b/bfa_client/src/client/bfa_client.go
index 4820b57..dfa5bab 100644
--- a/bfa_client/src/client/bfa_client.go
+++ b/bfa_client/src/client/bfa_client.go
@@ -153,16 +153,18 @@ func sealers() {
 		balance     bool
 		header      bool
 		format      string
+		formatStr   string
 	)
-	description = "Presenta la lista de selladores. Opcionalmente indica el último bloque sellado por cada uno."
+	description = "Presenta la lista de selladores. Opcionalmente presenta información sobre los selladores."
 	setFlags()
 	flags.Int64Var(&blockNumber, "block-number", latest, "Número del bloque en el cual se quiere conocer la lista de selladores (-1 para el último)")
 	flags.BoolVar(&lastBlock, "last-block", false, "Muestra el último bloque sellado por cada sellador, o 0 si un nodo no ha sellado en las últimas 5 rondas.")
 	flags.BoolVar(&timestamp, "timestamp", false, "Muestra el timestamp del sellado.")
 	flags.BoolVar(&difficulty, "difficulty", false, "Muestra la dificultad del sellado (1: fuera de turno, 2: en turno).")
 	flags.BoolVar(&balance, "balance", false, "Muestra el saldo del sellador (en Wei).")
-	flags.BoolVar(&header, "header", true, "Muestra un encabezado en cada columna.")
+	flags.BoolVar(&header, "header", false, "Muestra un encabezado en cada columna.")
 	flags.StringVar(&format, "format", "", "Formato del timestamp. Ignorado en formato json. Opciones: 'unix', 'rfc3339', 'long' ('YYYY-MM-DD hh:mm:ss'), 'short' ('hh:mm:ss') o un formato específico. Ejemplo 'DD/MM/YY hh.mm.ss'. También se admite el formato del paquete 'time' de go.")
+	flags.StringVar(&formatStr, "output-format", "", "Formato de la salida. Ignorado en formato json. Es una string en la que se reemplazan las ocurrencias de {sealer}, {last-block}, {timestamp}, {difficulty} y {balance} por sus respectivos valores.")
 	parseFlags(false)
 	if blockNumber == 0 {
 		util.Error("El bloque génesis no tiene firmantes")
@@ -173,21 +175,41 @@ func sealers() {
 	defer node.Close()
 	blockNumber = node.BlockNumberInRange(blockNumber)
 	extended := lastBlock || timestamp || difficulty || balance
-	if extended {
-		sealers := node.SealersStatus(blockNumber)
+	if !extended && formatStr == "" {
+		sealers := node.SealersAtBlock(blockNumber)
+		sort.Slice(sealers, func(i, j int) bool { return sealers[i] < sealers[j] })
 		if json {
 			util.PrintJson(sealers)
-			return
-		}
-		var (
-			list         []string
-			lastBlockLen int64
-			timestampLen int64
-			balanceLen   int64
-		)
-		for sealer := range sealers {
-			list = append(list, sealer)
+		} else {
+			for _, sealer := range sealers {
+				fmt.Println(sealer)
+			}
 		}
+		return
+	}
+	sealers := node.SealersStatus(blockNumber)
+	if json {
+		util.PrintJson(sealers)
+		return
+	}
+	var (
+		list         []string
+		lastBlockLen int64
+		timestampLen int64
+		balanceLen   int64
+	)
+	for sealer := range sealers {
+		list = append(list, sealer)
+	}
+	if formatStr != "" {
+		sort.Strings(list)
+		formatStr = strings.Replace(formatStr, "{sealer}", "%[1]v", 1)
+		formatStr = strings.Replace(formatStr, "{last-block}", "%[3]v", 1)
+		formatStr = strings.Replace(formatStr, "{timestamp}", "%[5]v", 1)
+		formatStr = strings.Replace(formatStr, "{difficulty}", "%[6]v", 1)
+		formatStr = strings.Replace(formatStr, "{balance}", "%[8]v", 1)
+		timestamp = false
+	} else {
 		switch {
 		case lastBlock:
 			sort.Slice(list, func(i, j int) bool { return sealers[list[i]].LastBlock > sealers[list[j]].LastBlock })
@@ -198,7 +220,7 @@ func sealers() {
 		case balance:
 			sort.Slice(list, func(i, j int) bool { return sealers[list[i]].Balance.Cmp(sealers[list[j]].Balance) > 0 })
 		}
-		formatStr := "%42v"
+		formatStr = "%42v"
 		if lastBlock {
 			formatStr += " %[2]*[3]v"
 			lastBlockLen = util.Max(2, int64(len(strconv.FormatInt(sealers[list[0]].LastBlock, 10))))
@@ -222,7 +244,6 @@ func sealers() {
 				balanceLen = util.Max(balanceLen, int64(len(s.Balance.String())))
 			}
 		}
-		formatStr += "\n"
 		if header {
 			fmt.Printf(formatStr,
 				fmt.Sprintf("%-24v", "Sealer"),
@@ -231,29 +252,17 @@ func sealers() {
 				"Dif",
 				balanceLen, fmt.Sprintf("%*v", -balanceLen/2-4, "Balance"))
 		}
-		for _, sealer := range list {
-			var formatedTimestamp interface{}
-			s := sealers[sealer]
-			if timestamp {
-				t := int64(s.Time)
-				if len(format) > 0 && t > 0 {
-					formatedTimestamp = time.Unix(t, 0).Format(format)
-				} else {
-					formatedTimestamp = t
-				}
-			}
-			fmt.Printf(formatStr, sealer, lastBlockLen, s.LastBlock, timestampLen, formatedTimestamp, s.Difficulty, balanceLen, s.Balance)
-		}
-	} else {
-		sealers := node.SealersAtBlock(blockNumber)
-		sort.Slice(sealers, func(i, j int) bool { return sealers[i] < sealers[j] })
-		if json {
-			util.PrintJson(sealers)
-		} else {
-			for _, sealer := range sealers {
-				fmt.Println(sealer)
-			}
+	}
+	formatStr += "\n"
+	for _, sealer := range list {
+		var formatedTimestamp interface{}
+		s := sealers[sealer]
+		t := int64(s.Time)
+		formatedTimestamp = t
+		if timestamp && len(format) > 0 && t > 0 {
+			formatedTimestamp = time.Unix(t, 0).Format(format)
 		}
+		fmt.Printf(formatStr, sealer, lastBlockLen, s.LastBlock, timestampLen, formatedTimestamp, s.Difficulty, balanceLen, s.Balance)
 	}
 }
 
-- 
GitLab