This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
/
cmdr.go
123 lines (100 loc) · 3.56 KB
/
cmdr.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
// Copyright © 2020 Hedzr Yeh.
package server
import (
"github.com/hedzr/cmdr"
"github.com/hedzr/cmdr-http2/cli/server/tls"
"github.com/hedzr/cmdr/plugin/daemon"
"github.com/sirupsen/logrus"
"net"
"time"
)
// AttachToCmdr adds command-line commands and options to cmdr system
func AttachToCmdr(root cmdr.OptCmd) {
// http 2 client
root.NewSubCommand().
Titles("h2", "h2-test").
Description("test http 2 client", "test http 2 client,\nverbose long descriptions here.").
Group("Test").
Action(func(cmd *cmdr.Command, args []string) (err error) {
runClient()
return
})
}
// WithCmdrDaemonSupport adds daemon support to cmdr system
func WithCmdrDaemonSupport() cmdr.ExecOption {
return daemon.WithDaemon(newDaemon(),
modifier, onAppStart, onAppExit,
daemon.WithOnGetListener(getOnGetListener),
)
}
// WithCmdrHook adds hooking to cmdr system so that we can modify daemon `server` command at an appropriate time.
func WithCmdrHook() cmdr.ExecOption {
return cmdr.WithXrefBuildingHooks(func(root *cmdr.RootCommand, args []string) {
// app.server.port
if cx := root.Command.FindSubCommand("server"); cx != nil {
cx.Description = "daemon service: HTTP2 server"
// logrus.Debugf("`server` command found")
opt := cmdr.NewCmdFrom(cx)
if flg := cx.FindFlag("port"); flg != nil {
flg.DefaultValue = defaultPort
} else {
opt.NewFlagV(defaultPort, "port", "p").
Description("the port to listen.", "").
Group("").
Placeholder("PORT")
}
certOptCmd := opt.NewSubCommand("certs", "ca").
Description("certificates operations...", "").
Group("CA")
certCreateCmd := certOptCmd.NewSubCommand("create", "c").
Description("create CA, server and client certificates").
Action(tls.CertCreate)
certCreateCmd.NewFlagV([]string{}, "host", "h").
Description("Comma-separated hostnames and IPs to generate a certificate for")
certCreateCmd.NewFlagV("", "start-date", "f", "from", "valid-from").
Description("Creation date formatted as Jan 1 15:04:05 2011 (default now)")
certCreateCmd.NewFlagV(365*10*24*time.Hour, "valid-for", "duration", "d").
Description("Duration that certificate is valid for")
// caCmd := certOptCmd.NewSubCommand("ca").
// Description("certification tool (such as create-ca, create-cert, ...)", "certification tool (such as create-ca, create-cert, ... )\nverbose long descriptions here.").
// Group("CA")
//
// caCreateCmd := caCmd.NewSubCommand("create", "c").
// Description("create NEW CA certification", "").
// Group("Tool").
// Action(tls.CaCreate)
}
}, func(root *cmdr.RootCommand, args []string) {
logrus.Debugf("cmd: root=%+v, args: %v", root, args)
})
}
func modifier(daemonServerCommands *cmdr.Command) *cmdr.Command {
if startCmd := daemonServerCommands.FindSubCommand("start"); startCmd != nil {
startCmd.PreAction = onServerPreStart
startCmd.PostAction = onServerPostStop
}
return daemonServerCommands
}
func onAppStart(cmd *cmdr.Command, args []string) (err error) {
logrus.Debug("onAppStart")
return
}
func onAppExit(cmd *cmdr.Command, args []string) {
logrus.Debug("onAppExit")
}
func onServerPostStop(cmd *cmdr.Command, args []string) {
logrus.Debug("onServerPostStop")
}
// onServerPreStart is earlier than onAppStart.
func onServerPreStart(cmd *cmdr.Command, args []string) (err error) {
// earlierInitLogger() // deprecated by cmdr.WithLogex()
logrus.Debug("onServerPreStart")
return
}
func getOnGetListener() net.Listener {
// l := h2listener
// h2listener = nil
// return l
return h2listener
}
var h2listener net.Listener