ente/cli/internal/api/log.go

69 lines
1.9 KiB
Go
Raw Normal View History

2023-09-08 14:42:58 +00:00
package api
import (
"fmt"
"strings"
2023-09-13 12:39:54 +00:00
"github.com/fatih/color"
"github.com/go-resty/resty/v2"
2023-09-08 14:42:58 +00:00
)
func logRequest(req *resty.Request) {
2023-09-13 12:39:54 +00:00
fmt.Println(color.GreenString("Request:"))
fmt.Printf("%s %s\n", color.CyanString(req.Method), color.YellowString(req.URL))
fmt.Println(color.GreenString("Headers:"))
2023-09-08 14:42:58 +00:00
for k, v := range req.Header {
redacted := false
for _, rh := range RedactedHeaders {
2023-09-13 12:39:54 +00:00
if strings.EqualFold(strings.ToLower(k), strings.ToLower(rh)) {
2023-09-08 14:42:58 +00:00
redacted = true
break
}
}
if redacted {
2023-09-13 12:39:54 +00:00
fmt.Printf("%s: %s\n", color.CyanString(k), color.RedString("REDACTED"))
2023-09-08 14:42:58 +00:00
} else {
2023-09-13 12:39:54 +00:00
if len(v) == 1 {
fmt.Printf("%s: %s\n", color.CyanString(k), color.YellowString(v[0]))
} else {
fmt.Printf("%s: %s\n", color.CyanString(k), color.YellowString(strings.Join(v, ",")))
}
2023-09-08 14:42:58 +00:00
}
}
2024-03-12 09:02:41 +00:00
// log query params if present
if len(req.QueryParam) > 0 {
fmt.Println(color.GreenString("Query Params:"))
for k, v := range req.QueryParam {
2024-03-12 09:03:32 +00:00
if k == TokenQuery {
v = []string{"REDACTED"}
}
2024-03-12 09:02:41 +00:00
fmt.Printf("%s: %s\n", color.CyanString(k), color.YellowString(strings.Join(v, ",")))
}
}
2023-09-08 14:42:58 +00:00
}
func logResponse(resp *resty.Response) {
2023-09-13 12:39:54 +00:00
fmt.Println(color.GreenString("Response:"))
if resp.StatusCode() < 200 || resp.StatusCode() >= 300 {
fmt.Printf("%s %s\n", color.CyanString(resp.Proto()), color.RedString(resp.Status()))
} else {
fmt.Printf("%s %s\n", color.CyanString(resp.Proto()), color.YellowString(resp.Status()))
}
2023-09-08 14:42:58 +00:00
fmt.Printf("Time Duration: %s\n", resp.Time())
2023-09-13 12:39:54 +00:00
fmt.Println(color.GreenString("Headers:"))
2023-09-08 14:42:58 +00:00
for k, v := range resp.Header() {
redacted := false
for _, rh := range RedactedHeaders {
2023-09-13 12:39:54 +00:00
if strings.EqualFold(strings.ToLower(k), strings.ToLower(rh)) {
2023-09-08 14:42:58 +00:00
redacted = true
break
}
}
if redacted {
2023-09-13 12:39:54 +00:00
fmt.Printf("%s: %s\n", color.CyanString(k), color.RedString("REDACTED"))
2023-09-08 14:42:58 +00:00
} else {
2023-09-13 12:39:54 +00:00
fmt.Printf("%s: %s\n", color.CyanString(k), color.YellowString(strings.Join(v, ",")))
2023-09-08 14:42:58 +00:00
}
}
}