From c0f49a7bb40b2f612f12055ab0b2b969f9393569 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Wed, 29 Jun 2022 16:18:28 +0200 Subject: [PATCH] Try 07 --- data/config/test-services.json | 6 ++++ pkg/templater/repo-apk.go | 58 ++++++++++++++++++++++++++++------ pkg/templater/repo-deb.go | 5 +-- pkg/templater/repo-helm.go | 12 ++++--- pkg/templater/repo.go | 13 ++++++-- pkg/templater/services.go | 36 ++++++++++++--------- 6 files changed, 94 insertions(+), 36 deletions(-) diff --git a/data/config/test-services.json b/data/config/test-services.json index a6e4ec3..07afabc 100644 --- a/data/config/test-services.json +++ b/data/config/test-services.json @@ -28,6 +28,12 @@ "type": "helm", "name": "grafana", "url": "https://grafana.github.io/helm-charts", + "enabled":true + }, + "AlpineTesting": { + "type": "apk", + "name": "testing", + "url": "http://mirror.arvancloud.com/alpine/edge/testing", "enabled": true } }, diff --git a/pkg/templater/repo-apk.go b/pkg/templater/repo-apk.go index c43cd4c..2689bb5 100644 --- a/pkg/templater/repo-apk.go +++ b/pkg/templater/repo-apk.go @@ -1,6 +1,7 @@ package templater import ( + "bufio" "fmt" "io/ioutil" "os" @@ -9,24 +10,60 @@ import ( "forge.cadoles.com/pcaseiro/templatefile/pkg/utils" ) +var APKConfigFile = "/etc/apk/repositories" + type APKRepository struct { - Name string `json:"name"` - Type string `json:"type"` - URL string `json:"url"` - Enabled bool `json:"enabled"` + Repository +} + +func (hr *APKRepository) urlIsPresent() (bool, error) { + f, err := os.Open(APKConfigFile) + if err != nil { + return false, err + } + defer f.Close() + + // Splits on newlines by default. + scanner := bufio.NewScanner(f) + + line := 1 + for scanner.Scan() { + if strings.Contains(scanner.Text(), hr.URL) { + return true, nil + } + + line++ + } + + if err := scanner.Err(); err != nil { + return false, err + } + return false, nil + } 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) + + URLIsPresent, err := hr.urlIsPresent() if err != nil { return err } - defer file.Close() - if _, err := file.WriteString(data); err != nil { - return err + + if URLIsPresent { + return nil + } else { + 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 } - return nil + } func (hr *APKRepository) Update() error { @@ -48,6 +85,7 @@ func (hr *APKRepository) Delete() error { } return nil } + func (hr *APKRepository) Manage() error { if hr.Enabled { return hr.Add() diff --git a/pkg/templater/repo-deb.go b/pkg/templater/repo-deb.go index 4f1adba..1c6f6f9 100644 --- a/pkg/templater/repo-deb.go +++ b/pkg/templater/repo-deb.go @@ -8,10 +8,7 @@ import ( ) type DebRepository struct { - Name string `json:"name"` - Type string `json:"type"` - URL string `json:"url"` - Enabled bool `json:"enabled"` + Repository } func (hr *DebRepository) Add() error { diff --git a/pkg/templater/repo-helm.go b/pkg/templater/repo-helm.go index 3d7154e..71f1803 100644 --- a/pkg/templater/repo-helm.go +++ b/pkg/templater/repo-helm.go @@ -1,10 +1,7 @@ package templater type HelmRepository struct { - Name string `json:"name"` - Type string `json:"type"` - URL string `json:"url"` - Enabled bool `json:"enabled"` + Repository } func (hr *HelmRepository) Add() error { @@ -18,6 +15,11 @@ func (hr *HelmRepository) Update() error { func (hr *HelmRepository) Delete() error { return nil } + func (hr *HelmRepository) Manage() error { - return nil + if hr.Enabled { + return hr.Add() + } else { + return hr.Delete() + } } diff --git a/pkg/templater/repo.go b/pkg/templater/repo.go index cbe8f39..8c143d0 100644 --- a/pkg/templater/repo.go +++ b/pkg/templater/repo.go @@ -1,8 +1,17 @@ package templater -type PackageRepository interface { // création de L'interface Forme - Manage() error // signature de la méthode Perimetre() +type PackageRepository interface { + Manage() error Update() error Add() error Delete() error } + +type Repository struct { + Actions PackageRepository + + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` + Enabled bool `json:"enabled"` +} diff --git a/pkg/templater/services.go b/pkg/templater/services.go index ace465d..7d58597 100644 --- a/pkg/templater/services.go +++ b/pkg/templater/services.go @@ -4,8 +4,6 @@ import ( "encoding/json" "fmt" "path/filepath" - - "github.com/mitchellh/mapstructure" ) type Service struct { @@ -13,27 +11,35 @@ type Service struct { Vars map[string]interface{} `json:"Vars"` Daemons map[string]SystemService `json:"Daemons"` Users map[string]SystemUser `json:"Users"` - Repos map[string]interface{} `json:"Repositories"` + Repos map[string]Repository `json:"Repositories"` Packages map[string]SystemPackage `json:"Packages"` } -func (s *Service) manageRepo(repo interface{}) error { - fmt.Printf("%v\n", repo) - if repo["type"] == "helm" { - var rp HelmRepository - mapstructure.Decode(repo, rp) - } +func (s *Service) manageRepos(repos map[string]Repository) error { + for _, repo := range s.Repos { + if repo.Type == "helm" { + rp := HelmRepository{repo} + return rp.Manage() + } + if repo.Type == "apk" { + rp := APKRepository{repo} + return rp.Manage() + } + if repo.Type == "deb" { + rp := DebRepository{} + return rp.Manage() + } + } return nil } func (s *Service) Manage(templateDir string) error { - for _, repo := range s.Repos { - if err := s.manageRepo(repo); err != nil { - return err - } - + // Manage packages repositories + err := s.manageRepos(s.Repos) + if err != nil { + return err } // Manage system packages @@ -44,7 +50,7 @@ func (s *Service) Manage(templateDir string) error { } } - err := processConfigFiles(s.ConfigFiles, s.Vars, templateDir) + err = processConfigFiles(s.ConfigFiles, s.Vars, templateDir) if err != nil { return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err) }