mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
This commit extends the documentation effort by adding detailed explanatory comments to key CLI components and core functionality throughout the cmd/ directory. The changes focus on explaining WHY certain logic is needed, not just WHAT the code does, improving code maintainability and helping developers understand complex business decisions. Key improvements: - Main entry points: Document CLI initialization, logging setup, and cache configuration with reasoning for design decisions - DNS proxy core: Explain DNS proxy constants, data structures, and core processing pipeline for handling DNS queries - Service management: Document service command structure, configuration patterns, and platform-specific service handling - Logging infrastructure: Explain log buffer management, level encoders, and log formatting decisions for different use cases - Metrics and monitoring: Document Prometheus metrics structure, HTTP endpoints, and conditional metric collection for performance - Network handling: Explain Linux-specific network interface filtering, virtual interface detection, and DNS configuration management - Hostname validation: Document RFC1123 compliance and DNS naming standards for system compatibility - Mobile integration: Explain HTTP retry logic, fallback mechanisms, and mobile platform integration patterns - Connection management: Document connection wrapper design to prevent log pollution during process lifecycle Technical details: - Added explanatory comments to 11 additional files in cmd/cli/ - Maintained consistent documentation style and format - Preserved all existing functionality while improving code clarity - Enhanced understanding of complex business logic and platform-specific behavior These comments help future developers understand the reasoning behind complex decisions, making the codebase more maintainable and reducing the risk of incorrect modifications during maintenance.
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/kardianos/service"
|
|
)
|
|
|
|
func init() {
|
|
if r, err := newLoopbackOSConfigurator(); err == nil {
|
|
useSystemdResolved = r.Mode() == "systemd-resolved"
|
|
}
|
|
// Disable quic-go's ECN support by default, see https://github.com/quic-go/quic-go/issues/3911
|
|
if os.Getenv("QUIC_GO_DISABLE_ECN") == "" {
|
|
os.Setenv("QUIC_GO_DISABLE_ECN", "true")
|
|
}
|
|
}
|
|
|
|
// setDependencies sets service dependencies for Linux
|
|
func setDependencies(svc *service.Config) {
|
|
svc.Dependencies = []string{
|
|
"Wants=network-online.target",
|
|
"After=network-online.target",
|
|
"Wants=NetworkManager-wait-online.service",
|
|
"After=NetworkManager-wait-online.service",
|
|
"Wants=nss-lookup.target",
|
|
"After=nss-lookup.target",
|
|
}
|
|
if out, _ := exec.Command("networkctl", "--no-pager").CombinedOutput(); len(out) > 0 {
|
|
if wantsSystemDNetworkdWaitOnline(bytes.NewReader(out)) {
|
|
svc.Dependencies = append(svc.Dependencies, "Wants=systemd-networkd-wait-online.service")
|
|
}
|
|
}
|
|
}
|
|
|
|
// setWorkingDirectory sets the working directory for the service
|
|
func setWorkingDirectory(svc *service.Config, dir string) {
|
|
svc.WorkingDirectory = dir
|
|
}
|
|
|
|
// wantsSystemDNetworkdWaitOnline reports whether "systemd-networkd-wait-online" service
|
|
// is required to be added to ctrld dependencies services.
|
|
// The input reader r is the output of "networkctl --no-pager" command.
|
|
func wantsSystemDNetworkdWaitOnline(r io.Reader) bool {
|
|
scanner := bufio.NewScanner(r)
|
|
// Skip header
|
|
scanner.Scan()
|
|
configured := false
|
|
for scanner.Scan() {
|
|
fields := strings.Fields(scanner.Text())
|
|
if len(fields) > 0 && fields[len(fields)-1] == "configured" {
|
|
configured = true
|
|
break
|
|
}
|
|
}
|
|
return configured
|
|
}
|