I can’t seem to find a way or an api to download a file in guile, I’m a beginner so I don’t know how to add libraries, I’m using the guix package manager.
I manage to make some code like this.
(use-modules (ice-9 popen)
(ice-9 textual-ports)
(srfi srfi-1)
(srfi srfi-13)
(web client)
(gcrypt hash)
(email base64) ; Import the guile-email base64 module
(ice-9 format)
(ice-9 regex)) ; For string->utf8
(define wget (open-pipe* OPEN_READ "wget" "https://download.qemu.org/qemu-9.2.0.tar.xz" "-O" "qemu-9.2.0.tar.xz" "-nc"))
(let ((output (get-string-all wget))
(status (close-pipe wget)))
(display output)
(if (zero? status)
(display "Download successful.n")
(display "Download failed.n")))
(define (calculate-sha256 filename)
(let* ((port (open-input-pipe (string-append "sha256sum " filename)))
(output (get-string-all port))
(status (close-pipe port)))
(if (zero? status)
(car (string-split output #space))
"Error calculating SHA256")))
(let ((sha256sum (calculate-sha256 "qemu-9.2.0.tar.xz")))
(format #t "SHA256: ~a~%" sha256sum))
This kind of works but of course this is just using wget, but it’s not the native way to use it. How to do it natively? I feel like using bash 1 liners is not the right way to do these things.
2
After hours of struggle, It helps using the repl to inspect what the functions return. What I didn’t understand was that (values 1 2)
couldn’t be assigned to a variable directly.
(use-modules (web client))
(cadr (call-with-values (lambda () (http-get "https://download.qemu.org/qemu-9.2.0.tar.xz" #:decode-body? #f)) list)) ; This returns a bytevector all you have to do now is save it to a file
4