// SPDX-FileCopyrightText: 2024 Dan Anglin // // SPDX-License-Identifier: GPL-3.0-or-later package utilities import ( "errors" "fmt" "io" "net/http" "os" "time" ) func ReadFile(path string) (string, error) { data, err := os.ReadFile(path) if err != nil { return "", fmt.Errorf("unable to read the data from the file: %w", err) } return string(data), nil } func FileExists(path string) (bool, error) { if _, err := os.Stat(path); err != nil { if errors.Is(err, os.ErrNotExist) { return false, nil } return false, fmt.Errorf("unable to check if the file exists: %w", err) } return true, nil } // TODO: move to client package func DownloadFile(url, path string) error { client := http.Client{ CheckRedirect: func(r *http.Request, _ []*http.Request) error { r.URL.Opaque = r.URL.Path return nil }, Timeout: time.Second * 30, } resp, err := client.Get(url) if err != nil { return fmt.Errorf("unable to download the file: %w", err) } defer resp.Body.Close() file, err := os.Create(path) if err != nil { return fmt.Errorf("unable to create %s: %w", path, err) } defer file.Close() if _, err = io.Copy(file, resp.Body); err != nil { return fmt.Errorf("unable to save the download to %s: %w", path, err) } return nil }