@@ -156,7 +156,8 @@ func getCustomFieldsMapping() (map[string]string, error) {
156156 return fieldsMap , nil
157157}
158158
159- func runAPI (cmd * cobra.Command , args []string ) {
159+ // prepareAPIRequest prepares the configuration and request payload.
160+ func prepareAPIRequest (cmd * cobra.Command ) (string , string , []byte , bool , bool , bool , error ) {
160161 // Check if the environment is initialized properly
161162 configFile := viper .ConfigFileUsed ()
162163 if configFile == "" || ! jiraConfig .Exists (configFile ) {
@@ -169,9 +170,9 @@ func runAPI(cmd *cobra.Command, args []string) {
169170 }
170171
171172 debug , err := cmd .Flags ().GetBool ("debug" )
172- cmdutil . ExitIfError ( err )
173-
174- endpoint := args [ 0 ]
173+ if err != nil {
174+ return "" , "" , nil , false , false , false , err
175+ }
175176
176177 method , err := cmd .Flags ().GetString ("method" )
177178 cmdutil .ExitIfError (err )
@@ -185,6 +186,9 @@ func runAPI(cmd *cobra.Command, args []string) {
185186 raw , err := cmd .Flags ().GetBool ("raw" )
186187 cmdutil .ExitIfError (err )
187188
189+ translateFields , err := cmd .Flags ().GetBool ("translate-fields" )
190+ cmdutil .ExitIfError (err )
191+
188192 var payload []byte
189193
190194 if file != "" && data != "" {
@@ -202,6 +206,42 @@ func runAPI(cmd *cobra.Command, args []string) {
202206 }
203207 }
204208
209+ return server , method , payload , raw , debug , translateFields , nil
210+ }
211+
212+ // processResponseBody formats the response body as needed.
213+ func processResponseBody (body []byte , raw , translateFields , debug bool ) []byte {
214+ if raw || len (body ) == 0 {
215+ return body
216+ }
217+
218+ // Check if the response looks like JSON
219+ trimmedBody := strings .TrimSpace (string (body ))
220+ isJSON := (strings .HasPrefix (trimmedBody , "{" ) && strings .HasSuffix (trimmedBody , "}" )) ||
221+ (strings .HasPrefix (trimmedBody , "[" ) && strings .HasSuffix (trimmedBody , "]" ))
222+
223+ if isJSON {
224+ // If we need to translate custom fields, do that before pretty printing
225+ if translateFields {
226+ body = translateCustomFields (body , debug )
227+ }
228+
229+ var prettyJSON bytes.Buffer
230+ err := json .Indent (& prettyJSON , body , "" , " " )
231+ if err == nil {
232+ body = prettyJSON .Bytes ()
233+ }
234+ }
235+
236+ return body
237+ }
238+
239+ func runAPI (cmd * cobra.Command , args []string ) {
240+ server , method , payload , raw , debug , translateFields , err := prepareAPIRequest (cmd )
241+ cmdutil .ExitIfError (err )
242+
243+ endpoint := args [0 ]
244+
205245 // Show a progress spinner during the request
206246 s := cmdutil .Info ("Sending request to Jira API..." )
207247 defer s .Stop ()
@@ -233,34 +273,17 @@ func runAPI(cmd *cobra.Command, args []string) {
233273 cmdutil .Failed ("Request failed: %s" , err )
234274 }
235275
236- defer resp .Body .Close ()
276+ defer func () {
277+ if err := resp .Body .Close (); err != nil {
278+ cmdutil .Failed ("Failed to close response body: %s" , err )
279+ }
280+ }()
237281
238282 body , err := io .ReadAll (resp .Body )
239283 cmdutil .ExitIfError (err )
240284
241- translateFields , err := cmd .Flags ().GetBool ("translate-fields" )
242- cmdutil .ExitIfError (err )
243-
244- // Try to pretty print JSON if the response appears to be JSON and raw mode is not enabled
245- if ! raw && len (body ) > 0 {
246- // Check if the response looks like JSON
247- trimmedBody := strings .TrimSpace (string (body ))
248- isJSON := (strings .HasPrefix (trimmedBody , "{" ) && strings .HasSuffix (trimmedBody , "}" )) ||
249- (strings .HasPrefix (trimmedBody , "[" ) && strings .HasSuffix (trimmedBody , "]" ))
250-
251- if isJSON {
252- // If we need to translate custom fields, do that before pretty printing
253- if translateFields {
254- body = translateCustomFields (body , debug )
255- }
256-
257- var prettyJSON bytes.Buffer
258- err = json .Indent (& prettyJSON , body , "" , " " )
259- if err == nil {
260- body = prettyJSON .Bytes ()
261- }
262- }
263- }
285+ // Process the response body with formatting and translation
286+ body = processResponseBody (body , raw , translateFields , debug )
264287
265288 fmt .Printf ("HTTP/%d %s\n " , resp .StatusCode , resp .Status )
266289
0 commit comments