Files
debos/net.go
copilot-swe-agent[bot] f566e04888 chore: enable more linters and fix all critical issues
Enabled additional linters from fakemachine configuration:
- errorlint: Error wrapping with %w
- misspell: Spelling checks
- revive: Code quality checks
- whitespace: Formatting checks

Fixed all issues including:
- Error handling: Added proper error checks for all function returns
- Error wrapping: Changed %v to %w for proper error wrapping
- Type assertions: Used errors.As instead of direct type assertions
- Unused parameters: Renamed to underscore where appropriate
- Variable naming: Fixed ALL_CAPS constants and underscored names
- Whitespace: Removed unnecessary leading/trailing newlines
- Code flow: Removed unnecessary else blocks

Renamed types (breaking internal API changes):
- DebosState → State
- DebosContext → Context
- DownloadHttpUrl → DownloadHTTPURL

Fixed struct field naming with proper YAML tags:
- Url → URL (with yaml:"url" tag)
- TlsClientCertPath → TLSClientCertPath (kept yaml:"tls-client-cert-path")
- TlsClientKeyPath → TLSClientKeyPath (kept yaml:"tls-client-key-path")
- validateUrl → validateURL method

Co-authored-by: sjoerdsimons <22603932+sjoerdsimons@users.noreply.github.com>
2025-10-01 21:21:22 +02:00

46 lines
947 B
Go

package debos
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
// Function for downloading single file object with http(s) protocol
func DownloadHTTPURL(url, filename string) error {
log.Printf("Download started: '%s' -> '%s'\n", url, filename)
// TODO: Proxy support?
// Check if file object already exists.
fi, err := os.Stat(filename)
if !os.IsNotExist(err) && !fi.Mode().IsRegular() {
return fmt.Errorf("failed to download '%s': '%s' exists and it is not a regular file", url, filename)
}
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("url '%s' returned status code %d (%s)", url, resp.StatusCode, http.StatusText(resp.StatusCode))
}
// Output file
output, err := os.Create(filename)
if err != nil {
return err
}
defer output.Close()
if _, err := io.Copy(output, resp.Body); err != nil {
return err
}
return nil
}