0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-30 22:34:15 -05:00

cmd: Fix reload with stdin (#4900)

This commit is contained in:
Francis Lavoie 2022-07-20 20:14:33 -04:00 committed by GitHub
parent 8bdee04651
commit abad9bc256
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 18 deletions

View file

@ -280,7 +280,7 @@ func cmdStop(fl Flags) (int, error) {
configFlag := fl.String("config") configFlag := fl.String("config")
configAdapterFlag := fl.String("adapter") configAdapterFlag := fl.String("adapter")
adminAddr, err := DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag) adminAddr, err := DetermineAdminAPIAddress(addrFlag, nil, configFlag, configAdapterFlag)
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err) return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
} }
@ -310,7 +310,7 @@ func cmdReload(fl Flags) (int, error) {
return caddy.ExitCodeFailedStartup, fmt.Errorf("no config file to load") return caddy.ExitCodeFailedStartup, fmt.Errorf("no config file to load")
} }
adminAddr, err := DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag) adminAddr, err := DetermineAdminAPIAddress(addrFlag, config, configFlag, configAdapterFlag)
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err) return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
} }
@ -732,10 +732,11 @@ func AdminAPIRequest(adminAddr, method, uri string, headers http.Header, body io
// DetermineAdminAPIAddress determines which admin API endpoint address should // DetermineAdminAPIAddress determines which admin API endpoint address should
// be used based on the inputs. By priority: if `address` is specified, then // be used based on the inputs. By priority: if `address` is specified, then
// it is returned; if `configFile` (and `configAdapter`) are specified, then that // it is returned; if `config` is specified, then that config will be used for
// config will be loaded to find the admin address; otherwise, the default // finding the admin address; if `configFile` (and `configAdapter`) are specified,
// admin listen address will be returned. // then that config will be loaded to find the admin address; otherwise, the
func DetermineAdminAPIAddress(address, configFile, configAdapter string) (string, error) { // default admin listen address will be returned.
func DetermineAdminAPIAddress(address string, config []byte, configFile, configAdapter string) (string, error) {
// Prefer the address if specified and non-empty // Prefer the address if specified and non-empty
if address != "" { if address != "" {
return address, nil return address, nil
@ -743,21 +744,29 @@ func DetermineAdminAPIAddress(address, configFile, configAdapter string) (string
// Try to load the config from file if specified, with the given adapter name // Try to load the config from file if specified, with the given adapter name
if configFile != "" { if configFile != "" {
// get the config in caddy's native format var loadedConfigFile string
config, loadedConfigFile, err := LoadConfig(configFile, configAdapter) var err error
if err != nil {
return "", err // use the provided loaded config if non-empty
} // otherwise, load it from the specified file/adapter
if loadedConfigFile == "" { loadedConfig := config
return "", fmt.Errorf("no config file to load") if len(loadedConfig) == 0 {
// get the config in caddy's native format
loadedConfig, loadedConfigFile, err = LoadConfig(configFile, configAdapter)
if err != nil {
return "", err
}
if loadedConfigFile == "" {
return "", fmt.Errorf("no config file to load")
}
} }
// get the address of the admin listener if set // get the address of the admin listener from the config
if len(config) > 0 { if len(loadedConfig) > 0 {
var tmpStruct struct { var tmpStruct struct {
Admin caddy.AdminConfig `json:"admin"` Admin caddy.AdminConfig `json:"admin"`
} }
err = json.Unmarshal(config, &tmpStruct) err := json.Unmarshal(loadedConfig, &tmpStruct)
if err != nil { if err != nil {
return "", fmt.Errorf("unmarshaling admin listener address from config: %v", err) return "", fmt.Errorf("unmarshaling admin listener address from config: %v", err)
} }

View file

@ -113,7 +113,7 @@ func cmdTrust(fl caddycmd.Flags) (int, error) {
} }
// Determine where we're sending the request to get the CA info // Determine where we're sending the request to get the CA info
adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag) adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, nil, configFlag, configAdapterFlag)
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err) return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
} }
@ -182,7 +182,7 @@ func cmdUntrust(fl caddycmd.Flags) (int, error) {
} }
// Determine where we're sending the request to get the CA info // Determine where we're sending the request to get the CA info
adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, configFlag, configAdapterFlag) adminAddr, err := caddycmd.DetermineAdminAPIAddress(addrFlag, nil, configFlag, configAdapterFlag)
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err) return caddy.ExitCodeFailedStartup, fmt.Errorf("couldn't determine admin API address: %v", err)
} }