diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index c3a0994a..9497e580 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -130,6 +130,10 @@ type GuiConfig struct { // When true, increases vertical space used by focused side panel, // creating an accordion effect 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 @@ -362,6 +366,7 @@ func GetDefaultConfig() UserConfig { SidePanelWidth: 0.3333, ShowBottomLine: true, ExpandFocusedSidePanel: false, + DetailsFormat: "json", }, ConfirmOnQuit: false, Logs: LogsConfig{ diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index 862fc4be..314f3b47 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -2,7 +2,6 @@ package gui import ( "context" - "encoding/json" "fmt" "strings" "time" @@ -200,7 +199,7 @@ func (gui *Gui) containerConfigStr(container *commands.Container) string { output += "none\n" } - data, err := json.MarshalIndent(&container.Details, "", " ") + data, err := utils.MarshalIntoFormat(&container.Details, gui.Config.UserConfig.Gui.DetailsFormat) if err != nil { return fmt.Sprintf("Error marshalling container details: %v", err) } diff --git a/pkg/gui/presentation/container_stats.go b/pkg/gui/presentation/container_stats.go index 82767881..bbc9197e 100644 --- a/pkg/gui/presentation/container_stats.go +++ b/pkg/gui/presentation/container_stats.go @@ -1,7 +1,6 @@ package presentation import ( - "encoding/json" "fmt" "math" "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)) 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 { return "", err } @@ -48,7 +47,7 @@ func RenderStats(userConfig *config.UserConfig, container *commands.Container, v pidsCount, dataReceived, dataSent, - string(originalJSON), + string(originalStats), ) return contents, nil diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index be86bd22..6046dbc1 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "bytes" + "encoding/json" "fmt" "html/template" "io" @@ -13,6 +14,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/yaml" "github.com/fatih/color" ) @@ -339,3 +341,23 @@ func IsValidHexValue(v string) bool { func OpensMenuStyle(str string) string { 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)) + } +}