diff options
Diffstat (limited to 'internal/admin/model.go')
| -rw-r--r-- | internal/admin/model.go | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/internal/admin/model.go b/internal/admin/model.go index 5a7f984..2841e0a 100644 --- a/internal/admin/model.go +++ b/internal/admin/model.go @@ -15,9 +15,7 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/storage/filesystem" "github.com/go-git/go-git/v5/storage/memory" - "gopkg.in/ini.v1" - "sigs.k8s.io/yaml" ) @@ -30,7 +28,7 @@ const ( Admin = 2 // GitExportMagic magic file name for daemon export GitExportMagic = "git-daemon-export-ok" - // GitWebExportMagic + // GitWebExportMagic magic filename for web repos GitWebExportMagic = "git-web-export-ok" ) @@ -71,11 +69,11 @@ type ServerRepos struct { BasePath string `json:"basepath"` } -func loadFromGit(gitUrl, filePath string) ([]byte, error) { +func loadFromGit(gitURL, filePath string) ([]byte, error) { fs := memfs.New() storer := memory.NewStorage() _, err := git.Clone(storer, fs, &git.CloneOptions{ - URL: gitUrl, + URL: gitURL, }) if err != nil { // log.error @@ -97,6 +95,7 @@ func loadLocalFile(path string) ([]byte, error) { log.Printf("config file not opened %s", path) return []byte{}, err } + defer file.Close() configBytes, err := io.ReadAll(file) if err != nil { log.Print("config file not read") @@ -105,9 +104,12 @@ func loadLocalFile(path string) ([]byte, error) { return configBytes, nil } +// loadServerConfig configPath should be the absolutepath to the configmap if it's not in a repo func loadServerConfig(mgmtRepo bool, baseDir, configPath string) (*ServerRepos, error) { - configBytes := []byte{} - var err error + var ( + configBytes []byte + err error + ) if mgmtRepo { repoURI := filepath.Join("file:///", baseDir, "mgmt.git") configBytes, err = loadFromGit(repoURI, configPath) @@ -117,17 +119,17 @@ func loadServerConfig(mgmtRepo bool, baseDir, configPath string) (*ServerRepos, return &ServerRepos{}, err } } else { - configBytes, err = loadLocalFile(filepath.Join(baseDir, configPath)) + configBytes, err = loadLocalFile(configPath) if err != nil { // log.error - log.Print("Failed to load config file from git") + log.Print("Failed to load config file from file system") return &ServerRepos{}, err } } config := &ServerRepos{} err = yaml.Unmarshal(configBytes, &config) if err != nil { - return &ServerRepos{}, errors.New("Could not parse gitserver config") + return &ServerRepos{}, errors.New("could not parse gitserver config") } return config, nil } @@ -189,8 +191,8 @@ func (r *GitRepo) ReconcileRepo(basePath string) { _, err := os.Stat(repoBase) if errors.Is(err, fs.ErrNotExist) { // if no exist -> init bare - fs := osfs.New(repoBase) - strg := filesystem.NewStorage(fs, nil) + repoFs := osfs.New(repoBase) + strg := filesystem.NewStorage(repoFs, nil) _, _ = git.Init(strg, nil) } // set export file for git-http-backend @@ -231,7 +233,9 @@ func (r *GitWeb) ReconcileGitConf(repoBase string) { if (GitWeb{} == *r) { if cfg.HasSection("gitweb") { cfg.DeleteSection("gitweb") - cfg.SaveTo(confPath) + if err := cfg.SaveTo(confPath); err != nil { + log.Fatalf("Coudln't save gitconfig %s", err) + } } return } @@ -240,5 +244,7 @@ func (r *GitWeb) ReconcileGitConf(repoBase string) { section.Key("owner").SetValue(r.Owner) section.Key("url").SetValue(r.URL) section.Key("category").SetValue(r.Category) - cfg.SaveTo(confPath) + if err := cfg.SaveTo(confPath); err != nil { + log.Fatalf("Coudln't save gitconfig %s", err) + } } |