From 2a4b7f4cd36aadd3448a303e6bcf1a703ad575d8 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 20 May 2026 17:21:49 -0400 Subject: cli cost: print more information --- cmd/lulu/cost.go | 20 ++++++++++++++++++-- cmd/lulu/test | 18 ++++++++++++------ cmd/lulu/testchecks/cost | 20 +++++++++++++++++++- cmd/lulu/testchecks/jobs | 2 ++ cmd/lulu/testchecks/orders | 2 -- cmd/lulu/tests/jobs | 1 + cmd/lulu/tests/orders | 1 - 7 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 cmd/lulu/testchecks/jobs delete mode 100644 cmd/lulu/testchecks/orders create mode 100644 cmd/lulu/tests/jobs delete mode 100644 cmd/lulu/tests/orders (limited to 'cmd') diff --git a/cmd/lulu/cost.go b/cmd/lulu/cost.go index 8599340..93c3ab4 100644 --- a/cmd/lulu/cost.go +++ b/cmd/lulu/cost.go @@ -3,8 +3,10 @@ package main import ( "encoding/json" "fmt" + "os" "regexp" "strconv" + "text/tabwriter" "github.com/alecthomas/kong" @@ -38,8 +40,21 @@ func (cmd *CostCmd) Run(cli *kong.Kong, clnt *lulu.Client) error { if err != nil { return err } - // TODO: -v flag to print everything - fmt.Printf("total: %s %s\n", cost.TotalCostInclTax, cost.Currency) + + w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) + for _, fee := range cost.Fees { + fmt.Fprintf(w, "fee %s %s:\t%s %s\t\n", fee.Type, fee.Sku, fee.TotalCostExclTax, fee.Currency) + } + for i, item := range cost.LineItemCosts { + fmt.Fprintf(w, "item %d:\t%s %s\t\n", i, item.TotalCostExclTax, cost.Currency) + } + fmt.Fprintf(w, "shipping:\t%s %s\t\n", cost.ShipCost.TotalCostExclTax, cost.Currency) + fmt.Fprintf(w, "fulfillment:\t%s %s\t\n", cost.FulfillmentCost.TotalCostExclTax, cost.Currency) + fmt.Fprintf(w, "discount:\t%s %s\t\n", cost.TotalDiscount, cost.Currency) + fmt.Fprintf(w, "tax:\t%s %s\t\n", cost.TotalTax, cost.Currency) + fmt.Fprintf(w, "total:\t%s %s\t\n", cost.TotalCostInclTax, cost.Currency) + w.Flush() + if len(addrVal.Warnings) > 0 { j, err := json.Marshal(addrVal) if err != nil { @@ -47,6 +62,7 @@ func (cmd *CostCmd) Run(cli *kong.Kong, clnt *lulu.Client) error { } cli.Errorf("%s\n", j) } + return nil } diff --git a/cmd/lulu/test b/cmd/lulu/test index 8c29a2f..e446356 100755 --- a/cmd/lulu/test +++ b/cmd/lulu/test @@ -2,10 +2,13 @@ set -u +flags="-e -u" + [[ ! -d testout ]] && mkdir testout [[ ! -d testerr ]] && mkdir testerr -for test in tests/* +[[ $# -gt 0 ]] && tests="tests/$1" || tests=tests/* +for test in $tests do echo $test... base=${test##*/} @@ -14,7 +17,7 @@ do out=testout/$base err=testerr/$base if [ -f $want ]; then # check against expected output - sh $test >$out 2>$err + sh ${flags} $test >$out 2>$err if ! cmp -s $want $out then echo "FAIL $test: Actual" @@ -22,19 +25,22 @@ do exit 1 fi elif [ -f $check ]; then # use script to check output - sh $test >$out 2>$err + sh ${flags} $test >$out 2>$err echo "checking with $check..." >>$err - if ! sh $check $out >>$err + if ! sh ${flags} $check $out >>$err then echo "FAIL $test: check failed: $check" exit 1 fi - else # test should fail - if sh $test >$out 2>$err + elif [ -f $test ]; then # test should fail + if sh ${flags} $test >$out 2>$err then echo "FAIL $test: expected error; got \"$(cat $out)\"" exit 1 fi + else + echo "FAIL: does not exist: \"$test\"" + exit 1 fi done diff --git a/cmd/lulu/testchecks/cost b/cmd/lulu/testchecks/cost index 5ce1d48..479bdf4 100644 --- a/cmd/lulu/testchecks/cost +++ b/cmd/lulu/testchecks/cost @@ -1 +1,19 @@ -grep -E '^total: [1-9][0-9]*(\.[0-9]+)? [A-Z]{3}$' $1 +money='[0-9]+(\.[0-9]+)? [A-Z]{3}' +for field in "item 0" "item 1" "shipping" "fulfillment" "discount" "tax" "total" +do + re="$(printf '^%s: +%s *$' "${field}" "${money}")" + echo "$re" + grep -E "$re" $1 +done +grep -E -v '^item [^01]' $1 +awk ' + # Ensure sum of fields equals total. + !/^total/ { wantTotal += $(NF-1) } + /^total/ { + gotTotal = $(NF-1) + if (gotTotal > wantTotal+1e7 || gotTotal < wantTotal-1e7) { + printf "total (%f) does not match expected (%f)\n", gotTotal, wantTotal >>"/dev/stderr" + exit 1 + } + } +' $1 diff --git a/cmd/lulu/testchecks/jobs b/cmd/lulu/testchecks/jobs new file mode 100644 index 0000000..2d90544 --- /dev/null +++ b/cmd/lulu/testchecks/jobs @@ -0,0 +1,2 @@ +grep -E 'created +extid +id +orderid +nitems +total +status' $1 +grep -E '^ *[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z +.*[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+(\.[0-9]+)?[A-Z]{3} +[A-Z_]+$' $1 diff --git a/cmd/lulu/testchecks/orders b/cmd/lulu/testchecks/orders deleted file mode 100644 index 2d90544..0000000 --- a/cmd/lulu/testchecks/orders +++ /dev/null @@ -1,2 +0,0 @@ -grep -E 'created +extid +id +orderid +nitems +total +status' $1 -grep -E '^ *[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z +.*[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+(\.[0-9]+)?[A-Z]{3} +[A-Z_]+$' $1 diff --git a/cmd/lulu/tests/jobs b/cmd/lulu/tests/jobs new file mode 100644 index 0000000..8c6365d --- /dev/null +++ b/cmd/lulu/tests/jobs @@ -0,0 +1 @@ +lulu -s jobs diff --git a/cmd/lulu/tests/orders b/cmd/lulu/tests/orders deleted file mode 100644 index 910948c..0000000 --- a/cmd/lulu/tests/orders +++ /dev/null @@ -1 +0,0 @@ -lulu -s orders -- cgit v1.2.3