Commit d9d90a76 authored by lukasburgey's avatar lukasburgey
Browse files

Add indentation to the logs

parent dfce6039
package indent
import (
"bytes"
"encoding/json"
)
var (
prefix = ""
indent = " "
)
// Indent indents a json string if possible
// if unsuccessful it just returns src
func Indent(src []byte) (ds []byte) {
buf := new(bytes.Buffer)
err := json.Indent(buf, src, prefix, indent)
if err != nil {
return src
}
ds = buf.Bytes()
return
}
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"git.scc.kit.edu/feudal/feudalClient/config" "git.scc.kit.edu/feudal/feudalClient/config"
deps "git.scc.kit.edu/feudal/feudalClient/deployments" deps "git.scc.kit.edu/feudal/feudalClient/deployments"
"git.scc.kit.edu/feudal/feudalClient/indent"
scripts "git.scc.kit.edu/feudal/feudalScripts/v2" scripts "git.scc.kit.edu/feudal/feudalScripts/v2"
) )
...@@ -121,43 +122,31 @@ func (sink *Sink) handleDep(dep deps.Dep) (output scripts.Output, err error) { ...@@ -121,43 +122,31 @@ func (sink *Sink) handleDep(dep deps.Dep) (output scripts.Output, err error) {
stdin.Write(iBytes) stdin.Write(iBytes)
stdin.Close() stdin.Close()
// decode json output // read stdout / stderr
var outputBytes, logOutputBytes []byte var stdoutBytes, stderrBytes []byte
outputBytes, err = ioutil.ReadAll(stdout)
if err != nil {
return
}
logOutputBytes, err = ioutil.ReadAll(stderr) stdoutBytes, err = ioutil.ReadAll(stdout)
if err != nil { if err != nil {
return return
} }
if sink.Config.Debug.Scripts {
if len(logOutputBytes) > 0 { stderrBytes, _ = ioutil.ReadAll(stderr)
dep.Log("Logs:\n%s", logOutputBytes) if sink.Config.Debug.Scripts && len(stderrBytes) > 0 {
dep.Log("End of Logs") dep.Log("Logs:\n%s", stderrBytes)
} dep.Log("End of Logs")
} }
// waits for command execution and pipe reading
err = cmd.Wait() err = cmd.Wait()
if err != nil { if err != nil {
return return
} }
if sink.Config.Debug.Scripts { if sink.Config.Debug.Scripts {
var indented = new(bytes.Buffer) dep.Log("Output: %s", indent.Indent(stdoutBytes))
err = json.Indent(indented, outputBytes, " ", " ")
if err != nil {
dep.Log("Output (unable to indent: %s):\n%s", err, outputBytes)
} else {
dep.Log("Output:\n%s", indented.String())
}
} }
err = json.Unmarshal(outputBytes, &output) err = json.Unmarshal(stdoutBytes, &output)
if err != nil { if err != nil {
if len(outputBytes) > 0 {
dep.Log("Raw output: %s", outputBytes)
}
return return
} }
......
package amqp package amqp
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
...@@ -9,6 +8,7 @@ import ( ...@@ -9,6 +8,7 @@ import (
"git.scc.kit.edu/feudal/feudalClient/config" "git.scc.kit.edu/feudal/feudalClient/config"
deps "git.scc.kit.edu/feudal/feudalClient/deployments" deps "git.scc.kit.edu/feudal/feudalClient/deployments"
"git.scc.kit.edu/feudal/feudalClient/indent"
"github.com/streadway/amqp" "github.com/streadway/amqp"
) )
...@@ -119,12 +119,7 @@ func (src *Source) connect() (deliveries <-chan amqp.Delivery, closed <-chan *am ...@@ -119,12 +119,7 @@ func (src *Source) connect() (deliveries <-chan amqp.Delivery, closed <-chan *am
func (src *Source) consume(sink chan<- deps.Dep, deliveries <-chan amqp.Delivery) { func (src *Source) consume(sink chan<- deps.Dep, deliveries <-chan amqp.Delivery) {
consumeWrapper := func(del amqp.Delivery) { consumeWrapper := func(del amqp.Delivery) {
if src.Config.Debug.Backend { if src.Config.Debug.Backend {
indented := new(bytes.Buffer) log.Printf("[AMQP] Received: %s", indent.Indent(del.Body))
if err := json.Indent(indented, del.Body, " ", " "); err != nil {
log.Printf("[AMQP] Received (unable to indent: %v):\n%s", err, del.Body)
} else {
log.Printf("[AMQP] Received:\n%s", indented)
}
} }
var newDep deps.Dep var newDep deps.Dep
......
package rest package rest
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
...@@ -11,6 +10,7 @@ import ( ...@@ -11,6 +10,7 @@ import (
"git.scc.kit.edu/feudal/feudalClient/config" "git.scc.kit.edu/feudal/feudalClient/config"
deps "git.scc.kit.edu/feudal/feudalClient/deployments" deps "git.scc.kit.edu/feudal/feudalClient/deployments"
"git.scc.kit.edu/feudal/feudalClient/indent"
) )
type ( type (
...@@ -70,13 +70,7 @@ func (src Source) fetchDeps(sink chan<- deps.Dep) (err error) { ...@@ -70,13 +70,7 @@ func (src Source) fetchDeps(sink chan<- deps.Dep) (err error) {
} }
if src.Config.Debug.Backend { if src.Config.Debug.Backend {
indented := new(bytes.Buffer) log.Printf("[HTTP] Fetched: %s", indent.Indent(body))
indentErr := json.Indent(indented, body, "", " ")
if indentErr != nil {
log.Printf("[HTTP] Fetched (unable to indent: %v):\n%s", err, body)
} else {
log.Printf("[HTTP] Fetched:\n%s", indented)
}
} }
go func() { go func() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment