Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cmd/tg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

"github.com/gotd/cli/internal/pretty"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
Expand All @@ -25,6 +26,8 @@ type app struct {
cfg Config
log *zap.Logger
opts telegram.Options

debugInvoker bool
}

func newApp() *app {
Expand Down Expand Up @@ -59,17 +62,20 @@ func (p *app) run(ctx context.Context, f func(ctx context.Context, api *tg.Clien
}
}

invoker := invokers.NewWaiter(client)
raw := tg.NewClient(invoker)

ctx, cancel := context.WithCancel(ctx)
g, ctx := errgroup.WithContext(ctx)

invoker := invokers.NewWaiter(client)
g.Go(func() error {
return invoker.Run(ctx)
})
g.Go(func() error {
defer cancel()
return f(ctx, raw)
var i tg.Invoker = invoker
if p.debugInvoker {
i = pretty.Invoker{Next: i}
}
return f(ctx, tg.NewClient(i))
})

if err := g.Wait(); !errors.Is(err, context.Canceled) {
Expand Down Expand Up @@ -113,6 +119,9 @@ func (p *app) Before(c *cli.Context) error {
if c.Bool("test") {
p.opts.DCList = dcs.StagingDCs()
}
if c.Bool("debug-invoker") {
p.debugInvoker = true
}

return nil
}
4 changes: 4 additions & 0 deletions cmd/tg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func main() {
Value: defaultConfigPath(),
Usage: "config to use",
},
&cli.BoolFlag{
Name: "debug-invoker",
Usage: "use pretty-printing debug invoker",
},
&cli.BoolFlag{
Name: "test",
Aliases: []string{"staging"},
Expand Down
46 changes: 46 additions & 0 deletions internal/pretty/pretty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Package pretty implements pretty-print facilities.
package pretty

import (
"context"
"fmt"
"reflect"
"time"

"github.com/gotd/td/bin"
"github.com/gotd/td/tdp"
"github.com/gotd/td/tg"
)

type Invoker struct {
Next tg.Invoker
}

func formatObject(input interface{}) string {
o, ok := input.(tdp.Object)
if !ok {
// Handle tg.*Box values.
rv := reflect.Indirect(reflect.ValueOf(input))
for i := 0; i < rv.NumField(); i++ {
if v, ok := rv.Field(i).Interface().(tdp.Object); ok {
return formatObject(v)
}
}

return fmt.Sprintf("%T (not object)", input)
}
return tdp.Format(o)
}

func (i Invoker) InvokeRaw(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
fmt.Println("→", formatObject(input))
start := time.Now()
if err := i.Next.InvokeRaw(ctx, input, output); err != nil {
fmt.Println("←", err)
return err
}

fmt.Printf("← (%s) %s\n", time.Since(start).Round(time.Millisecond), formatObject(output))

return nil
}