yats.git

commit 0f5549e69c64ceaef6d08f96aabbc01a1ba71606

Author: Paolo Lulli <paolo@lulli.net>

Add switch to unpack logs and base64

 client/main.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++-----


diff --git a/client/main.go b/client/main.go
index 2bb2451ab1bbf80a11349fe31cb860c3a8b05a2c..52db9f528b40d37f664adea4d8b525ba1a9d02f3 100644
--- a/client/main.go
+++ b/client/main.go
@@ -10,6 +10,7 @@  */
 package main
 
 import (
+	"encoding/base64"
 	"encoding/json"
 	"fmt"
 	"os"
@@ -66,6 +67,8 @@ 	sinceDays := flag.BoolP("day", "", false, "days ago")
 	sinceYears := flag.BoolP("year", "", false, "years ago")
 
 	reformatOutput := flag.BoolP("format", "f", false, "format output")
+	logOutput := flag.BoolP("value-only", "l", false, "log value only output")
+	decodeBase64Output := flag.BoolP("base64-decode", "b", false, "decode base64 output")
 
 	flag.Parse()
 
@@ -132,17 +135,11 @@
 	if *listEvents {
 		listEvents := yatsClient.EventList(*sourceOption, fromTimestamp, toTimestamp)
 		if *reformatOutput {
-
-			var result map[string]interface{}
-			json.Unmarshal([]byte(listEvents), &result)
-			innerData := result["data"]
-
-			dataJson, er := json.Marshal(innerData)
-			if er != nil {
-				fmt.Printf("Parsing response: %v", er)
+			dataJson, errDataPortion := getDataJson(listEvents)
+			if errDataPortion {
+				fmt.Printf("Could not parse data attribute: %v", errDataPortion)
 				return
 			}
-			//fmt.Printf("--->%s", dataJson)
 
 			var eventListModel []EventModel
 			err := json.Unmarshal(dataJson, &eventListModel)
@@ -162,8 +159,59 @@ 	}
 
 	if "" != *listMetrics {
 		listResponse, err := yatsClient.MetricList(*sourceOption, *listMetrics, fromTimestamp, toTimestamp)
+
 		if err == nil {
+			if *reformatOutput {
+
+				dataJson, errDataPortion := getDataJson(listResponse)
+				if errDataPortion {
+					fmt.Printf("Could not parse data attribute: %v", errDataPortion)
+					return
+				}
+
+				var metricListModel []MetricModel
+				err := json.Unmarshal(dataJson, &metricListModel)
+
+				if err == nil {
+					for _, metric := range metricListModel {
+						fmt.Printf("%d %s=%s\n", metric.Mtime, metric.Name, metric.Value)
+					}
+				} else {
+					fmt.Println(err)
+				}
+				os.Exit(0)
+			}
+			if *logOutput {
+				dataJson, errDataPortion := getDataJson(listResponse)
+				if errDataPortion {
+					fmt.Printf("Could not parse data attribute: %v", errDataPortion)
+					return
+				}
+
+				var metricListModel []MetricModel
+				err := json.Unmarshal(dataJson, &metricListModel)
+
+				if err == nil {
+					for _, metric := range metricListModel {
+						if *decodeBase64Output {
+							decodedBytes, err := base64.StdEncoding.DecodeString(metric.Value)
+							if err != nil {
+								fmt.Println("Error decoding string:", err)
+								return
+							}
+							fmt.Println(string(decodedBytes))
+						} else {
+							fmt.Printf("%s\n", metric.Value)
+						}
+					}
+				} else {
+					fmt.Println(err)
+				}
+				os.Exit(0)
+			}
+
 			fmt.Printf("%s", listResponse)
+
 		} else {
 			fmt.Println(err)
 		}
@@ -175,6 +223,19 @@ 		listResponse := yatsClient.PositionList(*sourceOption, fromTimestamp, toTimestamp)
 		fmt.Printf("%s", listResponse)
 		os.Exit(0)
 	}
+}
+
+func getDataJson(list string) ([]byte, bool) {
+	var result map[string]interface{}
+	json.Unmarshal([]byte(list), &result)
+	innerData := result["data"]
+
+	dataJson, er := json.Marshal(innerData)
+	if er != nil {
+		fmt.Printf("Parsing response: %v", er)
+		return nil, true
+	}
+	return dataJson, false
 }
 
 func calculateCutDate(ts int64, sinceSeconds *bool, sinceMinutes *bool, sinceHours *bool, sinceDays *bool, sinceYears *bool) int64 {