Files
asciinema/main.go

135 lines
3.2 KiB
Go
Raw Normal View History

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)
}
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 {
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() {
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)
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")
2015-03-09 16:44:15 +01:00
assumeYes := cfg.Record.Yes || boolArg(args, "--yes")
maxWait := uintArg(args, "--max-wait", cfg.Record.MaxWait)
2015-03-03 16:13:33 +01:00
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
}