Skip to content

Commit

Permalink
feat: add a pretty flag on some commands
Browse files Browse the repository at this point in the history
This will simplify things on windows when no jq can not be installed
that easy.

Signed-off-by: Christian Ege <[email protected]>
  • Loading branch information
graugans committed Dec 6, 2023
1 parent c9eb4f4 commit ac6aeec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
22 changes: 9 additions & 13 deletions cmd/ovp8xx/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,27 @@ Copyright © 2023 Christian Ege <[email protected]>
package cmd

import (
"fmt"

"github.com/graugans/go-ovp8xx/pkg/ovp8xx"
"github.com/spf13/cobra"
)

func getCommand(cmd *cobra.Command, args []string) error {

pointers, err := cmd.Flags().GetStringSlice("pointer")
if err != nil {
return err
}
host, err := rootCmd.PersistentFlags().GetString("ip")
var result ovp8xx.Config
var err error
helper, err := NewHelper(cmd)

Check failure on line 14 in cmd/ovp8xx/cmd/get.go

View workflow job for this annotation

GitHub Actions / build

undefined: NewHelper

Check failure on line 14 in cmd/ovp8xx/cmd/get.go

View workflow job for this annotation

GitHub Actions / lint

undefined: NewHelper

Check failure on line 14 in cmd/ovp8xx/cmd/get.go

View workflow job for this annotation

GitHub Actions / lint

undefined: NewHelper
if err != nil {
return err
}

o3r := ovp8xx.NewClient(
ovp8xx.WithHost(host),
ovp8xx.WithHost(helper.hostname()),
)

result, err := o3r.Get(pointers)
if err != nil {
if result, err = o3r.Get(helper.jsonPointers()); err != nil {
return err
}
if err := helper.printJSONResult(result.String()); err != nil {
return err
} else {
fmt.Printf("%s\n", result)
}
return nil
}
Expand Down Expand Up @@ -70,4 +65,5 @@ NOTE: This command only modifies temporary data, any changes will be lost after
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")
}
22 changes: 9 additions & 13 deletions cmd/ovp8xx/cmd/getSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,27 @@ Copyright © 2023 Christian Ege <[email protected]>
package cmd

import (
"fmt"

"github.com/graugans/go-ovp8xx/pkg/ovp8xx"
"github.com/spf13/cobra"
)

func getSchemaCommand(cmd *cobra.Command, args []string) error {

pointers, err := cmd.Flags().GetStringSlice("pointer")
var result string
var err error
helper, err := NewHelper(cmd)

Check failure on line 14 in cmd/ovp8xx/cmd/getSchema.go

View workflow job for this annotation

GitHub Actions / build

undefined: NewHelper

Check failure on line 14 in cmd/ovp8xx/cmd/getSchema.go

View workflow job for this annotation

GitHub Actions / lint

undefined: NewHelper

Check failure on line 14 in cmd/ovp8xx/cmd/getSchema.go

View workflow job for this annotation

GitHub Actions / lint

undefined: NewHelper
if err != nil {
return err
}
o3r := ovp8xx.NewClient(
ovp8xx.WithHost(helper.hostname()),
)

host, err := rootCmd.PersistentFlags().GetString("ip")
if err != nil {
if result, err = o3r.GetSchema(helper.jsonPointers()); err != nil {
return err
}

o3r := ovp8xx.NewClient(
ovp8xx.WithHost(host),
)

if result, err := o3r.GetSchema(pointers); err != nil {
if err := helper.printJSONResult(result); err != nil {
return err
} else {
fmt.Printf("%s\n", result)
}
return nil
}
Expand Down Expand Up @@ -89,4 +84,5 @@ When no query is provided the complete schema is returend.
func init() {
rootCmd.AddCommand(getSchemaCmd)
getSchemaCmd.Flags().StringSliceP("pointer", "p", []string{""}, "A JSON pointer to be queried. This can be used multiple times")
getSchemaCmd.Flags().Bool("pretty", false, "Pretty print the JSON received from the device")
}
15 changes: 7 additions & 8 deletions cmd/ovp8xx/cmd/getinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@ Copyright © 2023 Christian Ege <[email protected]>
package cmd

import (
"fmt"

"github.com/graugans/go-ovp8xx/pkg/ovp8xx"
"github.com/spf13/cobra"
)

func getInitCommand(cmd *cobra.Command, args []string) error {

host, err := rootCmd.PersistentFlags().GetString("ip")
var result ovp8xx.Config
var err error
helper, err := NewHelper(cmd)

Check failure on line 14 in cmd/ovp8xx/cmd/getinit.go

View workflow job for this annotation

GitHub Actions / build

undefined: NewHelper

Check failure on line 14 in cmd/ovp8xx/cmd/getinit.go

View workflow job for this annotation

GitHub Actions / lint

undefined: NewHelper (typecheck)

Check failure on line 14 in cmd/ovp8xx/cmd/getinit.go

View workflow job for this annotation

GitHub Actions / lint

undefined: NewHelper) (typecheck)
if err != nil {
return err
}

o3r := ovp8xx.NewClient(
ovp8xx.WithHost(host),
ovp8xx.WithHost(helper.hostname()),
)

if result, err := o3r.GetInit(); err != nil {
if result, err = o3r.GetInit(); err != nil {
return err
} else {
fmt.Printf("%s\n", result)
}
helper.printJSONResult(result.String())
return nil
}

Expand All @@ -42,4 +40,5 @@ longer useable when the expectation from the safed configuration is no longer me

func init() {
rootCmd.AddCommand(getInitCmd)
getInitCmd.Flags().Bool("pretty", false, "Pretty print the JSON received from the device")
}

0 comments on commit ac6aeec

Please sign in to comment.