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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rootCmd = &cobra.Command{ | |
Use: "manualrun", | |
Short: "trigger a manual run", | |
Long: func() string { | |
var b bytes.Buffer | |
wb := bufio.NewWriter(&b) | |
h := tabwriter.NewWriter(wb, 0, 10, 5, ' ', 0) | |
fmt.Fprintf(h, "Trigger a manual run, among other tools.\n\n") | |
fmt.Fprintf(h, "Format for the --input flag:\n") | |
fmt.Fprintf(h, " payer:<id>[/link1,link2,...]\trun manual calculation at payer level, optional acct list is for log filtering\n") | |
fmt.Fprintf(h, " msp:<id>\trun manual calculation at MSP level\n") | |
fmt.Fprintf(h, " link:<id>\trun manual calculation at link acct level\n") | |
fmt.Fprintf(h, " detectri:<msp|payer>[:id]\trun RI detection manually\n") | |
fmt.Fprintf(h, " detectsp:<msp|payer>[:id]\trun SavingsPlan detection manually\n") | |
fmt.Fprintf(h, " curlinks:<msp|payer>[:id]\tdetect actual customers from CUR, can use --detect-moved-payer flag\n") | |
fmt.Fprintf(h, " fees:<id>\tdetect fees for the input link id\n") | |
fmt.Fprintf(h, " detecttags:<id>\tdetect tags for the input payer id\n\n") | |
fmt.Fprintf(h, "Notes:\n") | |
fmt.Fprintf(h, " * When triggering CUR (using trigger: input), it's recommended to use the --use-sns flag.\n\n") | |
h.Flush() | |
wb.Flush() | |
return b.String() | |
}(), | |
RunE: func(cmd *cobra.Command, args []string) error { | |
defer func(begin time.Time) { | |
log.Println("duration:", time.Since(begin)) | |
}(time.Now()) | |
// redacted | |
return nil | |
}, | |
} |
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