2019-07-01 06:07:58 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-06-29 05:39:41 +08:00
|
|
|
package caddycmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2019-10-01 11:23:58 +08:00
|
|
|
"regexp"
|
2019-06-29 05:39:41 +08:00
|
|
|
)
|
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
// Command represents a subcommand. Name, Func,
|
|
|
|
// and Short are required.
|
2019-10-01 11:23:58 +08:00
|
|
|
type Command struct {
|
2019-10-02 01:02:13 +08:00
|
|
|
// The name of the subcommand. Must conform to the
|
|
|
|
// format described by the RegisterCommand() godoc.
|
|
|
|
// Required.
|
2019-10-01 11:23:58 +08:00
|
|
|
Name string
|
2019-06-29 05:39:41 +08:00
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
// Run is a function that executes a subcommand using
|
|
|
|
// the parsed flags. It returns an exit code and any
|
|
|
|
// associated error.
|
|
|
|
// Required.
|
2019-10-01 11:23:58 +08:00
|
|
|
Func CommandFunc
|
2019-06-29 05:39:41 +08:00
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
// Usage is a brief message describing the syntax of
|
|
|
|
// the subcommand's flags and args. Use [] to indicate
|
|
|
|
// optional parameters and <> to enclose literal values
|
|
|
|
// intended to be replaced by the user. Do not prefix
|
|
|
|
// the string with "caddy" or the name of the command
|
|
|
|
// since these will be prepended for you; only include
|
|
|
|
// the actual parameters for this command.
|
2019-10-01 11:23:58 +08:00
|
|
|
Usage string
|
2019-06-29 05:39:41 +08:00
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
// Short is a one-line message explaining what the
|
|
|
|
// command does. Should not end with punctuation.
|
|
|
|
// Required.
|
2019-10-01 11:23:58 +08:00
|
|
|
Short string
|
2019-06-29 05:39:41 +08:00
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
// Long is the full help text shown to the user.
|
|
|
|
// Will be trimmed of whitespace on both ends before
|
|
|
|
// being printed.
|
2019-10-01 11:23:58 +08:00
|
|
|
Long string
|
2019-06-29 05:39:41 +08:00
|
|
|
|
2019-10-02 01:02:13 +08:00
|
|
|
// Flags is the flagset for command.
|
2019-10-01 11:23:58 +08:00
|
|
|
Flags *flag.FlagSet
|
2019-06-29 05:39:41 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
// CommandFunc is a command's function. It runs the
|
|
|
|
// command and returns the proper exit code along with
|
|
|
|
// any error that occurred.
|
|
|
|
type CommandFunc func(Flags) (int, error)
|
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
var commands = make(map[string]Command)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterCommand(Command{
|
|
|
|
Name: "help",
|
|
|
|
Func: cmdHelp,
|
|
|
|
Usage: "<command>",
|
|
|
|
Short: "Shows help for a Caddy subcommand",
|
|
|
|
})
|
|
|
|
|
|
|
|
RegisterCommand(Command{
|
2019-10-01 11:23:58 +08:00
|
|
|
Name: "start",
|
|
|
|
Func: cmdStart,
|
2019-10-02 01:02:13 +08:00
|
|
|
Usage: "[--config <path> [[--adapter <name>]]",
|
|
|
|
Short: "Starts the Caddy process in the background and then returns",
|
2019-10-01 11:23:58 +08:00
|
|
|
Long: `
|
2019-10-02 01:02:13 +08:00
|
|
|
Starts the Caddy process, optionally bootstrapped with an initial config file.
|
|
|
|
This command unblocks after the server starts running or fails to run.
|
|
|
|
|
|
|
|
On Windows, the spawned child process will remain attached to the terminal, so
|
|
|
|
closing the window will forcefully stop Caddy; to avoid forgetting this, try
|
|
|
|
using 'caddy run' instead to keep it in the foreground.`,
|
2019-10-01 11:23:58 +08:00
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("start", flag.ExitOnError)
|
|
|
|
fs.String("config", "", "Configuration file")
|
2019-10-01 13:43:39 +08:00
|
|
|
fs.String("adapter", "", "Name of config adapter to apply")
|
2019-10-01 11:23:58 +08:00
|
|
|
return fs
|
|
|
|
}(),
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 11:23:58 +08:00
|
|
|
Name: "run",
|
|
|
|
Func: cmdRun,
|
2019-10-02 01:02:13 +08:00
|
|
|
Usage: "[--config <path> [--adapter <name>]] [--environ]",
|
|
|
|
Short: `Starts the Caddy process and blocks indefinitely`,
|
2019-10-01 11:23:58 +08:00
|
|
|
Long: `
|
2019-10-02 01:02:13 +08:00
|
|
|
Starts the Caddy process, optionally bootstrapped with an initial config file,
|
|
|
|
and blocks indefinitely until the server is stopped; i.e. runs Caddy in
|
|
|
|
"daemon" mode (foreground).
|
2019-10-01 11:23:58 +08:00
|
|
|
|
|
|
|
If a config file is specified, it will be applied immediately after the process
|
|
|
|
is running. If the config file is not in Caddy's native JSON format, you can
|
2019-10-01 13:43:39 +08:00
|
|
|
specify an adapter with --adapter to adapt the given config file to
|
2019-10-01 11:23:58 +08:00
|
|
|
Caddy's native format. The config adapter must be a registered module. Any
|
|
|
|
warnings will be printed to the log, but beware that any adaptation without
|
|
|
|
errors will immediately be used. If you want to review the results of the
|
2019-10-01 13:43:39 +08:00
|
|
|
adaptation first, use the 'adapt' subcommand.
|
2019-10-01 11:23:58 +08:00
|
|
|
|
|
|
|
As a special case, if the current working directory has a file called
|
2019-10-02 01:02:13 +08:00
|
|
|
"Caddyfile" and the caddyfile config adapter is plugged in (default), then
|
|
|
|
that file will be loaded and used to configure Caddy, even without any command
|
|
|
|
line flags.
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-01 13:43:39 +08:00
|
|
|
If --environ is specified, the environment as seen by the Caddy process will
|
2019-10-01 11:23:58 +08:00
|
|
|
be printed before starting. This is the same as the environ command but does
|
2019-10-02 01:02:13 +08:00
|
|
|
not quit after printing, and can be useful for troubleshooting.`,
|
2019-10-01 11:23:58 +08:00
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("run", flag.ExitOnError)
|
|
|
|
fs.String("config", "", "Configuration file")
|
2019-10-01 13:43:39 +08:00
|
|
|
fs.String("adapter", "", "Name of config adapter to apply")
|
|
|
|
fs.Bool("environ", false, "Print environment")
|
2019-10-01 11:23:58 +08:00
|
|
|
fs.String("pingback", "", "Echo confirmation bytes to this address on success")
|
|
|
|
return fs
|
|
|
|
}(),
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 11:23:58 +08:00
|
|
|
Name: "stop",
|
|
|
|
Func: cmdStop,
|
2019-10-29 04:39:37 +08:00
|
|
|
Short: "Gracefully stops a started Caddy process",
|
2019-10-02 01:02:13 +08:00
|
|
|
Long: `
|
2019-10-29 04:39:37 +08:00
|
|
|
Stops the background Caddy process as gracefully as possible.
|
2019-10-02 01:02:13 +08:00
|
|
|
|
2019-12-24 03:41:05 +08:00
|
|
|
It requires that the admin API is enabled and accessible, since it will
|
|
|
|
use the API's /stop endpoint. The address of this request can be
|
|
|
|
customized using the --address flag if it is not the default.`,
|
2019-11-16 06:45:18 +08:00
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("stop", flag.ExitOnError)
|
|
|
|
fs.String("address", "", "The address to use to reach the admin API endpoint, if not the default")
|
|
|
|
return fs
|
|
|
|
}(),
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 11:23:58 +08:00
|
|
|
Name: "reload",
|
|
|
|
Func: cmdReload,
|
2019-10-01 13:43:39 +08:00
|
|
|
Usage: "--config <path> [--adapter <name>] [--address <interface>]",
|
2019-10-02 01:02:13 +08:00
|
|
|
Short: "Changes the config of the running Caddy instance",
|
|
|
|
Long: `
|
|
|
|
Gives the running Caddy instance a new configuration. This has the same effect
|
|
|
|
as POSTing a document to the /load API endpoint, but is convenient for simple
|
|
|
|
workflows revolving around config files.
|
|
|
|
|
|
|
|
Since the admin endpoint is configurable, the endpoint configuration is loaded
|
|
|
|
from the --address flag if specified; otherwise it is loaded from the given
|
|
|
|
config file; otherwise the default is assumed.`,
|
2019-10-01 11:23:58 +08:00
|
|
|
Flags: func() *flag.FlagSet {
|
2019-10-01 13:43:39 +08:00
|
|
|
fs := flag.NewFlagSet("reload", flag.ExitOnError)
|
2019-10-02 01:02:13 +08:00
|
|
|
fs.String("config", "", "Configuration file (required)")
|
2019-10-01 13:43:39 +08:00
|
|
|
fs.String("adapter", "", "Name of config adapter to apply")
|
2019-10-01 11:23:58 +08:00
|
|
|
fs.String("address", "", "Address of the administration listener, if different from config")
|
|
|
|
return fs
|
|
|
|
}(),
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 11:23:58 +08:00
|
|
|
Name: "version",
|
|
|
|
Func: cmdVersion,
|
2019-10-02 01:02:13 +08:00
|
|
|
Short: "Prints the version",
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 11:23:58 +08:00
|
|
|
Name: "list-modules",
|
|
|
|
Func: cmdListModules,
|
2019-10-02 01:02:13 +08:00
|
|
|
Usage: "[--versions]",
|
|
|
|
Short: "Lists the installed Caddy modules",
|
2019-10-01 13:43:39 +08:00
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("list-modules", flag.ExitOnError)
|
|
|
|
fs.Bool("versions", false, "Print version information")
|
|
|
|
return fs
|
|
|
|
}(),
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 11:23:58 +08:00
|
|
|
Name: "environ",
|
|
|
|
Func: cmdEnviron,
|
2019-10-02 01:02:13 +08:00
|
|
|
Short: "Prints the environment",
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 11:23:58 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 13:43:39 +08:00
|
|
|
Name: "adapt",
|
2019-10-01 11:23:58 +08:00
|
|
|
Func: cmdAdaptConfig,
|
2019-10-07 10:48:09 +08:00
|
|
|
Usage: "--config <path> [--adapter <name>] [--pretty] [--validate]",
|
2019-10-02 01:02:13 +08:00
|
|
|
Short: "Adapts a configuration to Caddy's native JSON",
|
2019-10-01 11:23:58 +08:00
|
|
|
Long: `
|
2019-10-02 01:02:13 +08:00
|
|
|
Adapts a configuration to Caddy's native JSON format and writes the
|
|
|
|
output to stdout, along with any warnings to stderr.
|
|
|
|
|
|
|
|
If --pretty is specified, the output will be formatted with indentation
|
|
|
|
for human readability.
|
|
|
|
|
|
|
|
If --validate is used, the adapted config will be checked for validity.
|
|
|
|
If the config is invalid, an error will be printed to stderr and a non-
|
|
|
|
zero exit status will be returned.`,
|
2019-10-01 13:43:39 +08:00
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("adapt", flag.ExitOnError)
|
2019-10-02 01:02:13 +08:00
|
|
|
fs.String("config", "", "Configuration file to adapt (required)")
|
2019-10-07 10:48:09 +08:00
|
|
|
fs.String("adapter", "caddyfile", "Name of config adapter")
|
2019-10-01 13:43:39 +08:00
|
|
|
fs.Bool("pretty", false, "Format the output for human readability")
|
2019-10-02 01:02:13 +08:00
|
|
|
fs.Bool("validate", false, "Validate the output")
|
2019-10-01 13:43:39 +08:00
|
|
|
return fs
|
|
|
|
}(),
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-10-01 13:43:39 +08:00
|
|
|
|
2019-10-09 10:12:15 +08:00
|
|
|
RegisterCommand(Command{
|
2019-10-01 13:43:39 +08:00
|
|
|
Name: "validate",
|
|
|
|
Func: cmdValidateConfig,
|
|
|
|
Usage: "--config <path> [--adapter <name>]",
|
2019-10-02 01:02:13 +08:00
|
|
|
Short: "Tests whether a configuration file is valid",
|
2019-10-01 13:43:39 +08:00
|
|
|
Long: `
|
2019-10-02 01:02:13 +08:00
|
|
|
Loads and provisions the provided config, but does not start running it.
|
|
|
|
This reveals any errors with the configuration through the loading and
|
|
|
|
provisioning stages.`,
|
2019-10-01 13:43:39 +08:00
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("load", flag.ExitOnError)
|
|
|
|
fs.String("config", "", "Input configuration file")
|
|
|
|
fs.String("adapter", "", "Name of config adapter")
|
|
|
|
return fs
|
|
|
|
}(),
|
2019-10-09 10:12:15 +08:00
|
|
|
})
|
2019-06-29 05:39:41 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
// RegisterCommand registers the command cmd.
|
|
|
|
// cmd.Name must be unique and conform to the
|
|
|
|
// following format:
|
|
|
|
//
|
|
|
|
// - lowercase
|
|
|
|
// - alphanumeric and hyphen characters only
|
|
|
|
// - cannot start or end with a hyphen
|
|
|
|
// - hyphen cannot be adjacent to another hyphen
|
|
|
|
//
|
|
|
|
// This function panics if the name is already registered,
|
|
|
|
// if the name does not meet the described format, or if
|
|
|
|
// any of the fields are missing from cmd.
|
|
|
|
func RegisterCommand(cmd Command) {
|
|
|
|
if cmd.Name == "" {
|
|
|
|
panic("command name is required")
|
2019-07-05 23:59:13 +08:00
|
|
|
}
|
2019-10-01 11:23:58 +08:00
|
|
|
if cmd.Func == nil {
|
|
|
|
panic("command function missing")
|
2019-07-05 23:59:13 +08:00
|
|
|
}
|
2019-10-01 11:23:58 +08:00
|
|
|
if cmd.Short == "" {
|
|
|
|
panic("command short string is required")
|
2019-07-05 23:59:13 +08:00
|
|
|
}
|
2019-10-01 11:23:58 +08:00
|
|
|
if _, exists := commands[cmd.Name]; exists {
|
|
|
|
panic("command already registered: " + cmd.Name)
|
2019-07-05 23:59:13 +08:00
|
|
|
}
|
2019-10-01 11:23:58 +08:00
|
|
|
if !commandNameRegex.MatchString(cmd.Name) {
|
|
|
|
panic("invalid command name")
|
2019-06-29 05:39:41 +08:00
|
|
|
}
|
2019-10-01 11:23:58 +08:00
|
|
|
commands[cmd.Name] = cmd
|
2019-06-29 05:39:41 +08:00
|
|
|
}
|
2019-08-10 02:05:47 +08:00
|
|
|
|
2019-10-01 11:23:58 +08:00
|
|
|
var commandNameRegex = regexp.MustCompile(`^[a-z0-9]$|^([a-z0-9]+-?[a-z0-9]*)+[a-z0-9]$`)
|