jsattler / go-comdirect

Go module and CLI tool to interact with the comdirect REST API
Apache License 2.0
44 stars 9 forks source link

Templated output for CSV #22

Open eblechschmidt opened 1 year ago

eblechschmidt commented 1 year ago

Hello Joshua,

thank you for this awesome tool. I would be interested to have a more flexible way of changeing the content of the CSV ouput (for e.g. adding the remittance info). Would you be interested in a PR using go template to format the output?

I could imaging two possbile solutions to this:

  1. CSV foramting completely determinde by template. For transactions the template could look something like:

    REMITTER,DEPTOR,BOOKING DATE,STATUS,TYPE,VALUE,UNIT
    {{range .Values}}{{.Creditor.HolderName}},{{.BookingDate}},{{t.BookingStatus}},{{t.TransactionType.Text}},{{.Amount | formatAmountValue}},{{t.Amount.Unit}}
    {{end}}

    In this case I would embed a template file which keeps the current setup and I would add an additional flag for specifying a custom template file path.

  2. Separate the header and the row into two templates:

    header_tpl := "REMITTER,DEPTOR,BOOKING DATE,STATUS,TYPE,VALUE,UNIT"
    row_tpl := "{{.Creditor.HolderName}},{{.BookingDate}},{{t.BookingStatus}},{{t.TransactionType.Text}},{{.Amount | formatAmountValue}},{{t.Amount.Unit}}"

In this case I would add two flags (one for the header and one for the row template) which would override the default formating.

Which of the solutions would you prefer? I don't really care.

Best regards

Eike

jsattler commented 1 year ago

Hi @eblechschmidt, thanks for reaching out - I like your idea. I have to admit that I'm not that experienced in using go templates, so I find it hard to choose between those two options. It is not clear for me what either option would mean for the user of the CLI?

In general, I would like to keep the CLI as simple as possible. If you think that adding this feature does not increase the complexity too much, feel free to open a PR!

onjen commented 1 year ago

I would be very interested in such a feature. Option 1 sounds good to me! If you need someone for testing, let me know :)

eblechschmidt commented 1 year ago

@onjen do you have go installed? Then you could clone https://github.com/eblechschmidt/go-comdirect and test it. So far I have just implemented it for the transaction command but will finalize it for the other commands as well soon.

eblechschmidt commented 1 year ago

@jsattler I just created a pull request and checked the output for all commands to you version via e.g. comdirect report -f csv | diff -u - <(go run main.go report -f csv). I only escaped the names in the transaction command output with double quotes because they could contain comma.

One thing that is worth discussing that so far the length of holderName is limited to 30 (which I implemented for the templated version as well). For me that makes sense for the table output but I think it is not the optimal default for CSV output. What do you think? https://github.com/jsattler/go-comdirect/blob/f95a8a1e3db6aa38d019696b7acc877beab1aaf4/comdirect/cmd/transaction.go#L119-L123