Skip to content

Commit

Permalink
feat: oasis paratime show parameters
Browse files Browse the repository at this point in the history
Similar to oasis network show parameters. Prints ROFL staking thresholds for
now.
  • Loading branch information
matevz committed Nov 15, 2024
1 parent 911db13 commit 025ea13
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 211 deletions.
37 changes: 37 additions & 0 deletions cmd/common/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,40 @@ func JSONMarshalUniversalValue(v interface{}) []byte {
cobra.CheckErr(err)
return vJSON
}

// PrettifyFromJSON takes a JSON-compatible interface and returns a simplified string representation
// by removing JSON syntax elements (braces, brackets, quotes, commas) and cleaning up whitespace.
// The output is a clean, line-by-line representation of the data where each non-empty line is
// trimmed of trailing whitespace.
//
// Example:
//
// input := map[string]interface{}{"name": "Alice", "data": {"age": 30}}
// output := PrettifyFromJSON(input)
// // Output will be:
// // name: Alice
// // data:
// // age: 30
func PrettifyFromJSON(blob interface{}) string {
pp, err := json.MarshalIndent(blob, "", " ")
cobra.CheckErr(err)

out := string(pp)
out = strings.ReplaceAll(out, "{", "")
out = strings.ReplaceAll(out, "}", "")
out = strings.ReplaceAll(out, "[", "")
out = strings.ReplaceAll(out, "]", "")
out = strings.ReplaceAll(out, ",", "")
out = strings.ReplaceAll(out, "\"", "")

ret := ""
for _, line := range strings.Split(out, "\n") {
line = strings.TrimRight(line, " \n")
if len(line) == 0 {
continue
}
ret += line + "\n"
}

return ret
}
26 changes: 1 addition & 25 deletions cmd/network/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,30 +390,6 @@ func showNativeToken(ctx context.Context, height int64, npa *common.NPASelection
}
}

func PrettifyFromJSON(blob interface{}) string {
pp, err := json.MarshalIndent(blob, "", " ")
cobra.CheckErr(err)

out := string(pp)
out = strings.ReplaceAll(out, "{", "")
out = strings.ReplaceAll(out, "}", "")
out = strings.ReplaceAll(out, "[", "")
out = strings.ReplaceAll(out, "]", "")
out = strings.ReplaceAll(out, ",", "")
out = strings.ReplaceAll(out, "\"", "")

ret := ""
for _, line := range strings.Split(out, "\n") {
line = strings.TrimRight(line, " \n")
if len(line) == 0 {
continue
}
ret += line + "\n"
}

return ret
}

func showParameters(ctx context.Context, height int64, cons consensus.ClientBackend) {
checkErr := func(what string, err error) {
if err != nil {
Expand Down Expand Up @@ -456,7 +432,7 @@ func showParameters(ctx context.Context, height int64, cons consensus.ClientBack
doc[name] = params
} else {
fmt.Printf("=== %s PARAMETERS ===\n", strings.ToUpper(name))
out := PrettifyFromJSON(params)
out := common.PrettifyFromJSON(params)
fmt.Printf("%s\n", out)
}
}
Expand Down
Loading

0 comments on commit 025ea13

Please sign in to comment.