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") for _, line := range lines { fmt.Println(line) } return nil } func (hr *APKRepository) Manage() error { if hr.Enabled { return hr.Add() } else { return hr.Delete() } }