mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-21 19:17:42 +00:00
Compare commits
No commits in common. "ff09a3365ece98719c871b2284d488011cc7f4a2" and "b9dd14a588e70d0c910310ebd6b1c0337de3d9f8" have entirely different histories.
ff09a3365e
...
b9dd14a588
@ -59,9 +59,9 @@ You can find an example config structure in the [config.json](https://github.com
|
||||
"method": "GET",
|
||||
"url": "https://example.com",
|
||||
"no_proxy_check": false,
|
||||
"timeout": 2000,
|
||||
"dodos_count": 10,
|
||||
"request_count": 1000,
|
||||
"timeout": 10000,
|
||||
"dodos_count": 1,
|
||||
"request_count": 1,
|
||||
"params": {},
|
||||
"headers": {},
|
||||
"cookies": {},
|
||||
|
@ -1,9 +1,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "github.com/aykhans/dodo/types"
|
||||
@ -42,18 +42,7 @@ func (config *RequestConfig) Print() {
|
||||
t.SetOutputMirror(os.Stdout)
|
||||
t.SetStyle(table.StyleLight)
|
||||
t.SetColumnConfigs([]table.ColumnConfig{
|
||||
{
|
||||
Number: 2,
|
||||
WidthMaxEnforcer: func(col string, maxLen int) string {
|
||||
lines := strings.Split(col, "\n")
|
||||
for i, line := range lines {
|
||||
if len(line) > maxLen {
|
||||
lines[i] = line[:maxLen-3] + "..."
|
||||
}
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
},
|
||||
WidthMax: 50},
|
||||
{Number: 2, WidthMax: 50},
|
||||
})
|
||||
|
||||
newHeaders := make(map[string][]string)
|
||||
@ -67,23 +56,23 @@ func (config *RequestConfig) Print() {
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"URL", config.URL})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Timeout", config.Timeout})
|
||||
t.AppendRow(table.Row{"Timeout", fmt.Sprintf("%dms", config.Timeout/time.Millisecond)})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Dodos", config.DodosCount})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Requests", config.RequestCount})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Params", string(utils.PrettyJSONMarshal(config.Params, 3, "", " "))})
|
||||
t.AppendRow(table.Row{"Params", utils.MarshalJSON(config.Params, 3)})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Headers", string(utils.PrettyJSONMarshal(newHeaders, 3, "", " "))})
|
||||
t.AppendRow(table.Row{"Headers", utils.MarshalJSON(newHeaders, 3)})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Cookies", string(utils.PrettyJSONMarshal(config.Cookies, 3, "", " "))})
|
||||
t.AppendRow(table.Row{"Cookies", utils.MarshalJSON(config.Cookies, 3)})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Proxies Count", string(utils.PrettyJSONMarshal(config.Proxies, 3, "", " "))})
|
||||
t.AppendRow(table.Row{"Proxies Count", len(config.Proxies)})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Proxy Check", !config.NoProxyCheck})
|
||||
t.AppendSeparator()
|
||||
t.AppendRow(table.Row{"Body", string(utils.PrettyJSONMarshal(config.Body, 3, "", " "))})
|
||||
t.AppendRow(table.Row{"Body", utils.MarshalJSON(config.Body, 3)})
|
||||
|
||||
t.Render()
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -6,7 +6,7 @@ require (
|
||||
github.com/go-playground/validator/v10 v10.23.0
|
||||
github.com/jedib0t/go-pretty/v6 v6.6.5
|
||||
github.com/valyala/fasthttp v1.58.0
|
||||
golang.org/x/net v0.33.0
|
||||
golang.org/x/net v0.32.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
4
go.sum
4
go.sum
@ -35,8 +35,8 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZ
|
||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
|
||||
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
|
||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
|
||||
|
@ -6,77 +6,49 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type TruncatedMarshaller struct {
|
||||
Value interface{}
|
||||
MaxItems int
|
||||
func MarshalJSON(v any, maxSliceSize uint) string {
|
||||
rv := reflect.ValueOf(v)
|
||||
if rv.Kind() == reflect.Slice && rv.Len() == 0 {
|
||||
return "[]"
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(truncateLists(v, int(maxSliceSize)), "", " ")
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func (t TruncatedMarshaller) MarshalJSON() ([]byte, error) {
|
||||
val := reflect.ValueOf(t.Value)
|
||||
|
||||
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
|
||||
return json.Marshal(t.Value)
|
||||
}
|
||||
|
||||
length := val.Len()
|
||||
if length <= t.MaxItems {
|
||||
return json.Marshal(t.Value)
|
||||
}
|
||||
|
||||
truncated := make([]interface{}, t.MaxItems+1)
|
||||
|
||||
for i := 0; i < t.MaxItems; i++ {
|
||||
truncated[i] = val.Index(i).Interface()
|
||||
}
|
||||
|
||||
remaining := length - t.MaxItems
|
||||
truncated[t.MaxItems] = fmt.Sprintf("+%d", remaining)
|
||||
|
||||
return json.Marshal(truncated)
|
||||
}
|
||||
|
||||
func PrettyJSONMarshal(v interface{}, maxItems int, prefix, indent string) []byte {
|
||||
truncated := processValue(v, maxItems)
|
||||
d, _ := json.MarshalIndent(truncated, prefix, indent)
|
||||
return d
|
||||
}
|
||||
|
||||
func processValue(v interface{}, maxItems int) interface{} {
|
||||
val := reflect.ValueOf(v)
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Map:
|
||||
newMap := make(map[string]interface{})
|
||||
iter := val.MapRange()
|
||||
for iter.Next() {
|
||||
k := iter.Key().String()
|
||||
newMap[k] = processValue(iter.Value().Interface(), maxItems)
|
||||
}
|
||||
return newMap
|
||||
func truncateLists(v interface{}, maxItems int) interface{} {
|
||||
rv := reflect.ValueOf(v)
|
||||
|
||||
switch rv.Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
return TruncatedMarshaller{Value: v, MaxItems: maxItems}
|
||||
|
||||
if rv.Len() > maxItems {
|
||||
newSlice := reflect.MakeSlice(rv.Type(), maxItems, maxItems)
|
||||
reflect.Copy(newSlice, rv.Slice(0, maxItems))
|
||||
newSlice = reflect.Append(newSlice, reflect.ValueOf(fmt.Sprintf("...(%d more)", rv.Len()-maxItems)))
|
||||
return newSlice.Interface()
|
||||
}
|
||||
case reflect.Map:
|
||||
newMap := reflect.MakeMap(rv.Type())
|
||||
for _, key := range rv.MapKeys() {
|
||||
newMap.SetMapIndex(key, reflect.ValueOf(truncateLists(rv.MapIndex(key).Interface(), maxItems)))
|
||||
}
|
||||
return newMap.Interface()
|
||||
case reflect.Struct:
|
||||
newMap := make(map[string]interface{})
|
||||
t := val.Type()
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
if field.IsExported() {
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if jsonTag == "-" {
|
||||
continue
|
||||
newStruct := reflect.New(rv.Type()).Elem()
|
||||
for i := 0; i < rv.NumField(); i++ {
|
||||
newStruct.Field(i).Set(reflect.ValueOf(truncateLists(rv.Field(i).Interface(), maxItems)))
|
||||
}
|
||||
fieldName := field.Name
|
||||
if jsonTag != "" {
|
||||
fieldName = jsonTag
|
||||
return newStruct.Interface()
|
||||
case reflect.Ptr:
|
||||
if rv.IsNil() {
|
||||
return nil
|
||||
}
|
||||
newMap[fieldName] = processValue(val.Field(i).Interface(), maxItems)
|
||||
return truncateLists(rv.Elem().Interface(), maxItems)
|
||||
}
|
||||
}
|
||||
return newMap
|
||||
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user