From ab885f07b844fd60adb9d49ed7884f3cd2d939a7 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 9 Aug 2019 12:05:47 -0600 Subject: Implement config adapters and beginning of Caddyfile adapter Along with several other changes, such as renaming caddyhttp.ServerRoute to caddyhttp.Route, exporting some types that were not exported before, and tweaking the caddytls TLS values to be more consistent. Notably, we also now disable automatic cert management for names which already have a cert (manually) loaded into the cache. These names no longer need to be specified in the "skip_certificates" field of the automatic HTTPS config, because they will be skipped automatically. --- cmd/main.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'cmd/main.go') diff --git a/cmd/main.go b/cmd/main.go index 16d065b..e0a3686 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -25,6 +25,7 @@ import ( "os" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig" ) // Main implements the main function of the caddy command. @@ -62,6 +63,7 @@ var commands = map[string]commandFunc{ "version": cmdVersion, "list-modules": cmdListModules, "environ": cmdEnviron, + "adapt-config": cmdAdaptConfig, } func usageString() string { @@ -85,3 +87,66 @@ func handlePingbackConn(conn net.Conn, expect []byte) error { } return nil } + +// loadConfig loads the config from configFile and adapts it +// using adapterName. If adapterName is specified, configFile +// must be also. It prints any warnings to stderr, and returns +// the resulting JSON config bytes. +func loadConfig(configFile, adapterName string) ([]byte, error) { + // specifying an adapter without a config file is ambiguous + if configFile == "" && adapterName != "" { + return nil, fmt.Errorf("cannot adapt config without config file (use --config)") + } + + // load initial config and adapter + var config []byte + var cfgAdapter caddyconfig.Adapter + var err error + if configFile != "" { + config, err = ioutil.ReadFile(configFile) + if err != nil { + return nil, fmt.Errorf("reading config file: %v", err) + } + } else if adapterName == "" { + // as a special case when no config file or adapter + // is specified, see if the Caddyfile adapter is + // plugged in, and if so, try using a default Caddyfile + cfgAdapter = caddyconfig.GetAdapter("caddyfile") + if cfgAdapter != nil { + config, err = ioutil.ReadFile("Caddyfile") + if err != nil && !os.IsNotExist(err) { + return nil, fmt.Errorf("reading default Caddyfile: %v", err) + } + configFile = "Caddyfile" + } + } + + // load config adapter + if adapterName != "" { + cfgAdapter = caddyconfig.GetAdapter(adapterName) + if cfgAdapter == nil { + return nil, fmt.Errorf("unrecognized config adapter: %s", adapterName) + } + } + + // adapt config + if cfgAdapter != nil { + adaptedConfig, warnings, err := cfgAdapter.Adapt(config, map[string]string{ + "filename": configFile, + // TODO: all other options... (http-port, etc...) + }) + if err != nil { + return nil, fmt.Errorf("adapting config using %s: %v", adapterName, err) + } + for _, warn := range warnings { + msg := warn.Message + if warn.Directive != "" { + msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message) + } + fmt.Printf("[WARNING][%s] %s:%d: %s", adapterName, warn.File, warn.Line, msg) + } + config = adaptedConfig + } + + return config, nil +} -- cgit v1.2.3