Skip to content

Conversation

fabn
Copy link
Contributor

@fabn fabn commented Nov 19, 2024

Following what has been suggested in #323 I tried to implement a prettyJSON template function and a pretty flag for existing json.

My go skills are near to zero, so forgive me if I wrote bad code. I'm open to suggestions 😉

Basic usage:

# Output formatted json from object
{{- with $msg := .Message | tryParseJSON -}}
{{- json $msg true }}{{ "\n" }}
{{- end -}}
# Output formatted JSON from string
{{ .Message | prettyJSON }}

@tksm
Copy link
Contributor

tksm commented Nov 21, 2024

Hi, thanks for the PR!

How about enhancing prettyJSON to handle both JSON strings and JSON objects(map[string]any) directly, instead of introducing the pretty flag for json? This change might simplify the usage and make it more intuitive.

Updated prettyJSON

Since Go templates cannot handle errors, it returns the original string if an error occurs.

		"prettyJSON": func(value any) string {
			var data map[string]any

			switch v := value.(type) {
			case string:
				if err := json.Unmarshal([]byte(v), &data); err != nil {
					return v
				}
			case map[string]any:
				data = v
			default:
				return fmt.Sprintf("%v", value)
			}

			b, err := json.MarshalIndent(data, "", "  ")
			if err != nil {
				return fmt.Sprintf("%v", value)
			}

			return string(b)
		},

Example: Handling JSON strings

dist/stern --stdin --template \
  '{{ .Message | prettyJSON }}{{"\n"}}' <<EOS
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:10","msg":"hello","key1":"val1"}
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:13","msg":"world","key1":"val1"}
invalid json
EOS

Invalid JSON strings (such as invalid json) are printed as is.

{
  "caller": "sandbox2210433069/prog.go:10",
  "key1": "val1",
  "level": "info",
  "msg": "hello",
  "ts": 1257894000
}
{
  "caller": "sandbox2210433069/prog.go:13",
  "key1": "val1",
  "level": "info",
  "msg": "world",
  "ts": 1257894000
}
invalid json

Example: Handling JSON objects

dist/stern --stdin --template \
  '{{ with $msg := .Message | tryParseJSON }}{{ prettyJSON $msg }}{{"\n"}}{{end}}' <<EOS
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:10","msg":"hello","key1":"val1"}
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:13","msg":"world","key1":"val1"}
invalid json
EOS

Invalid JSON strings (such as invalid json) are ignored because of the with clause.

{
  "caller": "sandbox2210433069/prog.go:10",
  "key1": "val1",
  "level": "info",
  "msg": "hello",
  "ts": 1257894000
}
{
  "caller": "sandbox2210433069/prog.go:13",
  "key1": "val1",
  "level": "info",
  "msg": "world",
  "ts": 1257894000
}

@fabn
Copy link
Contributor Author

fabn commented Nov 21, 2024

Awesome, will do that.

@fabn
Copy link
Contributor Author

fabn commented Nov 22, 2024

@tksm I pushed your suggestion, let me know

@fabn
Copy link
Contributor Author

fabn commented Nov 22, 2024

I guess I should update the doc as well, but I want to wait for your review before doing that

@tksm
Copy link
Contributor

tksm commented Nov 22, 2024

LGTM for the prettyJSON func 👍

@tksm
Copy link
Contributor

tksm commented Nov 22, 2024

📝 I have already removed extjson and ppextjson from the template function list in #325, but thank you for updating it anyway.

@fabn
Copy link
Contributor Author

fabn commented Nov 22, 2024

LGTM for the prettyJSON func 👍

I added some examples in the readme and removed outdated options.

Co-authored-by: Takashi Kusumi <tkusumi@zlab.co.jp>
Copy link
Contributor

@tksm tksm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tksm tksm merged commit ccd8add into stern:master Nov 22, 2024
2 checks passed
@fabn fabn deleted the feature/ppJson branch November 22, 2024 11:05
@dylanrb123
Copy link

Hello, I just found this and would love to try to use it but it looks like it hasn't been released. Any chance you could cut a new version? Thanks!

@superbrothers
Copy link
Member

I've just released a new version. Thanks. https://github.com/stern/stern/releases/tag/v1.32.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants