2014-08-03 19:50:39 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
2015-02-24 16:22:31 +01:00
|
|
|
"io/ioutil"
|
2014-08-03 19:50:39 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/asciinema/asciinema-cli/api"
|
2015-02-24 16:22:31 +01:00
|
|
|
"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
|
2015-02-24 16:22:31 +01:00
|
|
|
Recorder asciicast.Recorder
|
2014-11-12 20:54:50 +01:00
|
|
|
Command string
|
|
|
|
|
Title string
|
|
|
|
|
NoConfirm bool
|
2014-11-28 23:59:41 +01:00
|
|
|
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,
|
2015-02-24 16:22:31 +01:00
|
|
|
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,
|
2014-09-07 12:00:01 +02:00
|
|
|
"upload without asking for confirmation",
|
2014-08-03 19:50:39 +02:00
|
|
|
)
|
2014-11-28 23:59:41 +01: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-11-28 23:59:41 +01:00
|
|
|
)
|
2014-08-03 19:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *RecordCommand) Execute(args []string) error {
|
2015-02-24 16:22:31 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2015-02-24 16:22:31 +01: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
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 16:22:31 +01:00
|
|
|
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
|
|
|
|
2015-02-24 16:22:31 +01:00
|
|
|
url, err := c.API.UploadAsciicast(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
util.Warningf("Upload failed, asciicast saved at %v", path)
|
2015-02-24 17:00:05 +01:00
|
|
|
util.Warningf("Retry later by executing: asciinema upload %v", path)
|
2015-02-24 16:22:31 +01:00
|
|
|
return err
|
|
|
|
|
}
|
2014-08-03 19:50:39 +02:00
|
|
|
|
2015-02-24 16:22:31 +01: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
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 16:22:31 +01:00
|
|
|
func tmpPath() (string, error) {
|
2015-02-24 17:02:53 +01:00
|
|
|
file, err := ioutil.TempFile("", "asciicast-")
|
2015-02-24 16:22:31 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2014-11-28 23:59:41 +01:00
|
|
|
}
|
2015-02-25 16:19:21 +01:00
|
|
|
defer file.Close()
|
2014-11-28 23:59:41 +01:00
|
|
|
|
2015-02-24 16:22:31 +01:00
|
|
|
return file.Name(), nil
|
2014-08-03 19:50:39 +02:00
|
|
|
}
|