Closed WashingtonKK closed 2 months ago
At logs, we directly printing to
os.stdout
andos.stderr
, Actually, cobra should take care to redirecting to out or errorSo We need to modify the log to like below
func logError( cmd *cobra.Command , err error) { boldRed := color.New(color.FgRed, color.Bold) boldRed.Fprintf(cmd.OutOrStderr(, "\nerror: ") fmt.Fprintf(cmd.OutOrStderr(), "%s\n\n", color.RedString(err.Error())) }
Please modify the log functions like above and test
if it works , then create a
custom cobra.Command
, like belowtype cmd struct { *cobra.Command } func (c *cmd) logJSON(iList ...interface{}) { for _, i := range iList { m, err := json.Marshal(i) if err != nil { c.logError(err) return } pj, err := prettyjson.Format(m) if err != nil { c.logError(err) return } fmt.Fprintf(c.OutOrStdout(), "\n%s\n\n", string(pj)) } } func (c *cmd) logUsage(u string) { fmt.Fprintf(c.OutOrStdout(), color.YellowString("\nusage: %s\n\n"), u) } func (c *cmd) logError(err error) { boldRed := color.New(color.FgRed, color.Bold) boldRed.Fprintf(c.OutOrStderr(), "\nerror: ") fmt.Fprintf(c.OutOrStderr(), "%s\n\n", color.RedString(err.Error())) }
@arvindh123 , this implementation does not work, and coverage is still not generated.
At logs, we directly printing to
os.stdout
andos.stderr
, Actually, cobra should take care to redirecting to out or error So We need to modify the log to like belowfunc logError( cmd *cobra.Command , err error) { boldRed := color.New(color.FgRed, color.Bold) boldRed.Fprintf(cmd.OutOrStderr(, "\nerror: ") fmt.Fprintf(cmd.OutOrStderr(), "%s\n\n", color.RedString(err.Error())) }
Please modify the log functions like above and test if it works , then create a
custom cobra.Command
, like belowtype cmd struct { *cobra.Command } func (c *cmd) logJSON(iList ...interface{}) { for _, i := range iList { m, err := json.Marshal(i) if err != nil { c.logError(err) return } pj, err := prettyjson.Format(m) if err != nil { c.logError(err) return } fmt.Fprintf(c.OutOrStdout(), "\n%s\n\n", string(pj)) } } func (c *cmd) logUsage(u string) { fmt.Fprintf(c.OutOrStdout(), color.YellowString("\nusage: %s\n\n"), u) } func (c *cmd) logError(err error) { boldRed := color.New(color.FgRed, color.Bold) boldRed.Fprintf(c.OutOrStderr(), "\nerror: ") fmt.Fprintf(c.OutOrStderr(), "%s\n\n", color.RedString(err.Error())) }
@arvindh123 , this implementation does not work, and coverage is still not generated.
I suppose this is because the function OutOrStdout
simply returns the writer that is set using SetOut
and SerErr
and uses it to write all output which still does not solve our issue.
Closing as duplicate of https://github.com/absmach/magistrala/pull/2319.
What type of PR is this?
This is an enhancement because it adds cli tests for users.
What does this do?
This adds tests for cli, to check for invalid args input, and logging of output.
Which issue(s) does this PR fix/relate to?
Have you included tests for your changes?
Yes, I have included tests for my changes.
Did you document any new/modified feature?
No, I have not updated the documentation because there are no new features, just an increase in scope of the existing tests to cover CLI.
Notes
This is the first PR in this series, to be merged first.