/
main.go
149 lines (117 loc) · 3.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"os"
"strconv"
"strings"
"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"
)
const Version = "1.1.1"
var GitCommit string // initialized during build
var usage = `Record and share your terminal sessions, the right way.
Usage:
asciinema rec [-c <command>] [-t <title>] [-w <sec>] [-y] [<filename>]
asciinema play [-w <sec>] <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, defaultValue uint) uint {
val := args[name]
if val != nil {
number, err := strconv.ParseUint(val.(string), 10, 0)
if err == nil {
return uint(number)
}
}
return defaultValue
}
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
}
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.GetConfig(env)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
api := api.New(cfg.ApiUrl(), env["USER"], cfg.ApiToken(), Version)
args, _ := docopt.Parse(usage, nil, true, formatVersion(), false)
switch cmdName(args) {
case "rec":
command := util.FirstNonBlank(stringArg(args, "--command"), cfg.RecordCommand())
title := stringArg(args, "--title")
assumeYes := cfg.RecordYes() || boolArg(args, "--yes")
maxWait := uintArg(args, "--max-wait", cfg.RecordMaxWait())
filename := stringArg(args, "<filename>")
cmd := commands.NewRecordCommand(api, env)
err = cmd.Execute(command, title, assumeYes, maxWait, filename)
case "play":
maxWait := uintArg(args, "--max-wait", cfg.PlayMaxWait())
filename := stringArg(args, "<filename>")
cmd := commands.NewPlayCommand()
err = cmd.Execute(filename, maxWait)
case "upload":
filename := stringArg(args, "<filename>")
cmd := commands.NewUploadCommand(api)
err = cmd.Execute(filename)
case "auth":
cmd := commands.NewAuthCommand(api)
err = cmd.Execute()
}
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}