@@ -16,6 +16,7 @@ import (
1616 "io"
1717 "net/http"
1818 "os"
19+ "strconv"
1920
2021 "github.com/hyperledger/fabric-protos-go-apiv2/common"
2122 "github.com/hyperledger/fabric/internal/osnadmin"
@@ -63,6 +64,11 @@ func executeForArgs(args []string) (output string, exit int, err error) {
6364 configUpdateEnvelopePath := update .Flag ("config-update-envelope" , "Path to the file containing an up-to-date config update envelope for the channel" ).Short ('e' ).Required ().String ()
6465 tlsHandshakeTimeShift := update .Flag ("tlsHandshakeTimeShift" , "The amount of time to shift backwards for certificate expiration checks during TLS handshakes with the orderer endpoint" ).Short ('t' ).Default ("0" ).Duration ()
6566
67+ fetch := channel .Command ("fetch" , "Fetch a specified block, writing it to a file." )
68+ fetchChannelID := fetch .Flag ("channelID" , "Channel ID" ).Short ('c' ).Required ().String ()
69+ fetchBlockID := fetch .Flag ("blockID" , "Block ID - <newest|oldest|config|(number)>" ).Short ('b' ).Required ().String ()
70+ fetchOutputFile := fetch .Flag ("outputfile" , "Puth to a file." ).Short ('f' ).Required ().String ()
71+
6672 command , err := app .Parse (args )
6773 if err != nil {
6874 return "" , 1 , err
@@ -141,6 +147,14 @@ func executeForArgs(args []string) (output string, exit int, err error) {
141147 resp , err = osnadmin .Remove (osnURL , * removeChannelID , caCertPool , tlsClientCert )
142148 case update .FullCommand ():
143149 resp , err = osnadmin .Update (osnURL , marshaledConfigEnvelope , caCertPool , tlsClientCert , * tlsHandshakeTimeShift )
150+ case fetch .FullCommand ():
151+ if * fetchBlockID != "newest" && * fetchBlockID != "oldest" && * fetchBlockID != "config" {
152+ _ , err = strconv .Atoi (* fetchBlockID )
153+ if err != nil {
154+ return "" , 1 , fmt .Errorf ("'%s' not equal <newest|oldest|config|(number)>" , * fetchBlockID )
155+ }
156+ }
157+ resp , err = osnadmin .Fetch (osnURL , * fetchChannelID , * fetchBlockID , caCertPool , tlsClientCert )
144158 }
145159 if err != nil {
146160 return errorOutput (err ), 1 , nil
@@ -151,22 +165,28 @@ func executeForArgs(args []string) (output string, exit int, err error) {
151165 return errorOutput (err ), 1 , nil
152166 }
153167
154- output , err = responseOutput (! * noStatus , resp .StatusCode , bodyBytes )
168+ output , err = responseOutput (! * noStatus , resp .StatusCode , bodyBytes , * fetchOutputFile )
155169 if err != nil {
156170 return errorOutput (err ), 1 , nil
157171 }
158172
159173 return output , 0 , nil
160174}
161175
162- func responseOutput (showStatus bool , statusCode int , responseBody []byte ) (string , error ) {
176+ func responseOutput (showStatus bool , statusCode int , responseBody []byte , outputFile string ) (string , error ) {
163177 var buffer bytes.Buffer
164178 if showStatus {
165179 fmt .Fprintf (& buffer , "Status: %d\n " , statusCode )
166180 }
167181 if len (responseBody ) != 0 {
168- if err := json .Indent (& buffer , responseBody , "" , "\t " ); err != nil {
169- return "" , err
182+ if outputFile != "" {
183+ if err := os .WriteFile (outputFile , responseBody , 0o644 ); err != nil {
184+ return "" , err
185+ }
186+ } else {
187+ if err := json .Indent (& buffer , responseBody , "" , "\t " ); err != nil {
188+ return "" , err
189+ }
170190 }
171191 }
172192 return buffer .String (), nil
0 commit comments