-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcli.go
118 lines (99 loc) · 3.09 KB
/
cli.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
package revealgo
import (
"fmt"
"io"
"os"
"golang.org/x/crypto/bcrypt"
flags "github.com/jessevdk/go-flags"
)
const (
ExitCodeOK = 0
ExitCodeError = 1
)
type CLI struct {
OutStream, ErrStream io.Writer
options *CLIOptions
parser *flags.Parser
}
type CLIOptions struct {
Port int `short:"p" long:"port" description:"TCP port number of this server" default:"3000"`
Theme string `long:"theme" description:"Slide theme or original css file name. default themes: beige, black, blood, league, moon, night, serif, simple, sky, solarized, and white" default:"black.css"`
DisableAutoOpen bool `long:"disable-auto-open" description:"Disable automatic opening of the browser"`
Transition string `long:"transition" description:"Transition effect for slides: default, cube, page, concave, zoom, linear, fade, none" default:"default"`
Separator string `long:"separator" description:"Horizontal slide separator characters" default:"^---"`
VerticalSeparator string `long:"vertical-separator" description:"Vertical slide separator characters" default:"^___"`
Multiplex bool `long:"multiplex" description:"Enable slide multiplexing"`
Version bool `short:"v" long:"version" description:"Show the version"`
}
func (cli *CLI) Run(args []string) int {
cli.init()
args, err := cli.parser.ParseArgs(args)
if err != nil {
flagError := err.(*flags.Error)
if flagError.Type == flags.ErrHelp {
cli.showHelp()
return ExitCodeOK
}
if flagError.Type == flags.ErrUnknownFlag {
fmt.Fprintf(cli.OutStream, "Use --help to view all available options.\n")
} else {
fmt.Fprintf(cli.OutStream, "Error parsing flags: %s\n", err)
}
return ExitCodeError
}
if cli.options.Version {
cli.showVersion()
return ExitCodeOK
}
if len(args) == 0 {
cli.showHelp()
return ExitCodeOK
}
cli.serve(args)
return ExitCodeOK
}
func (cli *CLI) init() {
cli.options = &CLIOptions{}
cli.parser = cli.newParser()
}
func (cli *CLI) serve(args []string) {
opts := cli.options
_, err := os.Stat(opts.Theme)
originalTheme := false
if err == nil {
originalTheme = true
}
server := Server{
port: opts.Port,
}
param := ServerParam{
Path: args[0],
Theme: addExtention(opts.Theme, "css"),
Transition: opts.Transition,
OriginalTheme: originalTheme,
DisableAutoOpen: opts.DisableAutoOpen,
Separator: opts.Separator,
VerticalSeparator: opts.VerticalSeparator,
}
if opts.Multiplex {
password := "this-is-not-a-secret"
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
param.Multiplex = MultiplexParam{
Secret: password,
Identifier: string(bytes),
}
}
server.Serve(param)
}
func (cli *CLI) showHelp() {
cli.parser.WriteHelp(cli.OutStream)
}
func (cli *CLI) showVersion() {
fmt.Fprintf(cli.OutStream, "revealgo version %s\n", Version)
}
func (cli *CLI) newParser() *flags.Parser {
parser := flags.NewParser(cli.options, flags.HelpFlag|flags.PassDoubleDash)
parser.Name = "revealgo"
parser.Usage = "[options] [MARKDOWN FILE]"
return parser
}