Cobra

Using tabwriter to improve on cobra’s help information

2020-02-27

If you’ve been following the blog, you know that I’m a fan of cobra as a CLI library. Let me share how I use tabwriter to compliment cobra’s autogenerated help information. For reference, you can check this post as well. The following code is a copy (not an exact copy) of one of the tools that I use at work. It will look something like this. Trigger a manual run, among other tools. Format for the –input flag: payer:<id>[/link1,link2,…] run manual calculation at payer level, optional link acct list is for log filtering msp:<id> run manual calculation at MSP level link:<id> run manual calculation at link acct level detectri:<msp|payer>[:id] run RI detection manually detectsp:<msp|payer>[:id] run SavingsPlan detection manually curlinks:<msp|payer>[:id] detect actual customers from CUR, can use –detect-moved-payer flag fees:<id> detect fees for the input link id detecttags:<id> detect tags for the input payer id Notes: When triggering CUR (using trigger: input), it's recommended to use the –use-sns flag. Usage: linkbatchd manualrun [flags] Flags: –input string see subcommand description for more details –date string date to trigger in UTC, fmt: yyyy-mm-dd, yyyy-mm-dd,yyyy-mm-dd for date range (from,to) –invoice-type string type of invoice to calculate: 'account' or 'cross_tag', used in trigger: input

Cobra · Golang · Tabwriter

1 minute

Using k8s.io/klog together with cobra in golang

2019-02-05

This post is somehow related to a previous article about using glog together with cobra. This time, we will be using klog which is a Kubernetes fork of glog. # run the -h command $ ./cobraklog -h Usage of ./cobraklog: -alsologtostderr log to standard error as well as files -log_backtrace_at value when logging hits line file:N, emit a stack trace -log_dir string If non-empty, write log files in this directory -log_file string If non-empty, use this log file -logtostderr log to standard error instead of files -skip_headers If true, avoid header prefixes in the log messages -stderrthreshold value logs at or above this threshold go to stderr (default 2) -v value log level for V logs -vmodule value comma-separated list of pattern=N settings for file-filtered logging # run cobra's `help` command $ ./cobraklog help Use klog together with cobra. Usage: cobraklog [command] Available Commands: help Help about any command run run command Flags: --alsologtostderr log to standard error as well as files --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) --log_dir string If non-empty, write log files in this directory --log_file string If non-empty, use this log file --logtostderr log to standard error instead of files --skip_headers If true, avoid header prefixes in the log messages --stderrthreshold severity logs at or above this threshold go to stderr (default 2) -v, --v Level log level for V logs --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging -h, --help help for cobraklog Use "cobraklog [command] --help" for more information about a command. # run `help` on our subcommand `run` $ ./cobraklog help run Run command. Usage: cobraklog run [flags] Flags: --str string string to print (default "hello world") -h, --help help for run Global Flags: --alsologtostderr log to standard error as well as files --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) --log_dir string If non-empty, write log files in this directory --log_file string If non-empty, use this log file --logtostderr log to standard error instead of files --skip_headers If true, avoid header prefixes in the log messages --stderrthreshold severity logs at or above this threshold go to stderr (default 2) -v, --v Level log level for V logs --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging # run the `run` subcommand $ ./cobraklog run --logtostderr I0205 16:37:39.110849 13672 main.go:41] echo=hello world $ ./cobraklog run --logtostderr --str "hello alien world" I0205 16:39:37.212187 13685 main.go:41] echo=hello alien world

Cobra · Golang · Klog

2 minutes

Using glog together with cobra in golang

2017-12-01

If you have been programming with golang, you’ve probably heard of cobra. I use it extensively at work and also in my personal projects. Recently though, I’ve been using glog more and more. And I quite like it. The thing is, it has a couple of flag definitions in its init() function using golang’s builtin flag library. And I wanted to include those flags into cobra’s flag definitions. This is how I did it.

Cobra · Glog · Golang

2 minutes