Debian Codegraph @

cmd/cobra.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package caddycmd
2
3 import (
4 "fmt"
5
6 "github.com/spf13/cobra"
7
8 "github.com/caddyserver/caddy/v2"
9 )
10
11 var defaultFactory = newRootCommandFactory(func() *cobra.Command {
12 bin := caddy.CustomBinaryName
13 if bin == "" {
14 bin = "caddy"
15 }
16
•17 short := "An extensible server platform"
18 long := caddy.CustomLongDescription
19 if long == "" {
20 long = `Caddy is an extensible server platform written in Go.
21
22 At its core, Caddy merely manages configuration. Modules are plugged
23 in statically at compile-time to provide useful functionality. Caddy's
24 standard distribution includes common modules to serve HTTP, TLS,
25 and PKI applications, including the automation of certificates.
26
27 To run Caddy, use:
28
29 - 'caddy run' to run Caddy in the foreground (recommended).
30 - 'caddy start' to start Caddy in the background; only do this
31 if you will be keeping the terminal window open until you run
32 'caddy stop' to close the server.
33
34 When Caddy is started, it opens a locally-bound administrative socket
35 to which configuration can be POSTed via a restful HTTP API (see
36 https://caddyserver.com/docs/api).
37
38 Caddy's native configuration format is JSON. However, config adapters
39 can be used to convert other config formats to JSON when Caddy receives
40 its configuration. The Caddyfile is a built-in config adapter that is
41 popular for hand-written configurations due to its straightforward
42 syntax (see https://caddyserver.com/docs/caddyfile). Many third-party
43 adapters are available (see https://caddyserver.com/docs/config-adapters).
44 Use 'caddy adapt' to see how a config translates to JSON.
45
46 For convenience, the CLI can act as an HTTP client to give Caddy its
47 initial configuration for you. If a file named Caddyfile is in the
48 current working directory, it will do this automatically. Otherwise,
49 you can use the --config flag to specify the path to a config file.
50
51 Some special-purpose subcommands build and load a configuration file
52 for you directly from command line input; for example:
53
54 - caddy file-server
55 - caddy reverse-proxy
56 - caddy respond
57
58 These commands disable the administration endpoint because their
59 configuration is specified solely on the command line.
60
61 In general, the most common way to run Caddy is simply:
62
63 $ caddy run
64
65 Or, with a configuration file:
66
67 $ caddy run --config caddy.json
68
69 If running interactively in a terminal, running Caddy in the
70 background may be more convenient:
71
72 $ caddy start
73 ...
74 $ caddy stop
75
76 This allows you to run other commands while Caddy stays running.
77 Be sure to stop Caddy before you close the terminal!
78
79 Depending on the system, Caddy may need permission to bind to low
80 ports. One way to do this on Linux is to use setcap:
81
82 $ sudo setcap cap_net_bind_service=+ep $(which caddy)
83
84 Remember to run that command again after replacing the binary.
85
86 See the Caddy website for tutorials, configuration structure,
87 syntax, and module documentation: https://caddyserver.com/docs/
88
89 Custom Caddy builds are available on the Caddy download page at:
90 https://caddyserver.com/download
91
92 The xcaddy command can be used to build Caddy from source with or
93 without additional plugins: https://github.com/caddyserver/xcaddy
94
95 Where possible, Caddy should be installed using officially-supported
96 package installers: https://caddyserver.com/docs/install
97
98 Instructions for running Caddy in production are also available:
99 https://caddyserver.com/docs/running
100 `
101 }
102
103 return &cobra.Command{
104 Use: bin,
•105 Short: short,
106 Long: long,
107 Example: ` $ caddy run
108 $ caddy run --config caddy.json
109 $ caddy reload --config caddy.json
110 $ caddy stop`,
111
112 // kind of annoying to have all the help text printed out if
113 // caddy has an error provisioning its modules, for instance...
114 SilenceUsage: true,
115 Version: onlyVersionText(),
116 }
117 })
118
119 const fullDocsFooter = `Full documentation is available at:
120 https://caddyserver.com/docs/command-line`
121
122 func init() {
123 defaultFactory.Use(func(rootCmd *cobra.Command) {
124 rootCmd.SetVersionTemplate("{{.Version}}\n")
125 rootCmd.SetHelpTemplate(rootCmd.HelpTemplate() + "\n" + fullDocsFooter + "\n")
126 })
127 }
128
129 func onlyVersionText() string {
130 _, f := caddy.Version()
131 return f
132 }
133
134 func caddyCmdToCobra(caddyCmd Command) *cobra.Command {
135 cmd := &cobra.Command{
136 Use: caddyCmd.Name + " " + caddyCmd.Usage,
137 Short: caddyCmd.Short,
138 Long: caddyCmd.Long,
139 }
140 if caddyCmd.CobraFunc != nil {
141 caddyCmd.CobraFunc(cmd)
142 } else {
143 cmd.RunE = WrapCommandFuncForCobra(caddyCmd.Func)
144 cmd.Flags().AddGoFlagSet(caddyCmd.Flags)
145 }
146 return cmd
147 }
148
149 // WrapCommandFuncForCobra wraps a Caddy CommandFunc for use
150 // in a cobra command's RunE field.
151 func WrapCommandFuncForCobra(f CommandFunc) func(cmd *cobra.Command, _ []string) error {
152 return func(cmd *cobra.Command, _ []string) error {
153 status, err := f(Flags{cmd.Flags()})
154 if status > 1 {
155 cmd.SilenceErrors = true
156 return &exitError{ExitCode: status, Err: err}
157 }
158 return err
159 }
160 }
161
162 // exitError carries the exit code from CommandFunc to Main()
163 type exitError struct {
164 ExitCode int
165 Err error
166 }
167
168 func (e *exitError) Error() string {
169 if e.Err == nil {
170 return fmt.Sprintf("exiting with status %d", e.ExitCode)
171 }
172 return e.Err.Error()
173 }