2014-08-02 22:27:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2014-08-09 23:39:23 +02:00
|
|
|
"fmt"
|
2014-08-02 22:27:17 +02:00
|
|
|
"os"
|
2015-03-03 16:13:33 +01:00
|
|
|
"strconv"
|
2015-03-11 12:07:26 +01:00
|
|
|
"strings"
|
2014-08-02 22:27:17 +02:00
|
|
|
|
2015-03-05 15:57:12 +01:00
|
|
|
"github.com/asciinema/asciinema/Godeps/_workspace/src/github.com/docopt/docopt-go"
|
|
|
|
|
"github.com/asciinema/asciinema/api"
|
|
|
|
|
"github.com/asciinema/asciinema/commands"
|
|
|
|
|
"github.com/asciinema/asciinema/util"
|
2014-08-02 22:27:17 +02:00
|
|
|
)
|
|
|
|
|
|
2015-06-21 17:58:14 +02:00
|
|
|
const Version = "1.1.1"
|
2015-03-05 15:37:35 +01:00
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
var usage = `Record and share your terminal sessions, the right way.
|
|
|
|
|
|
|
|
|
|
Usage:
|
2015-08-08 21:28:41 +02:00
|
|
|
asciinema rec [-c <command>] [-t <title>] [-w <sec>] [-y] [-q] [<filename>]
|
2015-05-05 19:44:31 -07:00
|
|
|
asciinema play [-w <sec>] <filename>
|
2015-03-03 16:13:33 +01:00
|
|
|
asciinema upload <filename>
|
|
|
|
|
asciinema auth
|
|
|
|
|
asciinema -h | --help
|
|
|
|
|
asciinema --version
|
|
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
|
rec Record terminal session
|
|
|
|
|
play Replay terminal session
|
|
|
|
|
upload Upload locally saved terminal session to asciinema.org
|
|
|
|
|
auth Assign local API token to asciinema.org account
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-c, --command=<command> Specify command to record, defaults to $SHELL
|
|
|
|
|
-t, --title=<title> Specify title of the asciicast
|
|
|
|
|
-w, --max-wait=<sec> Reduce recorded terminal inactivity to max <sec> seconds
|
2015-08-08 21:28:41 +02:00
|
|
|
-y, --yes Answer "yes" to all prompts (e.g. upload confirmation)
|
|
|
|
|
-q, --quiet Be quiet, suppress all notices/warnings (implies -y)
|
2015-03-03 16:13:33 +01:00
|
|
|
-h, --help Show this message
|
|
|
|
|
--version Show version`
|
|
|
|
|
|
|
|
|
|
func cmdName(args map[string]interface{}) string {
|
|
|
|
|
for _, cmd := range []string{"rec", "play", "upload", "auth"} {
|
|
|
|
|
if args[cmd].(bool) {
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stringArg(args map[string]interface{}, name string) string {
|
|
|
|
|
val := args[name]
|
|
|
|
|
|
|
|
|
|
if val != nil {
|
|
|
|
|
return val.(string)
|
|
|
|
|
} else {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func boolArg(args map[string]interface{}, name string) bool {
|
|
|
|
|
return args[name].(bool)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 16:44:15 +01:00
|
|
|
func uintArg(args map[string]interface{}, name string, defaultValue uint) uint {
|
2015-03-03 16:13:33 +01:00
|
|
|
val := args[name]
|
|
|
|
|
|
|
|
|
|
if val != nil {
|
|
|
|
|
number, err := strconv.ParseUint(val.(string), 10, 0)
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
return uint(number)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 16:44:15 +01:00
|
|
|
return defaultValue
|
2015-03-03 16:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatVersion() string {
|
2015-07-02 21:41:27 +02:00
|
|
|
return fmt.Sprintf("asciinema %v", Version)
|
2015-03-03 16:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-11 12:07:26 +01:00
|
|
|
func environment() map[string]string {
|
|
|
|
|
env := map[string]string{}
|
|
|
|
|
|
|
|
|
|
for _, keyval := range os.Environ() {
|
|
|
|
|
pair := strings.SplitN(keyval, "=", 2)
|
|
|
|
|
env[pair[0]] = pair[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return env
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-02 22:27:17 +02:00
|
|
|
func main() {
|
2015-03-11 12:07:26 +01:00
|
|
|
env := environment()
|
|
|
|
|
|
|
|
|
|
if !util.IsUtf8Locale(env) {
|
2015-02-02 19:07:52 +01:00
|
|
|
fmt.Println("asciinema needs a UTF-8 native locale to run. Check the output of `locale` command.")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 12:49:26 +01:00
|
|
|
cfg, err := util.GetConfig(env)
|
2014-11-12 20:54:50 +01:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2014-08-09 23:39:23 +02:00
|
|
|
|
2015-03-12 12:22:42 +01:00
|
|
|
api := api.New(cfg.ApiUrl(), env["USER"], cfg.ApiToken(), Version)
|
2015-03-03 16:13:33 +01:00
|
|
|
args, _ := docopt.Parse(usage, nil, true, formatVersion(), false)
|
2014-09-07 11:59:13 +02:00
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
switch cmdName(args) {
|
|
|
|
|
case "rec":
|
2015-03-12 12:22:42 +01:00
|
|
|
command := util.FirstNonBlank(stringArg(args, "--command"), cfg.RecordCommand())
|
2015-03-03 16:13:33 +01:00
|
|
|
title := stringArg(args, "--title")
|
2015-03-12 12:22:42 +01:00
|
|
|
assumeYes := cfg.RecordYes() || boolArg(args, "--yes")
|
2015-08-08 21:28:41 +02:00
|
|
|
|
|
|
|
|
if boolArg(args, "--quiet") {
|
|
|
|
|
util.BeQuiet()
|
|
|
|
|
assumeYes = true
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 12:22:42 +01:00
|
|
|
maxWait := uintArg(args, "--max-wait", cfg.RecordMaxWait())
|
2015-03-03 16:13:33 +01:00
|
|
|
filename := stringArg(args, "<filename>")
|
2015-03-11 12:10:48 +01:00
|
|
|
cmd := commands.NewRecordCommand(api, env)
|
2015-03-03 16:13:33 +01:00
|
|
|
err = cmd.Execute(command, title, assumeYes, maxWait, filename)
|
2014-08-09 23:39:23 +02:00
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
case "play":
|
2015-05-10 13:50:43 +02:00
|
|
|
maxWait := uintArg(args, "--max-wait", cfg.PlayMaxWait())
|
2015-03-03 16:13:33 +01:00
|
|
|
filename := stringArg(args, "<filename>")
|
|
|
|
|
cmd := commands.NewPlayCommand()
|
2015-05-05 19:44:31 -07:00
|
|
|
err = cmd.Execute(filename, maxWait)
|
2014-08-09 23:39:23 +02:00
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
case "upload":
|
|
|
|
|
filename := stringArg(args, "<filename>")
|
|
|
|
|
cmd := commands.NewUploadCommand(api)
|
|
|
|
|
err = cmd.Execute(filename)
|
2014-08-09 23:39:23 +02:00
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
case "auth":
|
2015-03-12 12:31:46 +01:00
|
|
|
cmd := commands.NewAuthCommand(api)
|
2015-03-03 16:13:33 +01:00
|
|
|
err = cmd.Execute()
|
|
|
|
|
}
|
2014-12-17 11:40:17 +01:00
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error: %v\n", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2014-08-09 23:39:23 +02:00
|
|
|
}
|