Split up main package into multiple files

This commit is contained in:
Tim Schubert 2023-11-18 15:02:17 +01:00
parent 00d867b257
commit c9faf272bd
Signed by: dadada
SSH key fingerprint: SHA256:bFAjFH3hR8zRBaJjzQDjc3o4jqoq5EZ87l+KXEjxIz0
5 changed files with 192 additions and 154 deletions

38
download.go Normal file
View file

@ -0,0 +1,38 @@
package main
import (
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
)
func download(
gitdir string,
url string,
) (string, error) {
res, err := http.Get(url + "/export/txt")
if err != nil {
return "", fmt.Errorf("Failed to get pad at %s: %w", url, err)
}
defer res.Body.Close()
padfile := path.Base(url) + ".txt"
padpath := filepath.Join(gitdir, padfile)
out, err := os.Create(padpath)
written, err := io.Copy(out, res.Body)
if err != nil {
return "", fmt.Errorf("Failed to write pad to file at %s: %w", padfile, err)
}
if written < 100 {
return "", fmt.Errorf("Skipping update of %s, because pad has likely been removed from %s", padfile, url)
}
return padfile, nil
}