1

Spf13/cobra command offers a number of elegant tools to provide feedback to the user. I have more experience using Python/headless service where the standard is to use logging libraries and then redirect to stdio if necessary.

However, the more I’ve been exploring cobra, it feels like this is the wrong path. Instead it feels like I should send everything through cobra, and pick and choose from that buffer whatever should go to logging.

Is there any idiomatic guidance here?

aronchick
  • 6,786
  • 9
  • 48
  • 75

1 Answers1

3

I would suggest to use methods provided from cobra.Command for messages that are intended to be read by users.

Logs are usually used to show/save messages that will be read by developers (in this case, you) or if users explicitly want to read the logs.

With that reasoning, you can actually use both of them. For example, you can perform

  • c.Println("<success message>") to tell users that the command success, and
  • Debug/Info/Error logs in your CLI app which will be displayed (or saved in a logfile) if user pass --verbose flag to your app.
wijayaerick
  • 703
  • 6
  • 12