Repository: caddyserver/caddy
File: cmd/main.go (Go)

Code before:
		if cfgAdapter != nil {
			config, err = os.ReadFile("Caddyfile")
			if errors.Is(err, fs.ErrNotExist) {
				// okay, no default Caddyfile; pretend like this never happened
				cfgAdapter = nil
			} else if err != nil {
				// default Caddyfile exists, but error reading it
				return nil, "", fmt.Errorf("reading default Caddyfile: %v", err)
			} else {
				// success reading default Caddyfile
				configFile = "Caddyfile"
				logger.Info("using adjacent Caddyfile")
			}
		}
	}

	// as a special case, if a config file starts with "caddyfile" or
	// has a ".caddyfile" extension, and no adapter is specified, and
	// no adapter module name matches the extension, assume
	// caddyfile adapter for convenience
	baseConfig := strings.ToLower(filepath.Base(configFile))
	baseConfigExt := filepath.Ext(baseConfig)
	startsOrEndsInCaddyfile := strings.HasPrefix(baseConfig, "caddyfile") || strings.HasSuffix(baseConfig, ".caddyfile")

	// If the adapter is not specified,
	// the config file is not starts with "caddyfile",
	// the config file has an extension,
	// and isn't a JSON file (e.g. Caddyfile.yaml),
	// then we don't know what the config format is.
	if adapterName == "" && startsOrEndsInCaddyfile && (baseConfigExt != "" && baseConfigExt != ".caddyfile" && baseConfigExt != ".json") {
		return nil, "", fmt.Errorf("ambiguous config file format; please specify adapter (use --adapter)")
	}

	// If the config file starts or ends with "caddyfile",
	// the extension of the config file is not ".json", AND
	// the user did not specify an adapter, then we assume it's Caddyfile.
	if startsOrEndsInCaddyfile &&
		baseConfigExt != ".json" &&
		adapterName == "" {
		adapterName = "caddyfile"
	}

	// load config adapter
	if adapterName != "" {
		cfgAdapter = caddyconfig.GetAdapter(adapterName)
		if cfgAdapter == nil {
			return nil, "", fmt.Errorf("unrecognized config adapter: %s", adapterName)
		}
	}

	// adapt config

Review comment (suggestion):
```suggestion
	// the config file does not starts with "caddyfile",
```

Code after:
	}
	return false, nil
}

func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) {
	// if no logger is provided, use a nop logger
	// just so we don't have to check for nil
	if logger == nil {
		logger = zap.NewNop()
	}

	// specifying an adapter without a config file is ambiguous
	if adapterName != "" && configFile == "" {
		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 != "" {
		if configFile == "-" {
			config, err = io.ReadAll(os.Stdin)
			if err != nil {
				return nil, "", fmt.Errorf("reading config from stdin: %v", err)
			}
			logger.Info("using config from stdin")
		} else {
			config, err = os.ReadFile(configFile)
			if err != nil {
				return nil, "", fmt.Errorf("reading config from file: %v", err)
			}
			logger.Info("using config from file", zap.String("file", configFile))
		}
	} else if adapterName == "" {
		// if the Caddyfile adapter is plugged in, we can try using an
		// adjacent Caddyfile by default
		cfgAdapter = caddyconfig.GetAdapter("caddyfile")
		if cfgAdapter != nil {
			config, err = os.ReadFile("Caddyfile")
			if errors.Is(err, fs.ErrNotExist) {
				// okay, no default Caddyfile; pretend like this never happened
				cfgAdapter = nil
			} else if err != nil {
				// default Caddyfile exists, but error reading it
				return nil, "", fmt.Errorf("reading default Caddyfile: %v", err)
			} else {
				// success reading default Caddyfile
				configFile = "Caddyfile"
				logger.Info("using adjacent Caddyfile")
			}