func (f Flags) Float64(name string) float64 {
	val, _ := strconv.ParseFloat(f.String(name), 64)
	return val
}
Float64 returns the float64 representation of the
flag given by name. It returns false if the flag
is not a float64 type. It panics if the flag is
not in the flag set.
package caddycmd
import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"path/filepath"
	"runtime"
	"runtime/debug"
	"strconv"
	"strings"
	"time"
	"github.com/caddyserver/caddy/v2"
	"github.com/caddyserver/caddy/v2/caddyconfig"
	"github.com/caddyserver/certmagic"
	"go.uber.org/zap"
)
func init() {
	goModule := caddy.GoModule()
	cleanModVersion := strings.TrimPrefix(goModule.Version, "v")
	certmagic.UserAgent = "Caddy/" + cleanModVersion
	certmagic.DefaultACME.Agreed = true
}
func Main() {
	switch len(os.Args) {
	case 0:
		fmt.Printf("[FATAL] no arguments provided by OS; args[0] must be command\n")
		os.Exit(caddy.ExitCodeFailedStartup)
	case 1:
		os.Args = append(os.Args, "help")
	}
	subcommandName := os.Args[1]
	subcommand, ok := commands[subcommandName]
	if !ok {
		if strings.HasPrefix(os.Args[1], "-") {
			fmt.Println("[ERROR] first argument must be a subcommand; see 'caddy help'")
		} else {
			fmt.Printf("[ERROR] '%s' is not a recognized subcommand; see 'caddy help'\n", os.Args[1])
		}
		os.Exit(caddy.ExitCodeFailedStartup)
	}
	fs := subcommand.Flags
	if fs == nil {
		fs = flag.NewFlagSet(subcommand.Name, flag.ExitOnError)
	}
	err := fs.Parse(os.Args[2:])
	if err != nil {
		fmt.Println(err)
		os.Exit(caddy.ExitCodeFailedStartup)
	}
	exitCode, err := subcommand.Func(Flags{fs})
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", subcommand.Name, err)
	}
	os.Exit(exitCode)
}
func handlePingbackConn(conn net.Conn, expect []byte) error {
	defer conn.Close()
	confirmationBytes, err := io.ReadAll(io.LimitReader(conn, 32))
	if err != nil {
		return err
	}
	if !bytes.Equal(confirmationBytes, expect) {
		return fmt.Errorf("wrong confirmation: %x", confirmationBytes)
	}
	return nil
}
func loadConfig(configFile, adapterName string) ([]byte, string, error) {
	if adapterName != "" && configFile == "" {
		return nil, "", fmt.Errorf("cannot adapt config without config file (use --config)")
	}
	var config []byte
	var cfgAdapter caddyconfig.Adapter
	var err error
	if configFile != "" {
		if configFile == "-" {
			config, err = io.ReadAll(os.Stdin)
		} else {
			config, err = os.ReadFile(configFile)
		}
		if err != nil {
			return nil, "", fmt.Errorf("reading config file: %v", err)
		}
		caddy.Log().Info("using provided configuration",
			zap.String("config_file", configFile),
			zap.String("config_adapter", adapterName))
	} else if adapterName == "" {
		cfgAdapter = caddyconfig.GetAdapter("caddyfile")
		if cfgAdapter != nil {
			config, err = os.ReadFile("Caddyfile")
			if os.IsNotExist(err) {
				cfgAdapter = nil
			} else if err != nil {
				return nil, "", fmt.Errorf("reading default Caddyfile: %v", err)
			} else {
				configFile = "Caddyfile"
				caddy.Log().Info("using adjacent Caddyfile")
			}
		}
	}
	if strings.HasPrefix(filepath.Base(configFile), "Caddyfile") &&
		filepath.Ext(configFile) != ".json" &&
		adapterName == "" {
		adapterName = "caddyfile"
	}
	if adapterName != "" {
		cfgAdapter = caddyconfig.GetAdapter(adapterName)
		if cfgAdapter == nil {
			return nil, "", fmt.Errorf("unrecognized config adapter: %s", adapterName)
		}
	}
	if cfgAdapter != nil {
		adaptedConfig, warnings, err := cfgAdapter.Adapt(config, map[string]interface{}{
			"filename": configFile,
		})
		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)
			}
			caddy.Log().Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line))
		}
		config = adaptedConfig
	}
	return config, configFile, nil
}
func watchConfigFile(filename, adapterName string) {
	defer func() {
		if err := recover(); err != nil {
			log.Printf("[PANIC] watching config file: %v\n%s", err, debug.Stack())
		}
	}()
	logger := func() *zap.Logger {
		return caddy.Log().
			Named("watcher").
			With(zap.String("config_file", filename))
	}
	info, err := os.Stat(filename)
	if err != nil {
		logger().Error("cannot watch config file", zap.Error(err))
		return
	}
	lastModified := info.ModTime()
	logger().Info("watching config file for changes")
	var lastErr time.Time
	finalError := func(err error) bool {
		if lastErr.IsZero() {
			lastErr = time.Now()
			return false
		}
		if time.Since(lastErr) > 30*time.Second {
			logger().Error("giving up watching config file; too many errors",
				zap.Error(err))
			return true
		}
		return false
	}
	for range time.Tick(1 * time.Second) {
		info, err := os.Stat(filename)
		if err != nil {
			if finalError(err) {
				return
			}
			continue
		}
		lastErr = time.Time{}  
		if !info.ModTime().After(lastModified) {
			continue
		}
		logger().Info("config file changed; reloading")
		lastModified = info.ModTime()
		config, _, err := loadConfig(filename, adapterName)
		if err != nil {
			logger().Error("unable to load latest config", zap.Error(err))
			continue
		}
		err = caddy.Load(config, false)
		if err != nil {
			logger().Error("applying latest config", zap.Error(err))
			continue
		}
	}
}
type Flags struct {
	*flag.FlagSet
}
func (f Flags) String(name string) string {
	return f.FlagSet.Lookup(name).Value.String()
}
func (f Flags) Bool(name string) bool {
	val, _ := strconv.ParseBool(f.String(name))
	return val
}
func (f Flags) Int(name string) int {
	val, _ := strconv.ParseInt(f.String(name), 0, strconv.IntSize)
	return int(val)
}