Skip to content

Commit

Permalink
Replace script with asciinema
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Heckel committed Sep 21, 2021
1 parent 69bf133 commit 2758e81
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 11 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ nfpms:
- rpm
dependencies:
- tmux
- asciinema
suggests:
- docker-ce
recommends:
Expand Down
3 changes: 0 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@ func (b *Bot) applySessionConfigDefaults(ev *messageEvent, conf *sessionConfig)
if conf.authMode == "" {
conf.authMode = config.OnlyMe
}
if conf.size == nil && b.config.Platform() != config.Discord {
conf.size = config.Medium // Discord has a 2000 char limit, so we can only do this for Slack
}
}
if conf.controlMode == "" {
if ev.Thread != "" {
Expand Down
67 changes: 59 additions & 8 deletions bot/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package bot

import (
"archive/zip"
"bufio"
"context"
_ "embed" // go:embed requires this
"errors"
"fmt"
"github.com/tidwall/sjson"
"golang.org/x/sync/errgroup"
"heckel.io/replbot/config"
"heckel.io/replbot/util"
Expand Down Expand Up @@ -153,6 +155,7 @@ type session struct {
tmux *util.Tmux
cursorOn bool
cursorUpdated time.Time
maxSize *config.Size
shareConn io.Closer
mu sync.RWMutex
}
Expand Down Expand Up @@ -212,6 +215,7 @@ func newSession(conf *sessionConfig, conn conn) *session {
active: true,
warnTimer: time.NewTimer(conf.global.IdleTimeout - time.Minute),
closeTimer: time.NewTimer(conf.global.IdleTimeout),
maxSize: conf.size,
}
return initSessionCommands(s)
}
Expand Down Expand Up @@ -461,7 +465,6 @@ func (s *session) shutdownHandler() error {
}
os.Remove(s.sshUserFile())
os.Remove(s.sshClientKeyFile())
os.Remove(s.tmux.RecordingFile())
s.mu.Lock()
s.active = false
if s.shareConn != nil {
Expand Down Expand Up @@ -571,7 +574,7 @@ func (s *session) createCommand() []string {
return []string{
"asciinema", "rec",
"--quiet",
"--idle-time-limit", "3",
"--idle-time-limit", "5",
"--title", "REPLbot session",
"--command", command,
s.asciinemaFile(),
Expand All @@ -596,6 +599,12 @@ func (s *session) sshUserFile() string {
}

func (s *session) createRecordingArchive(filename string) (*os.File, error) {
recordingFile := s.tmux.RecordingFile()
asciinemaFile := s.asciinemaFile()
defer func() {
os.Remove(recordingFile)
os.Remove(asciinemaFile)
}()
file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
Expand All @@ -604,12 +613,6 @@ func (s *session) createRecordingArchive(filename string) (*os.File, error) {
if err := zipAppendEntry(zw, "REPLbot session/README.md", recordingReadmeSource); err != nil {
return nil, err
}
recordingFile := s.tmux.RecordingFile()
asciinemaFile := s.asciinemaFile()
defer func() {
os.Remove(recordingFile)
os.Remove(asciinemaFile)
}()
if err := zipAppendFile(zw, "REPLbot session/terminal.txt", recordingFile); err != nil {
return nil, err
}
Expand Down Expand Up @@ -660,6 +663,9 @@ func (s *session) sendExitedMessageWithoutRecording() error {
}

func (s *session) sendExitedMessageWithRecording() error {
if err := s.maybePatchAsciinemaRecordingFile(); err != nil {
log.Printf("[%s] Cannot patch asciinema session file: %s", s.conf.id, err.Error())
}
url, expiry, err := s.maybeUploadAsciinemaRecording()
if err != nil {
log.Printf("[%s] Cannot upload recorded asciinema session: %s", s.conf.id, err.Error())
Expand Down Expand Up @@ -821,6 +827,11 @@ func (s *session) handleResizeCommand(input string) error {
if err := s.maybeSendMessageLengthWarning(size); err != nil {
return err
}
if s.maxSize.Max(size) == size {
s.mu.Lock()
s.maxSize = size
s.mu.Unlock()
}
return s.tmux.Resize(size.Width, size.Height)
}

Expand Down Expand Up @@ -850,3 +861,43 @@ func (s *session) maybeUploadAsciinemaRecording() (url string, expiry string, er
}
return url, expiry, nil
}

func (s *session) maybePatchAsciinemaRecordingFile() error {
replayFile := s.asciinemaFile()
tempReplayFile := fmt.Sprintf("%s.tmp", replayFile)
defer os.Remove(tempReplayFile)
in, err := os.Open(s.asciinemaFile())
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(tempReplayFile, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer out.Close()
rd := bufio.NewReader(in)
header, err := rd.ReadString('\n')
if err != nil {
return err
}
if header, err = sjson.Set(header, "width", s.maxSize.Width); err != nil {
return err
}
if header, err = sjson.Set(header, "height", s.maxSize.Height); err != nil {
return err
}
if _, err := out.WriteString(header); err != nil {
return err
}
if _, err := io.Copy(out, rd); err != nil {
return err
}
if err := out.Close(); err != nil {
return err
}
if err := in.Close(); err != nil {
return err
}
return os.Rename(tempReplayFile, replayFile)
}
8 changes: 8 additions & 0 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ var (
}
)

// Max returns the larger size of the two given sizes
func (s *Size) Max(other *Size) *Size {
if other.Width*other.Height > s.Width*s.Height {
return other
}
return s
}

// Constants used to toggle the cursor on or off
const (
CursorOff = time.Duration(0)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/tidwall/gjson v1.9.1 // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.2 // indirect
)

replace github.com/bwmarrin/discordgo => github.com/binwiederhier/discordgo v0.23.3-0.20210824013058-32da67e86a5d
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ github.com/slack-go/slack v0.9.4 h1:C+FC3zLxLxUTQjDy2RZeMHYon005zsCROiZNWVo+opQ=
github.com/slack-go/slack v0.9.4/go.mod h1:wWL//kk0ho+FcQXcBTmEafUI5dz4qz5f4mMk8oIkioQ=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tidwall/gjson v1.9.1 h1:wrrRk7TyL7MmKanNRck/Mcr3VU1sdMvJHvJXzqBIUNo=
github.com/tidwall/gjson v1.9.1/go.mod h1:jydLKE7s8J0+1/5jC4eXcuFlzKizGrCKvLmBVX/5oXc=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.2 h1:H1Llj/C9G+BoUN2DsybLHjWvr9dx4Uazavf0sXQ+rOs=
github.com/tidwall/sjson v1.2.2/go.mod h1:jmW2RZpbKuExPFUHeFSBMiovT9ZyOziEHDRkbsdp0B0=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
Expand Down

0 comments on commit 2758e81

Please sign in to comment.