templatefile/pkg/templater/repo-apk.go

58 lines
1.1 KiB
Go
Raw Normal View History

2022-06-27 15:58:37 +02:00
package templater
import (
"fmt"
"io/ioutil"
"os"
"strings"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
type APKRepository struct {
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
}
func (hr *APKRepository) Add() error {
data := fmt.Sprintf("%s", hr.URL)
file, err := os.OpenFile("/etc/apk/repositories", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
if _, err := file.WriteString(data); err != nil {
return err
}
return nil
}
func (hr *APKRepository) Update() error {
if _, stdErr, err := utils.RunSystemCommand("apk", "update"); err != nil {
return fmt.Errorf("%s [%s]", stdErr, err)
}
return nil
}
func (hr *APKRepository) Delete() error {
fileBytes, err := ioutil.ReadFile("/etc/apk/repositories")
if err != nil {
return err
}
lines := strings.Split(string(fileBytes), "\n")
2022-06-29 12:13:32 +02:00
for _, line := range lines {
fmt.Println(line)
2022-06-27 15:58:37 +02:00
}
return nil
}
func (hr *APKRepository) Manage() error {
if hr.Enabled {
return hr.Add()
} else {
return hr.Delete()
}
}