Skip to content

Commit f572e14

Browse files
authored
feat: prettify json output using stackit curl (#713)
1 parent 6a6a5bf commit f572e14

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

internal/cmd/curl/curl.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package curl
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
67
"io"
78
"net/http"
@@ -170,11 +171,29 @@ func getBearerToken(p *print.Printer) (string, error) {
170171
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
171172
return "", &errors.AuthError{}
172173
}
173-
token, err := auth.GetAuthField(auth.ACCESS_TOKEN)
174+
175+
userSessionExpired, err := auth.UserSessionExpired()
176+
if err != nil {
177+
return "", err
178+
}
179+
if userSessionExpired {
180+
return "", &errors.SessionExpiredError{}
181+
}
182+
183+
accessToken, err := auth.GetAccessToken()
174184
if err != nil {
175-
return "", fmt.Errorf("get access token: %w", err)
185+
return "", err
186+
}
187+
188+
accessTokenExpired, err := auth.TokenExpired(accessToken)
189+
if err != nil {
190+
return "", err
191+
}
192+
if accessTokenExpired {
193+
return "", &errors.AccessTokenExpiredError{}
176194
}
177-
return token, nil
195+
196+
return accessToken, nil
178197
}
179198

180199
func buildRequest(model *inputModel, bearerToken string) (*http.Request, error) {
@@ -213,6 +232,22 @@ func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) er
213232
if err != nil {
214233
return fmt.Errorf("read response body: %w", err)
215234
}
235+
236+
if strings.Contains(strings.ToLower(string(respBody)), "jwt is expired") {
237+
return &errors.SessionExpiredError{}
238+
}
239+
240+
if strings.Contains(strings.ToLower(string(respBody)), "jwt is missing") {
241+
return &errors.AuthError{}
242+
}
243+
244+
var prettyJSON bytes.Buffer
245+
if json.Valid(respBody) {
246+
if err := json.Indent(&prettyJSON, respBody, "", " "); err == nil {
247+
respBody = prettyJSON.Bytes()
248+
} // if indenting fails, fall back to original body
249+
}
250+
216251
output = append(output, respBody...)
217252

218253
if model.OutputFile == nil {

internal/cmd/curl/curl_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"net/http"
89
"os"
10+
"strings"
911
"testing"
1012

1113
"github.com/google/go-cmp/cmp"
@@ -427,6 +429,22 @@ func TestOutputResponse(t *testing.T) {
427429
},
428430
wantErr: false,
429431
},
432+
{
433+
name: "expired jwt curl",
434+
args: args{
435+
model: fixtureInputModel(),
436+
resp: &http.Response{Body: io.NopCloser(strings.NewReader("Jwt is expired"))},
437+
},
438+
wantErr: true,
439+
},
440+
{
441+
name: "mssing jwt curl",
442+
args: args{
443+
model: fixtureInputModel(),
444+
resp: &http.Response{Body: io.NopCloser(strings.NewReader("Jwt is missing"))},
445+
},
446+
wantErr: true,
447+
},
430448
}
431449
p := print.NewPrinter()
432450
p.Cmd = NewCmd(p)

0 commit comments

Comments
 (0)