All checks were successful
Build (artifact) / build (push) Successful in 27s
131 lines
2.7 KiB
Go
131 lines
2.7 KiB
Go
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 {
|
|
if os.IsNotExist(err) {
|
|
file, err := os.Create(fileName)
|
|
if err != nil {
|
|
fmt.Println("Error creating configuration file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = file.Write(data)
|
|
if err != nil {
|
|
fmt.Println("Error writing to file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
err = file.Chmod(filePermission)
|
|
if err != nil {
|
|
fmt.Println("Error changing file permission: ", err)
|
|
}
|
|
} else {
|
|
fmt.Println("Error opening configuration file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
} else {
|
|
if fileInfo.Size() == 0 {
|
|
file, err := os.Create(fileName)
|
|
if err != nil {
|
|
fmt.Println("Error creating configuration file: ", err)
|
|
os.Exit(1)
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = file.Write(data)
|
|
if err != nil {
|
|
fmt.Println("Error writing to file:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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,
|
|
Short: short,
|
|
Run: commandFunc,
|
|
}
|
|
}
|