github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/debugging.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package service 5 6 import ( 7 "fmt" 8 "strconv" 9 "time" 10 11 "github.com/keybase/client/go/libkb" 12 gregor1 "github.com/keybase/client/go/protocol/gregor1" 13 keybase1 "github.com/keybase/client/go/protocol/keybase1" 14 "github.com/keybase/go-framed-msgpack-rpc/rpc" 15 "golang.org/x/net/context" 16 ) 17 18 type DebuggingHandler struct { 19 libkb.Contextified 20 *BaseHandler 21 userHandler *UserHandler 22 walletHandler *walletHandler 23 } 24 25 func NewDebuggingHandler(xp rpc.Transporter, g *libkb.GlobalContext, userHandler *UserHandler, walletHandler *walletHandler) *DebuggingHandler { 26 return &DebuggingHandler{ 27 Contextified: libkb.NewContextified(g), 28 BaseHandler: NewBaseHandler(g, xp), 29 userHandler: userHandler, 30 walletHandler: walletHandler, 31 } 32 } 33 34 // See debugging_devel.go for additional scripts. 35 func (t *DebuggingHandler) Script(ctx context.Context, arg keybase1.ScriptArg) (res string, err error) { 36 ctx = libkb.WithLogTag(ctx, "DG") 37 m := libkb.NewMetaContext(ctx, t.G()) 38 defer m.Trace(fmt.Sprintf("Script(%s)", arg.Script), &err)() 39 args := arg.Args 40 log := func(format string, args ...interface{}) { 41 t.G().Log.CInfof(ctx, format, args...) 42 } 43 defer time.Sleep(100 * time.Millisecond) // Without this CInfof often doesn't reach the CLI 44 switch arg.Script { 45 case "journeycard": 46 log("journeycard-fastforward [days]") 47 log("journeycard-resetall") 48 log("journeycard-state <team-id>") 49 return "", nil 50 case "journeycard-fastforward": 51 uidGregor := gregor1.UID(m.G().ActiveDevice.UID().ToBytes()) 52 advance := 24 * time.Hour 53 if len(args) >= 1 { 54 days, err := strconv.Atoi(args[0]) 55 if err != nil { 56 return "", err 57 } 58 advance = time.Duration(days) * 24 * time.Hour 59 } 60 nTeams, nConvs, err := t.G().ChatHelper.JourneycardTimeTravel(m.Ctx(), uidGregor, advance) 61 if err != nil { 62 return "", err 63 } 64 log("time advanced by %v for %v teams and %v convs", advance, nTeams, nConvs) 65 return "", err 66 case "journeycard-resetall": 67 uidGregor := gregor1.UID(m.G().ActiveDevice.UID().ToBytes()) 68 err = t.G().ChatHelper.JourneycardResetAllConvs(m.Ctx(), uidGregor) 69 if err != nil { 70 return "", err 71 } 72 log("journeycard state has been reset for all convs") 73 return "", nil 74 case "journeycard-state": 75 if len(args) != 1 { 76 return "", fmt.Errorf("usage: journeycard-state <conv-id> (like 000059aa7f324dad7524b56ed1beb3e3d620b3897d640951710f1417c6b7b85f)") 77 } 78 teamID, err := keybase1.TeamIDFromString(args[0]) 79 if err != nil { 80 return "", err 81 } 82 uidGregor := gregor1.UID(m.G().ActiveDevice.UID().ToBytes()) 83 summary, err := t.G().ChatHelper.JourneycardDebugState(m.Ctx(), uidGregor, teamID) 84 return summary, err 85 case "": 86 return "", fmt.Errorf("empty script name") 87 default: 88 return t.scriptExtras(ctx, arg) 89 } 90 } 91 92 func (t *DebuggingHandler) FirstStep(ctx context.Context, arg keybase1.FirstStepArg) (result keybase1.FirstStepResult, err error) { 93 client := t.rpcClient() 94 cbArg := keybase1.SecondStepArg{Val: arg.Val + 1, SessionID: arg.SessionID} 95 var cbReply int 96 err = client.Call(ctx, "keybase.1.debugging.secondStep", []interface{}{cbArg}, &cbReply, 0) 97 if err != nil { 98 return 99 } 100 101 result.ValPlusTwo = cbReply 102 return 103 } 104 105 func (t *DebuggingHandler) SecondStep(_ context.Context, arg keybase1.SecondStepArg) (val int, err error) { 106 val = arg.Val + 1 107 return 108 } 109 110 func (t *DebuggingHandler) Increment(_ context.Context, arg keybase1.IncrementArg) (val int, err error) { 111 val = arg.Val + 1 112 return 113 }