@@ -698,94 +698,92 @@ func main() {
698698 }
699699
700700 if err == nil && actionRequest != "" {
701- if err == nil {
702- var rspJSON []byte
703- var req , rsp notecard.Request
704- note .JSONUnmarshal ([]byte (actionRequest ), & req )
701+ var rspJSON []byte
702+ var req , rsp notecard.Request
703+ note .JSONUnmarshal ([]byte (actionRequest ), & req )
705704
706- if ! actionForce {
707- err = validateRequest ([]byte (actionRequest ), lib .Config .SchemaUrl )
708- if err != nil {
709- goto done
710- }
705+ if ! actionForce {
706+ err = validateRequest ([]byte (actionRequest ), lib .Config .SchemaUrl )
707+ if err != nil {
708+ goto done
711709 }
710+ }
712711
713- // If we want to read the payload from a file, do so
714- if actionInput != "" {
715- var contents []byte
716- contents , err = os .ReadFile (actionInput )
717- if err == nil {
718- req .Payload = & contents
719- }
712+ // If we want to read the payload from a file, do so
713+ if actionInput != "" {
714+ var contents []byte
715+ contents , err = os .ReadFile (actionInput )
716+ if err == nil {
717+ req .Payload = & contents
720718 }
719+ }
721720
722- // Perform the transaction and do special handling for binary
723- if req .Req == "card.binary.get" {
724- expectedMD5 := req .Status
725- rsp , err = card .TransactionRequest (req )
721+ // Perform the transaction and do special handling for binary
722+ if req .Req == "card.binary.get" {
723+ expectedMD5 := req .Status
724+ rsp , err = card .TransactionRequest (req )
725+ if err == nil {
726+ var rspBytes []byte
727+ rspBytes , err = card .ReceiveBytes ()
726728 if err == nil {
727- var rspBytes []byte
728- rspBytes , err = card .ReceiveBytes ()
729- if err == nil {
730- rspBytes = bytes .TrimSuffix (rspBytes , []byte ("\n " ))
731- rspBytes , err = notecard .CobsDecode (rspBytes , byte ('\n' ))
732- if err == nil {
733- actualMD5 := fmt .Sprintf ("%x" , md5 .Sum (rspBytes ))
734- if expectedMD5 != actualMD5 {
735- err = fmt .Errorf ("actual MD5 %s != supplied 'status' field %s" , actualMD5 , expectedMD5 )
736- } else {
737- rsp .Payload = & rspBytes
738- rsp .Cobs = 0
739- }
740- }
741- }
742- }
743- } else if req .Req == "card.binary.put" && (req .Body == nil || len (* req .Body ) == 0 ) {
744- payload := * req .Payload
745- actualMD5 := fmt .Sprintf ("%x" , md5 .Sum (payload ))
746- if req .Status != "" && ! strings .EqualFold (req .Status , actualMD5 ) {
747- err = fmt .Errorf ("actual MD5 %s != supplied 'status' field %s" , actualMD5 , req .Status )
748- } else {
749- req .Status = actualMD5
750- payload , err = notecard .CobsEncode (payload , byte ('\n' ))
729+ rspBytes = bytes .TrimSuffix (rspBytes , []byte ("\n " ))
730+ rspBytes , err = notecard .CobsDecode (rspBytes , byte ('\n' ))
751731 if err == nil {
752- req . Payload = nil
753- req . Cobs = int32 ( len ( payload ))
754- rsp , err = card . TransactionRequest ( req )
755- if err == nil {
756- payload = append ( payload , byte ( '\n' ))
757- err = card . SendBytes ( payload )
732+ actualMD5 := fmt . Sprintf ( "%x" , md5 . Sum ( rspBytes ))
733+ if expectedMD5 != actualMD5 {
734+ err = fmt . Errorf ( "actual MD5 %s != supplied 'status' field %s" , actualMD5 , expectedMD5 )
735+ } else {
736+ rsp . Payload = & rspBytes
737+ rsp . Cobs = 0
758738 }
759739 }
760740 }
741+ }
742+ } else if req .Req == "card.binary.put" && (req .Body == nil || len (* req .Body ) == 0 ) {
743+ payload := * req .Payload
744+ actualMD5 := fmt .Sprintf ("%x" , md5 .Sum (payload ))
745+ if req .Status != "" && ! strings .EqualFold (req .Status , actualMD5 ) {
746+ err = fmt .Errorf ("actual MD5 %s != supplied 'status' field %s" , actualMD5 , req .Status )
761747 } else {
762- actionRequest = strings . ReplaceAll ( actionRequest , " \\ n" , " \n " )
763- rspJSON , err = card . TransactionJSON ([] byte (actionRequest ))
748+ req . Status = actualMD5
749+ payload , err = notecard . CobsEncode ( payload , byte ('\n' ))
764750 if err == nil {
765- _ = note .JSONUnmarshal (rspJSON , & rsp )
751+ req .Payload = nil
752+ req .Cobs = int32 (len (payload ))
753+ rsp , err = card .TransactionRequest (req )
754+ if err == nil {
755+ payload = append (payload , byte ('\n' ))
756+ err = card .SendBytes (payload )
757+ }
766758 }
767759 }
760+ } else {
761+ actionRequest = strings .ReplaceAll (actionRequest , "\\ n" , "\n " )
762+ rspJSON , err = card .TransactionJSON ([]byte (actionRequest ))
763+ if err == nil {
764+ _ = note .JSONUnmarshal (rspJSON , & rsp )
765+ }
766+ }
768767
769- // Write the payload to an output file if appropriate
770- if err == nil && actionOutput != "" {
771- if rsp .Payload != nil {
772- err = os .WriteFile (actionOutput , * rsp .Payload , 0644 )
773- if err != nil {
774- rsp .Payload = nil
775- }
768+ // Write the payload to an output file if appropriate
769+ if err == nil && actionOutput != "" {
770+ if rsp .Payload != nil {
771+ err = os .WriteFile (actionOutput , * rsp .Payload , 0644 )
772+ if err != nil {
773+ rsp .Payload = nil
776774 }
777775 }
776+ }
778777
779- // Output the response to the console
780- if ! actionVerbose {
781- if err == nil {
782- if actionPretty {
783- rspJSON , _ = note .JSONMarshalIndent (rsp , "" , " " )
784- } else {
785- rspJSON , _ = note .JSONMarshal (rsp )
786- }
787- fmt .Printf ("%s\n " , rspJSON )
778+ // Output the response to the console
779+ if ! actionVerbose {
780+ if err == nil {
781+ if actionPretty {
782+ rspJSON , _ = note .JSONMarshalIndent (rsp , "" , " " )
783+ } else {
784+ rspJSON , _ = note .JSONMarshal (rsp )
788785 }
786+ fmt .Printf ("%s\n " , rspJSON )
789787 }
790788 }
791789 }
0 commit comments