Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: support outputTemplate flags for non-request based commands #369

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cmd/activitylog/list/list.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (n *CmdList) RunE(cmd *cobra.Command, args []string) error {
cfg.Logger.Debugf("activity log filter: path=%s, host=%s, datefrom=%s, dateto=%s", activitylog.GetPath(), filter.Host, filter.DateFrom, filter.DateTo)

err = activitylog.GetLogEntries(filter, func(line []byte) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", line)
return n.factory.WriteOutputWithoutPropertyGuess(line, cmdutil.OutputContext{})
})

return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/alarms/subscribe/subscribe.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (n *CmdSubscribe) RunE(cmd *cobra.Command, args []string) error {
MaxMessages: n.flagCount,
ActionTypes: n.actionTypes,
OnMessage: func(msg string) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(msg))
return n.factory.WriteOutputWithoutPropertyGuess([]byte(msg), cmdutil.OutputContext{})
},
}
return c8ysubscribe.Subscribe(client, log, c8y.RealtimeAlarms(device), opts)
Expand Down
22 changes: 5 additions & 17 deletions pkg/cmd/assert/text/text.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/flags"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/iterator"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/jsonUtilities"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -79,11 +78,6 @@ func (n *CmdText) RunE(cmd *cobra.Command, args []string) error {
return err
}

consol, err := n.factory.Console()
if err != nil {
return err
}

inputIterators, err := cmdutil.NewRequestInputIterators(cmd, cfg)
if err != nil {
return err
Expand Down Expand Up @@ -132,12 +126,8 @@ func (n *CmdText) RunE(cmd *cobra.Command, args []string) error {
}
}

writeOutput := func(isJSON bool, output []byte) {
if isJSON {
_ = n.factory.WriteJSONToConsole(cfg, cmd, "", output)
} else {
fmt.Fprintf(consol, "%s\n", output)
}
writeOutput := func(output []byte) {
_ = n.factory.WriteOutputWithoutPropertyGuess(output, cmdutil.OutputContext{})
}

totalErrors := 0
Expand All @@ -155,16 +145,14 @@ func (n *CmdText) RunE(cmd *cobra.Command, args []string) error {
return cmderrors.NewUserErrorWithExitCode(cmderrors.ExitAbortedWithErrors, msg)
}

isJSON := jsonUtilities.IsJSONObject(input)

// schema match
if schema != nil {
var val interface{}
_ = json.Unmarshal(input, &val)
err = schema.Validate(val)

if err == nil {
writeOutput(isJSON, input)
writeOutput(input)
} else {
err = fmt.Errorf("%w. input does not match json schema. got=%s, wanted=%s", cmderrors.ErrAssertion, input, n.schema)
}
Expand All @@ -176,7 +164,7 @@ func (n *CmdText) RunE(cmd *cobra.Command, args []string) error {
if !pattern.Match(input) {
err = fmt.Errorf("%w. input does not match pattern. got=%s, wanted=%s", cmderrors.ErrAssertion, input, n.regex)
} else {
writeOutput(isJSON, input)
writeOutput(input)
}
}
}
Expand All @@ -187,7 +175,7 @@ func (n *CmdText) RunE(cmd *cobra.Command, args []string) error {
if !bytes.Equal(input, []byte(n.exact)) {
err = fmt.Errorf("%w. input does not match. got=%s, wanted=%s", cmderrors.ErrAssertion, input, n.exact)
} else {
writeOutput(isJSON, input)
writeOutput(input)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ func Initialize() (*root.CmdRoot, error) {

rootCmd := root.NewCmdRoot(cmdFactory, buildVersion, "")

// Add reference to root command
cmdFactory.SetCommand(rootCmd.Command)

tableOptions := &console.TableOptions{
MinColumnWidth: configHandler.ViewColumnMinWidth(),
MaxColumnWidth: configHandler.ViewColumnMaxWidth(),
Expand Down
23 changes: 2 additions & 21 deletions pkg/cmd/devices/assert/factory/factory.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/reubenmiller/go-c8y-cli/v2/pkg/completion"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/desiredstate"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/flags"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/jsonUtilities"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -48,10 +47,6 @@ func NewAssertCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateChecker)
if err != nil {
return err
}
consol, err := f.Console()
if err != nil {
return err
}

inputIterators, err := cmdutil.NewRequestInputIterators(cmd, cfg)
if err != nil {
Expand Down Expand Up @@ -111,12 +106,7 @@ func NewAssertCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateChecker)

if err == nil {
outValue := h.GetValue(result, input)

if jsonUtilities.IsJSONObject(outValue) {
_ = f.WriteJSONToConsole(cfg, cmd, "", outValue)
} else {
fmt.Fprintf(consol, "%s\n", outValue)
}
_ = f.WriteOutputWithoutPropertyGuess(outValue, cmdutil.OutputContext{})
}

if err != nil {
Expand Down Expand Up @@ -167,10 +157,6 @@ func NewAssertDeviceCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateCh
if err != nil {
return err
}
consol, err := f.Console()
if err != nil {
return err
}

inputIterators, err := cmdutil.NewRequestInputIterators(cmd, cfg)
if err != nil {
Expand Down Expand Up @@ -230,12 +216,7 @@ func NewAssertDeviceCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateCh

if err == nil {
outValue := h.GetValue(result, input)

if jsonUtilities.IsJSONObject(outValue) {
_ = f.WriteJSONToConsole(cfg, cmd, "", outValue)
} else {
fmt.Fprintf(consol, "%s\n", outValue)
}
_ = f.WriteOutputWithoutPropertyGuess(outValue, cmdutil.OutputContext{})
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/events/subscribe/subscribe.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (n *CmdSubscribe) RunE(cmd *cobra.Command, args []string) error {
MaxMessages: n.flagCount,
ActionTypes: n.actionTypes,
OnMessage: func(msg string) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(msg))
return n.factory.WriteOutputWithoutPropertyGuess([]byte(msg), cmdutil.OutputContext{})
},
}
return c8ysubscribe.Subscribe(client, log, c8y.RealtimeEvents(device), opts)
Expand Down
7 changes: 1 addition & 6 deletions pkg/cmd/extension/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
if len(cmds) == 0 {
return cmderrors.NewSystemError("no installed extensions found")
}
cfg, err := f.Config()

if err != nil {
return err
}

for _, c := range cmds {
ext := map[string]interface{}{}
Expand All @@ -88,7 +83,7 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
return cmderrors.NewUserError("Settings error. ", err)
}

if err := f.WriteJSONToConsole(cfg, cmd, "", rowText); err != nil {
if err := f.WriteOutputWithoutPropertyGuess(rowText, cmdutil.OutputContext{}); err != nil {
return cmderrors.NewSystemError("Failed to write to console. ", err)
}
}
Expand Down
23 changes: 2 additions & 21 deletions pkg/cmd/inventory/assert/factory/factory.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/reubenmiller/go-c8y-cli/v2/pkg/completion"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/desiredstate"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/flags"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/jsonUtilities"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -47,10 +46,6 @@ func NewAssertCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateChecker)
if err != nil {
return err
}
consol, err := f.Console()
if err != nil {
return err
}

inputIterators, err := cmdutil.NewRequestInputIterators(cmd, cfg)
if err != nil {
Expand Down Expand Up @@ -113,12 +108,7 @@ func NewAssertCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateChecker)
result, err = desiredstate.WaitForWithRetries(retries, interval, duration, state)
if err == nil {
outValue := h.GetValue(result, input)

if jsonUtilities.IsJSONObject(outValue) {
_ = f.WriteJSONToConsole(cfg, cmd, "", outValue)
} else {
fmt.Fprintf(consol, "%s\n", outValue)
}
_ = f.WriteOutputWithoutPropertyGuess(outValue, cmdutil.OutputContext{})
}
} else {
err = inputErr
Expand Down Expand Up @@ -176,10 +166,6 @@ func NewAssertDeviceCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateCh
if err != nil {
return err
}
consol, err := f.Console()
if err != nil {
return err
}

inputIterators, err := cmdutil.NewRequestInputIterators(cmd, cfg)
if err != nil {
Expand Down Expand Up @@ -242,12 +228,7 @@ func NewAssertDeviceCmdFactory(cmd *cobra.Command, f *cmdutil.Factory, h StateCh
result, err = desiredstate.WaitForWithRetries(retries, interval, duration, state)
if err == nil {
outValue := h.GetValue(result, input)

if jsonUtilities.IsJSONObject(outValue) {
_ = f.WriteJSONToConsole(cfg, cmd, "", outValue)
} else {
fmt.Fprintf(consol, "%s\n", outValue)
}
_ = f.WriteOutputWithoutPropertyGuess(outValue, cmdutil.OutputContext{})
}
} else {
err = inputErr
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/inventory/subscribe/subscribe.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (n *CmdSubscribe) RunE(cmd *cobra.Command, args []string) error {
MaxMessages: n.flagCount,
ActionTypes: n.actionTypes,
OnMessage: func(msg string) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(msg))
return n.factory.WriteOutputWithoutPropertyGuess([]byte(msg), cmdutil.OutputContext{})
},
}
return c8ysubscribe.Subscribe(client, log, c8y.RealtimeManagedObjects(device), opts)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/inventory/wait/wait.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (n *CmdWait) RunE(cmd *cobra.Command, args []string) error {
result, err := desiredstate.WaitFor(interval, duration, state)

if v, ok := result.(*c8y.ManagedObject); ok {
_ = n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(v.Item.Raw))
_ = n.factory.WriteOutputWithoutPropertyGuess([]byte(v.Item.Raw), cmdutil.OutputContext{})
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/measurements/subscribe/subscribe.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (n *CmdSubscribe) RunE(cmd *cobra.Command, args []string) error {
MaxMessages: n.flagCount,
ActionTypes: n.actionTypes,
OnMessage: func(msg string) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(msg))
return n.factory.WriteOutputWithoutPropertyGuess([]byte(msg), cmdutil.OutputContext{})
},
}
return c8ysubscribe.Subscribe(client, log, c8y.RealtimeMeasurements(device), opts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (n *SubscribeCmd) RunE(cmd *cobra.Command, args []string) error {
}

if isMatch {
if err := n.factory.WriteJSONToConsole(cfg, cmd, "", msg.Payload); err != nil {
if err := n.factory.WriteOutputWithoutPropertyGuess(msg.Payload, cmdutil.OutputContext{}); err != nil {
cfg.Logger.Warnf("Could not process line. only json lines are accepted. %s", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/operations/subscribe/subscribe.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (n *CmdSubscribe) RunE(cmd *cobra.Command, args []string) error {
MaxMessages: n.flagCount,
ActionTypes: n.actionTypes,
OnMessage: func(msg string) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(msg))
return n.factory.WriteOutputWithoutPropertyGuess([]byte(msg), cmdutil.OutputContext{})
},
}
return c8ysubscribe.Subscribe(client, log, c8y.RealtimeOperations(device), opts)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/operations/wait/wait.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ func (n *CmdWait) RunE(cmd *cobra.Command, args []string) error {
if v.FailureReason != "" {
cfg.Logger.Warnf("Failure reason: %s", v.FailureReason)
}
_ = n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(v.Item.Raw))

_ = n.factory.WriteOutputWithoutPropertyGuess([]byte(v.Item.Raw), cmdutil.OutputContext{})
}

if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions pkg/cmd/realtime/subscribe/subscribe.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ func (n *CmdSubscribe) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
cfg, err := n.factory.Config()
if err != nil {
return err
}
log, err := n.factory.Logger()
if err != nil {
return err
Expand All @@ -87,7 +83,7 @@ func (n *CmdSubscribe) RunE(cmd *cobra.Command, args []string) error {
MaxMessages: n.flagCount,
ActionTypes: n.actionTypes,
OnMessage: func(msg string) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(msg))
return n.factory.WriteOutputWithoutPropertyGuess([]byte(msg), cmdutil.OutputContext{})
},
}
return c8ysubscribe.Subscribe(client, log, n.flagChannel, opts)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/realtime/subscribeall/subscribeall.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (n *CmdSubscribeAll) RunE(cmd *cobra.Command, args []string) error {
MaxMessages: n.flagCount,
ActionTypes: n.actionTypes,
OnMessage: func(msg string) error {
return n.factory.WriteJSONToConsole(cfg, cmd, "", []byte(msg))
return n.factory.WriteOutputWithoutPropertyGuess([]byte(msg), cmdutil.OutputContext{})
},
}
return c8ysubscribe.SubscribeMultiple(client, log, patterns, opts)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/sessions/get/get.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ func (n *CmdGetSession) RunE(cmd *cobra.Command, args []string) error {
return err
}

return n.factory.WriteJSONToConsole(cfg, cmd, "", b)
return n.factory.WriteOutputWithoutPropertyGuess(b, cmdutil.OutputContext{})
}
7 changes: 6 additions & 1 deletion pkg/cmd/settings/list/list.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,10 @@ func (n *CmdList) listSettings(cmd *cobra.Command, args []string) error {
return cmderrors.NewUserError("Settings error. ", err)
}

return n.factory.WriteJSONToConsole(cfg, cmd, settingsPrefix, responseText)
commonOptions, err := cfg.GetOutputCommonOptions(cmd)
if err != nil {
return err
}
commonOptions.ResultProperty = settingsPrefix
return n.factory.WriteOutput(responseText, cmdutil.OutputContext{}, &commonOptions)
}
Loading
Loading