Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
matevz committed Nov 15, 2024
1 parent 911db13 commit 4d81d79
Showing 7 changed files with 450 additions and 210 deletions.
37 changes: 37 additions & 0 deletions cmd/common/json.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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 {
@@ -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)
}
}
Loading

0 comments on commit 4d81d79

Please sign in to comment.