2014-08-03 19:50:39 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2015-02-24 16:22:31 +01:00
|
|
|
"io/ioutil"
|
2014-08-03 19:50:39 +02:00
|
|
|
"os"
|
|
|
|
|
|
2015-03-05 15:57:12 +01:00
|
|
|
"github.com/asciinema/asciinema/api"
|
|
|
|
|
"github.com/asciinema/asciinema/asciicast"
|
|
|
|
|
"github.com/asciinema/asciinema/util"
|
2014-08-03 19:50:39 +02:00
|
|
|
)
|
|
|
|
|
|
2014-11-12 20:54:50 +01:00
|
|
|
type RecordCommand struct {
|
2015-03-03 16:13:33 +01:00
|
|
|
API api.API
|
2015-03-11 12:07:26 +01:00
|
|
|
Env map[string]string
|
2015-03-03 16:13:33 +01:00
|
|
|
Recorder asciicast.Recorder
|
2014-11-12 20:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-11 12:10:48 +01:00
|
|
|
func NewRecordCommand(api api.API, env map[string]string) *RecordCommand {
|
2014-11-12 20:54:50 +01:00
|
|
|
return &RecordCommand{
|
2014-11-15 13:33:20 +01:00
|
|
|
API: api,
|
2015-03-11 12:07:26 +01:00
|
|
|
Env: env,
|
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
|
|
|
|
2016-05-15 20:18:00 +02:00
|
|
|
func (c *RecordCommand) Execute(command, title string, assumeYes bool, maxWait float64, filename string) error {
|
2015-02-24 16:22:31 +01:00
|
|
|
var upload bool
|
|
|
|
|
var err error
|
|
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
if filename != "" {
|
2015-02-24 16:22:31 +01:00
|
|
|
upload = false
|
|
|
|
|
} else {
|
2015-03-03 16:13:33 +01:00
|
|
|
filename, err = tmpPath()
|
2015-02-24 16:22:31 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
upload = true
|
2014-08-03 19:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
2015-03-11 12:07:26 +01:00
|
|
|
err = c.Recorder.Record(filename, command, title, maxWait, assumeYes, c.Env)
|
2014-08-03 19:50:39 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 16:22:31 +01:00
|
|
|
if upload {
|
2015-03-03 16:13:33 +01:00
|
|
|
if !assumeYes {
|
2015-02-24 16:22:31 +01:00
|
|
|
util.Printf("Press <Enter> to upload, <Ctrl-C> to cancel.")
|
|
|
|
|
util.ReadLine()
|
|
|
|
|
}
|
2014-08-03 19:50:39 +02:00
|
|
|
|
2016-02-21 16:58:39 +01:00
|
|
|
var url, warn string
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
util.WithSpinner(0, func() {
|
|
|
|
|
url, warn, err = c.API.UploadAsciicast(filename)
|
|
|
|
|
})
|
2015-03-09 12:18:15 +01:00
|
|
|
|
|
|
|
|
if warn != "" {
|
|
|
|
|
util.Warningf(warn)
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 16:22:31 +01:00
|
|
|
if err != nil {
|
2015-03-03 16:13:33 +01:00
|
|
|
util.Warningf("Upload failed, asciicast saved at %v", filename)
|
|
|
|
|
util.Warningf("Retry later by executing: asciinema upload %v", filename)
|
2015-02-24 16:22:31 +01:00
|
|
|
return err
|
|
|
|
|
}
|
2014-08-03 19:50:39 +02:00
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
os.Remove(filename)
|
2015-02-24 16:22:31 +01:00
|
|
|
fmt.Println(url)
|
2014-08-03 19:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|