Files
asciinema/commands/rec.go

80 lines
1.4 KiB
Go
Raw Normal View History

2014-08-03 19:50:39 +02:00
package commands
import (
"fmt"
"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
Env map[string]string
2015-03-03 16:13:33 +01:00
Recorder asciicast.Recorder
2014-11-12 20:54:50 +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,
Env: env,
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
2015-03-03 16:13:33 +01:00
func (c *RecordCommand) Execute(command, title string, assumeYes bool, maxWait uint, filename string) error {
var upload bool
var err error
2015-03-03 16:13:33 +01:00
if filename != "" {
upload = false
} else {
2015-03-03 16:13:33 +01:00
filename, err = tmpPath()
if err != nil {
return err
}
upload = true
2014-08-03 19:50:39 +02: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
}
if upload {
2015-03-03 16:13:33 +01:00
if !assumeYes {
util.Printf("Press <Enter> to upload, <Ctrl-C> to cancel.")
util.ReadLine()
}
2014-08-03 19:50:39 +02:00
url, warn, err := c.API.UploadAsciicast(filename)
if warn != "" {
util.Warningf(warn)
}
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)
return err
}
2014-08-03 19:50:39 +02:00
2015-03-03 16:13:33 +01:00
os.Remove(filename)
fmt.Println(url)
2014-08-03 19:50:39 +02:00
}
return nil
}
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
}