Skip to content

Commit

Permalink
feature: add support for filtering the output of get
Browse files Browse the repository at this point in the history
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
graugans committed May 14, 2024
1 parent 141c0c0 commit ca35c8e
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/ovp8xx/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
}

0 comments on commit ca35c8e

Please sign in to comment.