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"
|
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-03-08 18:00:05 +01:00
|
|
|
const Version = "1.0.0.rc2"
|
2015-03-05 15:37:35 +01:00
|
|
|
|
|
|
|
|
var GitCommit string // populated during build
|
|
|
|
|
|
2015-03-03 16:13:33 +01:00
|
|
|
var usage = `Record and share your terminal sessions, the right way.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
asciinema rec [-c <command>] [-t <title>] [-w <sec>] [-y] [<filename>]
|
|
|
|
|
asciinema play <filename>
|
|
|
|
|
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
|
|
|
|
|
-y, --yes Answer yes to all prompts (e.g. upload confirmation)
|
|
|
|
|
-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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func uintArg(args map[string]interface{}, name string) uint {
|
|
|
|
|
val := args[name]
|
|
|
|
|
|
|
|
|
|
if val != nil {
|
|
|
|
|
number, err := strconv.ParseUint(val.(string), 10, 0)
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
return uint(number)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatVersion() string {
|
|
|
|
|
var commitInfo string
|
|
|
|
|
|
|
|
|
|
if GitCommit != "" {
|
|
|
|
|
commitInfo = "-" + GitCommit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("asciinema %v%v\n", Version, commitInfo)
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-02 22:27:17 +02:00
|
|
|
func main() {
|
2015-02-02 19:07:52 +01:00
|
|
|
if !util.IsUtf8Locale() {
|
|
|
|
|
fmt.Println("asciinema needs a UTF-8 native locale to run. Check the output of `locale` command.")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-12 20:58:14 +01:00
|
|
|
cfg, err := util.LoadConfig()
|
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
|
|
|
|
2014-11-15 13:33:20 +01:00
|
|
|
api := api.New(cfg.API.URL, cfg.API.Token, 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-05 16:30:55 +01:00
|
|
|
command := util.FirstNonBlank(stringArg(args, "--command"), cfg.Record.Command, os.Getenv("SHELL"), "/bin/sh")
|
2015-03-03 16:13:33 +01:00
|
|
|
title := stringArg(args, "--title")
|
|
|
|
|
assumeYes := boolArg(args, "--yes")
|
|
|
|
|
maxWait := uintArg(args, "--max-wait")
|
|
|
|
|
filename := stringArg(args, "<filename>")
|
|
|
|
|
cmd := commands.NewRecordCommand(api, cfg)
|
|
|
|
|
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":
|
|
|
|
|
filename := stringArg(args, "<filename>")
|
|
|
|
|
cmd := commands.NewPlayCommand()
|
|
|
|
|
err = cmd.Execute(filename)
|
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":
|
|
|
|
|
cmd := commands.NewAuthCommand(cfg)
|
|
|
|
|
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
|
|
|
}
|