-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add support for filtering the output of get
This is done with the help of the text/template package. One example to change the state of all ports except port6 to state CONF: {{ $state := "CONF" }}{ "ports": { {{$p := prefix ", "}}{{- range $key, $val := .ports -}}{{ if ne $key "port6" }}{{call $p}}"{{- $key -}}":{"state": "{{ $state }}"}{{end}}{{- end }} } } There are two custom functions toJSON and prefix
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,39 @@ Copyright © 2023 Christian Ege <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"text/template" | ||
|
||
"github.com/graugans/go-ovp8xx/v2/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// toJSON converts the given object to a JSON string representation. | ||
// If an error occurs during marshaling, an empty string is returned. | ||
// This is taken from https://github.com/intel/tfortools | ||
func toJSON(obj interface{}) string { | ||
b, err := json.MarshalIndent(obj, "", "\t") | ||
if err != nil { | ||
return "" | ||
} | ||
return string(b) | ||
} | ||
|
||
// prefix can be used to create a list separated by s and the very first | ||
// element is not prefixed. | ||
func prefix(s string) func() string { | ||
i := -1 | ||
return func() string { | ||
i++ | ||
if i == 0 { | ||
return "" | ||
} | ||
return s | ||
} | ||
} | ||
|
||
func getCommand(cmd *cobra.Command, args []string) error { | ||
var result ovp8xx.Config | ||
var err error | ||
|
@@ -23,6 +52,31 @@ func getCommand(cmd *cobra.Command, args []string) error { | |
if result, err = o3r.Get(helper.jsonPointers()); err != nil { | ||
return err | ||
} | ||
|
||
if cmd.Flags().Changed("format") { | ||
format, err := cmd.Flags().GetString("format") | ||
if err != nil { | ||
return fmt.Errorf("unable to get the format string from the command line: %w", err) | ||
} | ||
var inputData interface{} | ||
if err = json.Unmarshal([]byte(result.String()), &inputData); err != nil { | ||
return fmt.Errorf("unable to unmarshal the JSON data from the 'get' call: %w", err) | ||
} | ||
templateFunctions := template.FuncMap{ | ||
"toJSON": toJSON, | ||
"prefix": prefix, | ||
} | ||
tmpl, err := template.New("output").Funcs(templateFunctions).Parse(format) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse the template: %w", err) | ||
} | ||
|
||
if err := tmpl.Execute(os.Stdout, inputData); err != nil { | ||
return fmt.Errorf("unable to execute the template: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
if err := helper.printJSONResult(result.String()); err != nil { | ||
return err | ||
} | ||
|
@@ -66,4 +120,5 @@ func init() { | |
rootCmd.AddCommand(getCmd) | ||
getCmd.Flags().StringSliceP("pointer", "p", []string{""}, "A JSON pointer to be queried") | ||
getCmd.Flags().Bool("pretty", false, "Pretty print the JSON received from the device") | ||
getCmd.Flags().String("format", "", "Specify an alternative format for the JSON output") | ||
} |