Refactored config system again
All checks were successful
Build (artifact) / build (push) Successful in 27s
All checks were successful
Build (artifact) / build (push) Successful in 27s
This commit is contained in:
72
app/util.go
72
app/util.go
@@ -2,11 +2,44 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var SourceCertmanRepo = "http://127.0.0.1"
|
||||
|
||||
type GiteaRelease struct {
|
||||
Id int `json:"id"`
|
||||
Assets []Asset `json:"assets"`
|
||||
}
|
||||
type Asset struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func GetUserIds(username string) (int, int, error) {
|
||||
userEntry, err := user.Lookup(username)
|
||||
if err != nil {
|
||||
return -1, -1, fmt.Errorf("error getting user %s: %v", username, err)
|
||||
}
|
||||
uid, err := strconv.Atoi(strings.TrimSpace(userEntry.Uid))
|
||||
if err != nil {
|
||||
return -1, -1, err
|
||||
}
|
||||
gid, err := strconv.Atoi(strings.TrimSpace(userEntry.Gid))
|
||||
if err != nil {
|
||||
return -1, -1, err
|
||||
}
|
||||
|
||||
return uid, gid, nil
|
||||
}
|
||||
|
||||
func createFile(fileName string, filePermission os.FileMode, data []byte) {
|
||||
fileInfo, err := os.Stat(fileName)
|
||||
if err != nil {
|
||||
@@ -49,6 +82,45 @@ func createFile(fileName string, filePermission os.FileMode, data []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
// downloadFile streams data from a URL and saves it directly to a local file path
|
||||
func downloadFile(filepath string, url string) error {
|
||||
// 1. Send the HTTP GET request
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Always close the response body to prevent resource leaks
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 2. Check for a valid HTTP 200 OK status code
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("bad status: %s", resp.Status)
|
||||
}
|
||||
|
||||
// 3. Create the local destination file
|
||||
out, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
uid, gid, err := GetUserIds("certman")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = out.Chown(uid, gid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = out.Chmod(775)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 4. Stream the server response body directly into the local file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
func basicCmd(use, short string, commandFunc func(cmd *cobra.Command, args []string)) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: use,
|
||||
|
||||
Reference in New Issue
Block a user