forked from asciinema/asciinema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (130 loc) · 3.75 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"github.com/docopt/docopt-go"
"github.com/sykesm/asciinema/api"
"github.com/sykesm/asciinema/commands"
"github.com/sykesm/asciinema/config"
"github.com/sykesm/asciinema/util"
)
const Version = "1.2.1-sykesm"
var usage = `Record and share your terminal sessions, the right way.
Usage:
asciinema rec [-c <command>] [-t <title>] [-w <sec>] [-y] [-q] [<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 (can be fractional)
-y, --yes Answer "yes" to all prompts (e.g. upload confirmation)
-q, --quiet Be quiet, suppress all notices/warnings (implies -y)
-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 floatArg(args map[string]interface{}, name string, defaultValue float64) float64 {
val := args[name]
if val != nil {
number, err := strconv.ParseFloat(val.(string), 64)
if err == nil {
return float64(number)
}
}
return defaultValue
}
func formatVersion() string {
return fmt.Sprintf("asciinema %v", Version)
}
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 showCursorBack() {
fmt.Fprintf(os.Stdout, "\x1b[?25h")
}
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)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
showCursorBack()
os.Exit(1)
}()
defer showCursorBack()
cfg, err := config.Get(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")
if boolArg(args, "--quiet") {
util.BeQuiet()
assumeYes = true
}
maxWait := floatArg(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 := floatArg(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)
}
}