Draft: Add yaml as format of container stats and configs

Signed-off-by: Tony-Sol <tony.kent.nar.earth@gmail.com>
This commit is contained in:
Tony-Sol 2023-03-29 22:10:20 +03:00
parent fcc0bbe552
commit 036c3e4220
No known key found for this signature in database
GPG key ID: 0B27B02E462B2C67
4 changed files with 30 additions and 5 deletions

View file

@ -130,6 +130,10 @@ type GuiConfig struct {
// When true, increases vertical space used by focused side panel, // When true, increases vertical space used by focused side panel,
// creating an accordion effect // creating an accordion effect
ExpandFocusedSidePanel bool `yaml:"expandFocusedSidePanel"` ExpandFocusedSidePanel bool `yaml:"expandFocusedSidePanel"`
// DetailsFormat determines in which format will be printed container stats and configs
// By default "json", available values "json", "yaml" ("yml")
DetailsFormat string `yaml:"detailsFormat,omitempty"`
} }
// CommandTemplatesConfig determines what commands actually get called when we // CommandTemplatesConfig determines what commands actually get called when we
@ -362,6 +366,7 @@ func GetDefaultConfig() UserConfig {
SidePanelWidth: 0.3333, SidePanelWidth: 0.3333,
ShowBottomLine: true, ShowBottomLine: true,
ExpandFocusedSidePanel: false, ExpandFocusedSidePanel: false,
DetailsFormat: "json",
}, },
ConfirmOnQuit: false, ConfirmOnQuit: false,
Logs: LogsConfig{ Logs: LogsConfig{

View file

@ -2,7 +2,6 @@ package gui
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -200,7 +199,7 @@ func (gui *Gui) containerConfigStr(container *commands.Container) string {
output += "none\n" output += "none\n"
} }
data, err := json.MarshalIndent(&container.Details, "", " ") data, err := utils.MarshalIntoFormat(&container.Details, gui.Config.UserConfig.Gui.DetailsFormat)
if err != nil { if err != nil {
return fmt.Sprintf("Error marshalling container details: %v", err) return fmt.Sprintf("Error marshalling container details: %v", err)
} }

View file

@ -1,7 +1,6 @@
package presentation package presentation
import ( import (
"encoding/json"
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
@ -38,7 +37,7 @@ func RenderStats(userConfig *config.UserConfig, container *commands.Container, v
dataReceived := fmt.Sprintf("Traffic received: %s", utils.FormatDecimalBytes(stats.ClientStats.Networks.Eth0.RxBytes)) dataReceived := fmt.Sprintf("Traffic received: %s", utils.FormatDecimalBytes(stats.ClientStats.Networks.Eth0.RxBytes))
dataSent := fmt.Sprintf("Traffic sent: %s", utils.FormatDecimalBytes(stats.ClientStats.Networks.Eth0.TxBytes)) dataSent := fmt.Sprintf("Traffic sent: %s", utils.FormatDecimalBytes(stats.ClientStats.Networks.Eth0.TxBytes))
originalJSON, err := json.MarshalIndent(stats, "", " ") originalStats, err := utils.MarshalIntoFormat(stats, userConfig.Gui.DetailsFormat)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -48,7 +47,7 @@ func RenderStats(userConfig *config.UserConfig, container *commands.Container, v
pidsCount, pidsCount,
dataReceived, dataReceived,
dataSent, dataSent,
string(originalJSON), string(originalStats),
) )
return contents, nil return contents, nil

View file

@ -2,6 +2,7 @@ package utils
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
@ -13,6 +14,7 @@ import (
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/yaml"
"github.com/fatih/color" "github.com/fatih/color"
) )
@ -339,3 +341,23 @@ func IsValidHexValue(v string) bool {
func OpensMenuStyle(str string) string { func OpensMenuStyle(str string) string {
return ColoredString(fmt.Sprintf("%s...", str), color.FgMagenta) return ColoredString(fmt.Sprintf("%s...", str), color.FgMagenta)
} }
func MarshalIntoFormat(data interface{}, format string) ([]byte, error) {
dataJSON, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, err
}
switch format {
case "json":
return dataJSON, err
case "yaml", "yml":
// Use Unmarshal->Marshal hack to convert vendor-locked objects to YAML with same structure as JSON
var dataMirror yaml.MapSlice
if err := yaml.Unmarshal(dataJSON, &dataMirror); err != nil {
return nil, err
}
return yaml.Marshal(dataMirror)
default:
return nil, errors.New(fmt.Sprintf("Unsupported detailization format: %s", format))
}
}