Files
asciinema/main.go

149 lines
3.4 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"
"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-03-08 18:00:05 +01:00
const Version = "1.0.0.rc2"
2015-03-05 15:37:35 +01:00
2015-03-12 12:21:31 +01:00
var GitCommit string // initialized during build
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:
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)
}
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() {
env := environment()
if !util.IsUtf8Locale(env) {
fmt.Println("asciinema needs a UTF-8 native locale to run. Check the output of `locale` command.")
os.Exit(1)
}
cfg, err := util.LoadConfig(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
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)
2015-03-03 16:13:33 +01:00
switch cmdName(args) {
case "rec":
command := util.FirstNonBlank(stringArg(args, "--command"), cfg.RecordCommand())
2015-03-03 16:13:33 +01:00
title := stringArg(args, "--title")
assumeYes := cfg.RecordYes() || boolArg(args, "--yes")
maxWait := uintArg(args, "--max-wait", cfg.RecordMaxWait())
2015-03-03 16:13:33 +01:00
filename := stringArg(args, "<filename>")
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":
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
}