Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsh play --format=text #46849

Merged
merged 2 commits into from
Oct 1, 2024
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
23 changes: 21 additions & 2 deletions tool/tsh/common/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ func exportSession(cf *CLIConf) error {
}

switch format {
case teleport.JSON, teleport.YAML:
case teleport.JSON, teleport.YAML, teleport.Text:
default:
return trace.Errorf("Invalid format %s, only json and yaml are supported", format)
// this should be unreachable since kingpin validates the format flag
return trace.BadParameter("Invalid format %s", format)
}

sid, err := session.ParseID(cf.SessionID)
Expand All @@ -144,6 +145,8 @@ func exportSession(cf *CLIConf) error {
exporter = jsonSessionExporter{}
case teleport.YAML:
exporter = yamlSessionExporter{}
case teleport.Text:
exporter = textSessionExporter{}
}

exporter.WriteStart()
Expand Down Expand Up @@ -233,6 +236,22 @@ func (yamlSessionExporter) WriteEvent(evt apievents.AuditEvent) error {
return err
}

type textSessionExporter struct{}

func (textSessionExporter) WriteStart() error { return nil }
func (textSessionExporter) WriteEnd() error { return nil }
func (textSessionExporter) WriteSeparator() error { return nil }

func (textSessionExporter) WriteEvent(evt apievents.AuditEvent) error {
printEvent, ok := evt.(*apievents.SessionPrint)
if !ok {
return nil
}

_, err := os.Stdout.Write(printEvent.Data)
return trace.Wrap(err)
}

// exportFile converts the binary protobuf events from the file
// identified by path to text (JSON/YAML) and writes the converted
// events to standard out.
Expand Down
4 changes: 2 additions & 2 deletions tool/tsh/common/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -983,8 +983,8 @@ func Run(ctx context.Context, args []string, opts ...CliOption) error {
play.Flag("speed", "Playback speed, applicable when streaming SSH or Kubernetes sessions.").Default("1x").EnumVar(&cf.PlaySpeed, "0.5x", "1x", "2x", "4x", "8x")
play.Flag("skip-idle-time", "Quickly skip over idle time, applicable when streaming SSH or Kubernetes sessions.").BoolVar(&cf.NoWait)
play.Flag("format", defaults.FormatFlagDescription(
teleport.PTY, teleport.JSON, teleport.YAML,
)).Short('f').Default(teleport.PTY).EnumVar(&cf.Format, teleport.PTY, teleport.JSON, teleport.YAML)
teleport.PTY, teleport.JSON, teleport.YAML, teleport.Text,
)).Short('f').Default(teleport.PTY).EnumVar(&cf.Format, teleport.PTY, teleport.JSON, teleport.YAML, teleport.Text)
play.Arg("session-id", "ID or path to session file to play").Required().StringVar(&cf.SessionID)

// scp
Expand Down
Loading