-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Render container stats and configs as yaml #444
Changes from 4 commits
036c3e4
da5df63
c6a0138
8ab751e
781af68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package utils | |
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"html/template" | ||
"io" | ||
|
@@ -14,7 +15,12 @@ import ( | |
"github.com/go-errors/errors" | ||
"github.com/jesseduffield/gocui" | ||
|
||
// "github.com/jesseduffield/yaml" | ||
|
||
"github.com/fatih/color" | ||
"github.com/goccy/go-yaml" | ||
"github.com/goccy/go-yaml/lexer" | ||
"github.com/goccy/go-yaml/printer" | ||
) | ||
|
||
// SplitLines takes a multiline string and splits it on newlines | ||
|
@@ -52,6 +58,45 @@ func ColoredString(str string, colorAttribute color.Attribute) string { | |
return ColoredStringDirect(str, colour) | ||
} | ||
|
||
// ColoredYamlString takes an YAML formatted string and returns a colored string | ||
// with colors hardcoded as: | ||
// keys: cyan | ||
// Booleans: magenta | ||
// Numbers: yellow | ||
// Strings: green | ||
func ColoredYamlString(str string) string { | ||
format := func(attr color.Attribute) string { | ||
return fmt.Sprintf("%s[%dm", "\x1b", attr) | ||
} | ||
tokens := lexer.Tokenize(str) | ||
var p printer.Printer | ||
p.Bool = func() *printer.Property { | ||
return &printer.Property{ | ||
Prefix: format(color.FgMagenta), | ||
Suffix: format(color.Reset), | ||
} | ||
} | ||
p.Number = func() *printer.Property { | ||
return &printer.Property{ | ||
Prefix: format(color.FgYellow), | ||
Suffix: format(color.Reset), | ||
} | ||
} | ||
p.MapKey = func() *printer.Property { | ||
return &printer.Property{ | ||
Prefix: format(color.FgCyan), | ||
Suffix: format(color.Reset), | ||
} | ||
} | ||
p.String = func() *printer.Property { | ||
return &printer.Property{ | ||
Prefix: format(color.FgGreen), | ||
Suffix: format(color.Reset), | ||
} | ||
} | ||
return p.PrintTokens(tokens) | ||
} | ||
|
||
// MultiColoredString takes a string and an array of colour attributes and returns a colored | ||
// string with those attributes | ||
func MultiColoredString(str string, colorAttribute ...color.Attribute) string { | ||
|
@@ -339,3 +384,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": | ||
// Use Unmarshal->Marshal hack to convert vendor-locked objects to YAML with same structure as JSON | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain a bit more about what's happening here? What do you mean by vendor locked? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, So, in order to keep -- CPUUsage CPUUsage `json:"cpu_usage"`
++ CPUUsage CPUUsage `json:"cpu_usage" yaml:"cpu_usage"` and keep the resulting yaml structure exactly as json is, let's at first marshal into json as tagged, then into "any" yaml There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that makes sense. Could you capture that in a comment in the code? |
||
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)) | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a unit test for this? (Accidentally just left this as a general comment cos I'm reviewing from phone :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jesseduffield sure, done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And sadly, i don't get about scrapping "the JSON-related code" :c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I mean is, let's remove
DetailsFormat
and always use yaml formatting.