Skip to content

Commit ea77b0a

Browse files
committed
feat: prettify json output using stackit curl
1 parent d1e0504 commit ea77b0a

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

internal/cmd/curl/curl.go

Lines changed: 39 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"
@@ -104,6 +105,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
104105
}
105106

106107
if model.FailOnHTTPError && resp.StatusCode >= 400 {
108+
fmt.Println("failed!!")
107109
os.Exit(22)
108110
}
109111
return nil
@@ -170,11 +172,29 @@ func getBearerToken(p *print.Printer) (string, error) {
170172
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
171173
return "", &errors.AuthError{}
172174
}
173-
token, err := auth.GetAuthField(auth.ACCESS_TOKEN)
175+
176+
userSessionExpired, err := auth.UserSessionExpired()
177+
if err != nil {
178+
return "", err
179+
}
180+
if userSessionExpired {
181+
return "", &errors.SessionExpiredError{}
182+
}
183+
184+
accessToken, err := auth.GetAccessToken()
174185
if err != nil {
175-
return "", fmt.Errorf("get access token: %w", err)
186+
return "", err
187+
}
188+
189+
accessTokenExpired, err := auth.TokenExpired(accessToken)
190+
if err != nil {
191+
return "", err
192+
}
193+
if accessTokenExpired {
194+
return "", &errors.AccessTokenExpiredError{}
176195
}
177-
return token, nil
196+
197+
return accessToken, nil
178198
}
179199

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

218254
if model.OutputFile == nil {

internal/cmd/curl/curl_test.go

Lines changed: 10 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,14 @@ 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+
},
430440
}
431441
p := print.NewPrinter()
432442
p.Cmd = NewCmd(p)

0 commit comments

Comments
 (0)