Files
asciinema/commands/rec.go

125 lines
2.1 KiB
Go
Raw Normal View History

2014-08-03 19:50:39 +02:00
package commands
import (
"flag"
"fmt"
"io/ioutil"
2014-08-03 19:50:39 +02:00
"os"
"github.com/asciinema/asciinema-cli/api"
"github.com/asciinema/asciinema-cli/asciicast"
2014-08-03 19:50:39 +02:00
"github.com/asciinema/asciinema-cli/cli"
"github.com/asciinema/asciinema-cli/util"
)
2014-11-12 20:54:50 +01:00
type RecordCommand struct {
Cfg *util.Config
2014-11-15 13:33:20 +01:00
API api.API
Recorder asciicast.Recorder
2014-11-12 20:54:50 +01:00
Command string
Title string
NoConfirm bool
MaxWait uint
2014-11-12 20:54:50 +01:00
}
2014-11-15 13:33:20 +01:00
func NewRecordCommand(api api.API, cfg *util.Config) cli.Command {
2014-11-12 20:54:50 +01:00
return &RecordCommand{
2014-11-15 13:33:20 +01:00
API: api,
2014-11-12 20:54:50 +01:00
Cfg: cfg,
Recorder: asciicast.NewRecorder(),
2014-08-03 20:48:48 +02:00
}
2014-11-12 20:54:50 +01:00
}
2014-08-03 19:50:39 +02:00
2014-11-12 20:54:50 +01:00
func (c *RecordCommand) RegisterFlags(flags *flag.FlagSet) {
2014-08-03 19:50:39 +02:00
flags.StringVar(
2014-11-12 20:54:50 +01:00
&c.Command,
2014-08-03 19:50:39 +02:00
"c",
2014-11-12 20:54:50 +01:00
defaultRecCommand(c.Cfg.Record.Command),
2014-12-14 14:01:33 +01:00
"command to record",
2014-08-03 19:50:39 +02:00
)
flags.StringVar(
2014-11-12 20:54:50 +01:00
&c.Title,
2014-08-03 19:50:39 +02:00
"t",
"",
2014-12-14 14:01:33 +01:00
"set asciicast title",
2014-08-03 19:50:39 +02:00
)
flags.BoolVar(
2014-11-12 20:54:50 +01:00
&c.NoConfirm,
2014-08-03 19:50:39 +02:00
"y",
false,
"upload without asking for confirmation",
2014-08-03 19:50:39 +02:00
)
flags.UintVar(
&c.MaxWait,
"max-wait",
0,
2014-12-14 14:01:33 +01:00
"reduce recorded terminal inactivity to maximum of <max-wait> seconds (0 turns off)",
)
2014-08-03 19:50:39 +02:00
}
func (c *RecordCommand) Execute(args []string) error {
var path string
var upload bool
var err error
if len(args) > 0 {
path = args[0]
upload = false
} else {
path, err = tmpPath()
if err != nil {
return err
}
upload = true
2014-08-03 19:50:39 +02:00
}
err = c.Recorder.Record(path, c.Command, c.Title, c.MaxWait)
2014-08-03 19:50:39 +02:00
if err != nil {
return err
}
if upload {
if !c.NoConfirm {
util.Printf("Press <Enter> to upload, <Ctrl-C> to cancel.")
util.ReadLine()
}
2014-08-03 19:50:39 +02:00
url, err := c.API.UploadAsciicast(path)
if err != nil {
util.Warningf("Upload failed, asciicast saved at %v", path)
util.Warningf("Retry later by executing: asciinema upload %v", path)
return err
}
2014-08-03 19:50:39 +02:00
os.Remove(path)
fmt.Println(url)
2014-08-03 19:50:39 +02:00
}
return nil
}
func defaultRecCommand(recCommand string) string {
if recCommand == "" {
recCommand = os.Getenv("SHELL")
if recCommand == "" {
recCommand = "/bin/sh"
}
}
return recCommand
}
func tmpPath() (string, error) {
2015-02-24 17:02:53 +01:00
file, err := ioutil.TempFile("", "asciicast-")
if err != nil {
return "", err
}
2015-02-25 16:19:21 +01:00
defer file.Close()
return file.Name(), nil
2014-08-03 19:50:39 +02:00
}