MCPcopy
hub / github.com/caddyserver/caddy / loadConfigWithLogger

Function loadConfigWithLogger

cmd/main.go:146–242  ·  view source on GitHub ↗
(logger *zap.Logger, configFile, adapterName string)

Source from the content-addressed store, hash-verified

144}
145
146func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, string, error) {
147 // if no logger is provided, use a nop logger
148 // just so we don't have to check for nil
149 if logger == nil {
150 logger = zap.NewNop()
151 }
152
153 // specifying an adapter without a config file is ambiguous
154 if adapterName != "" && configFile == "" {
155 return nil, "", "", fmt.Errorf("cannot adapt config without config file (use --config)")
156 }
157
158 // load initial config and adapter
159 var config []byte
160 var cfgAdapter caddyconfig.Adapter
161 var err error
162 if configFile != "" {
163 if configFile == "-" {
164 config, err = io.ReadAll(os.Stdin)
165 if err != nil {
166 return nil, "", "", fmt.Errorf("reading config from stdin: %v", err)
167 }
168 logger.Info("using config from stdin")
169 } else {
170 config, err = os.ReadFile(configFile)
171 if err != nil {
172 return nil, "", "", fmt.Errorf("reading config from file: %v", err)
173 }
174 logger.Info("using config from file", zap.String("file", configFile))
175 }
176 } else if adapterName == "" {
177 // if the Caddyfile adapter is plugged in, we can try using an
178 // adjacent Caddyfile by default
179 cfgAdapter = caddyconfig.GetAdapter("caddyfile")
180 if cfgAdapter != nil {
181 config, err = os.ReadFile("Caddyfile")
182 if errors.Is(err, fs.ErrNotExist) {
183 // okay, no default Caddyfile; pretend like this never happened
184 cfgAdapter = nil
185 } else if err != nil {
186 // default Caddyfile exists, but error reading it
187 return nil, "", "", fmt.Errorf("reading default Caddyfile: %v", err)
188 } else {
189 // success reading default Caddyfile
190 configFile = "Caddyfile"
191 logger.Info("using adjacent Caddyfile")
192 }
193 }
194 }
195
196 if yes, err := isCaddyfile(configFile, adapterName); yes {
197 adapterName = "caddyfile"
198 } else if err != nil {
199 return nil, "", "", err
200 }
201
202 // load config adapter
203 if adapterName != "" {

Callers 2

LoadConfigFunction · 0.85
watchConfigFileFunction · 0.85

Calls 6

AdaptMethod · 0.95
GetAdapterFunction · 0.92
isCaddyfileFunction · 0.85
ReadFileMethod · 0.80
StringMethod · 0.45
IntMethod · 0.45

Tested by

no test coverage detected