refactor: enhance code clarity and maintainability across multiple files
- Updated comments to improve clarity and adhere to best practices in ascii.go, main.go, and diagnostics. - Removed unnecessary linter directives for improved readability in imaging.go and ptz.go. - Reformatted function signatures and added helper calls in tests for consistency and clarity. - Enhanced error handling and logging consistency in various server files, ensuring better maintainability.
This commit is contained in:
@@ -70,7 +70,7 @@ func ImageToASCII(imageData []byte, config ASCIIConfig) (string, error) {
|
||||
|
||||
// imageToASCIIFromImage is the core conversion function.
|
||||
//
|
||||
//nolint:gocyclo // Image to ASCII conversion has high complexity due to multiple pixel processing paths
|
||||
//nolint:gocyclo,lll // Image to ASCII conversion has high complexity due to multiple pixel processing paths
|
||||
func imageToASCIIFromImage(img image.Image, config ASCIIConfig, format string) (string, error) { //nolint:unparam // format reserved for future use
|
||||
// Validate configuration
|
||||
if config.Width <= 0 {
|
||||
|
||||
@@ -280,6 +280,7 @@ func (c *CLI) performDiscoveryOnInterface(interfaceName string) ([]*discovery.De
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("discovery failed: %w", err)
|
||||
}
|
||||
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
@@ -614,11 +615,13 @@ func (c *CLI) inspectRTSPStream(streamURI string) map[string]interface{} {
|
||||
// Use rtspeek library for detailed stream inspection
|
||||
ctx, cancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
defaultRetryDelay*time.Second, //nolint:mnd // Stream inspection timeout
|
||||
defaultRetryDelay*time.Second,
|
||||
)
|
||||
defer cancel()
|
||||
|
||||
streamInfo, err := sd.DescribeStream(ctx, streamURI, defaultRetryDelay*time.Second) //nolint:mnd // Stream description timeout
|
||||
streamInfo, err := sd.DescribeStream(
|
||||
ctx, streamURI, defaultRetryDelay*time.Second,
|
||||
)
|
||||
if err == nil && streamInfo != nil {
|
||||
details["reachable"] = streamInfo.IsReachable()
|
||||
|
||||
@@ -685,7 +688,7 @@ func (c *CLI) tryRTSPConnection(streamURI string) map[string]interface{} {
|
||||
}
|
||||
|
||||
// Try to connect
|
||||
conn, err := net.DialTimeout("tcp", hostPort, maxRetries*time.Second) //nolint:mnd // Connection timeout
|
||||
conn, err := net.DialTimeout("tcp", hostPort, maxRetries*time.Second)
|
||||
if err == nil {
|
||||
//nolint:errcheck // Close error is not critical for connectivity check
|
||||
_ = conn.Close()
|
||||
@@ -1615,7 +1618,7 @@ func (c *CLI) captureAndDisplaySnapshot(ctx context.Context) { //nolint:funlen /
|
||||
filename = "snapshot.jpg"
|
||||
}
|
||||
if err := os.WriteFile(
|
||||
filename, snapshotData, 0600, //nolint:gosec,mnd // 0600 appropriate for CLI output files
|
||||
filename, snapshotData, 0600, //nolint:mnd // 0600 appropriate for CLI output files
|
||||
); err != nil {
|
||||
fmt.Printf("❌ Failed to save file: %v\n", err)
|
||||
} else {
|
||||
|
||||
@@ -152,7 +152,7 @@ var (
|
||||
captureXML = flag.Bool("capture-xml", false, "Capture raw SOAP XML traffic and create tar.gz archive")
|
||||
)
|
||||
|
||||
//nolint:gocognit // Main function has high complexity due to multiple diagnostic operations
|
||||
//nolint:funlen,gocognit,gocyclo // Main function has high complexity due to multiple diagnostic operations
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
@@ -175,7 +175,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Create output directory
|
||||
if err := os.MkdirAll(*outputDir, 0750); err != nil { //nolint:gosec,mnd // 0750 appropriate for diagnostic output
|
||||
if err := os.MkdirAll(*outputDir, 0750); err != nil { //nolint:mnd // 0750 appropriate for diagnostic output
|
||||
log.Fatalf("Failed to create output directory: %v", err)
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ func main() {
|
||||
if *captureXML {
|
||||
timestamp := time.Now().Format("20060102-150405")
|
||||
xmlCaptureDir = filepath.Join(*outputDir, "temp_"+timestamp)
|
||||
if err := os.MkdirAll(xmlCaptureDir, 0750); err != nil { //nolint:gosec,mnd // 0750 appropriate for diagnostic output
|
||||
if err := os.MkdirAll(xmlCaptureDir, 0750); err != nil { //nolint:mnd // 0750 appropriate for diagnostic output
|
||||
log.Fatalf("Failed to create XML capture directory: %v", err)
|
||||
}
|
||||
|
||||
@@ -884,7 +884,7 @@ func saveReport(report *CameraReport, filename string) error {
|
||||
return fmt.Errorf("failed to marshal report: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filename, data, 0600); err != nil { //nolint:gosec,mnd // 0600 appropriate for diagnostic files
|
||||
if err := os.WriteFile(filename, data, 0600); err != nil { //nolint:mnd // 0600 appropriate for diagnostic files
|
||||
return fmt.Errorf("failed to write file: %w", err)
|
||||
}
|
||||
|
||||
@@ -1024,7 +1024,7 @@ func (t *LoggingTransport) saveCapture(capture *XMLCapture) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filename, data, 0600); err != nil { //nolint:gosec,mnd // 0600 appropriate for diagnostic files
|
||||
if err := os.WriteFile(filename, data, 0600); err != nil { //nolint:mnd // 0600 appropriate for diagnostic files
|
||||
log.Printf("Failed to write capture: %v", err)
|
||||
}
|
||||
|
||||
@@ -1032,7 +1032,7 @@ func (t *LoggingTransport) saveCapture(capture *XMLCapture) {
|
||||
reqFile := filepath.Join(t.LogDir, baseFilename+"_request.xml")
|
||||
prettyRequest := prettyPrintXML(capture.RequestBody)
|
||||
if err := os.WriteFile(
|
||||
reqFile, []byte(prettyRequest), 0600, //nolint:gosec,mnd // 0600 appropriate for diagnostic files
|
||||
reqFile, []byte(prettyRequest), 0600, //nolint:mnd // 0600 appropriate for diagnostic files
|
||||
); err != nil {
|
||||
log.Printf("Failed to write request XML: %v", err)
|
||||
}
|
||||
@@ -1040,7 +1040,7 @@ func (t *LoggingTransport) saveCapture(capture *XMLCapture) {
|
||||
respFile := filepath.Join(t.LogDir, baseFilename+"_response.xml")
|
||||
prettyResponse := prettyPrintXML(capture.ResponseBody)
|
||||
if err := os.WriteFile(
|
||||
respFile, []byte(prettyResponse), 0600, //nolint:gosec,mnd // 0600 appropriate for diagnostic files
|
||||
respFile, []byte(prettyResponse), 0600, //nolint:mnd // 0600 appropriate for diagnostic files
|
||||
); err != nil {
|
||||
log.Printf("Failed to write response XML: %v", err)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,9 @@ func main() {
|
||||
model := flag.String("model", "Virtual Multi-Lens Camera", "Device model")
|
||||
firmware := flag.String("firmware", "1.0.0", "Firmware version")
|
||||
serial := flag.String("serial", "SN-12345678", "Serial number")
|
||||
profiles := flag.Int("profiles", maxWorkers, "Number of camera profiles (1-10)") //nolint:mnd // Default profile count
|
||||
profiles := flag.Int(
|
||||
"profiles", maxWorkers, "Number of camera profiles (1-10)", //nolint:mnd // Default profile count is reasonable
|
||||
)
|
||||
ptz := flag.Bool("ptz", true, "Enable PTZ support")
|
||||
imaging := flag.Bool("imaging", true, "Enable Imaging support")
|
||||
events := flag.Bool("events", false, "Enable Events support")
|
||||
|
||||
Reference in New Issue
Block a user