diff options
Diffstat (limited to 'pkg/config/config.go')
-rw-r--r-- | pkg/config/config.go | 115 |
1 files changed, 107 insertions, 8 deletions
diff --git a/pkg/config/config.go b/pkg/config/config.go index 9b6acce..419d49d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,27 +1,126 @@ package config import ( + "errors" "fmt" "io" + "os" + "path" "strconv" "git.gabrielgio.me/cerrado/pkg/u" "git.sr.ht/~emersion/go-scfg" ) +var ( + ScanPathErr = errors.New("Scan path does not exist") + RepoPathErr = errors.New("Repository path does not exist") +) + type ( - Scan struct { + + // scan represents piece of the scan from the configuration file. + scan struct { Path string Public bool } - Configuration struct { - Scan *Scan + // configuration represents file configuration. + configuration struct { + Scan *scan RootReadme string } + + // This is a per repository configuration. + GitRepositoryConfiguration struct { + Name string + Path string + Public bool + } + + // ConfigurationRepository represents the configuration repository (as in + // database repositories). + // This holds all the function necessary to ask for configuration + // information. + ConfigurationRepository struct { + rootReadme string + repositories []*GitRepositoryConfiguration + } ) -func Parse(r io.Reader) (*Configuration, error) { +func LoadConfigurationRepository(configPath string) (*ConfigurationRepository, error) { + f, err := os.Open(configPath) + if err != nil { + return nil, err + } + + config, err := parse(f) + if err != nil { + return nil, err + } + + repo := &ConfigurationRepository{ + rootReadme: config.RootReadme, + } + + err = repo.expandOnScanPath(config.Scan.Path, config.Scan.Public) + if err != nil { + return nil, err + } + return repo, nil + +} + +// GetRootReadme returns root read path +func (c *ConfigurationRepository) GetRootReadme() string { + return c.rootReadme +} + +// GetByName returns configuration of repository for a given name. +// It returns nil if there is not match for it. +func (c *ConfigurationRepository) GetByName(name string) *GitRepositoryConfiguration { + for _, r := range c.repositories { + if r.Name == name { + return r + } + } + return nil +} + +// List returns all the configuration for all repositories. +func (c *ConfigurationRepository) List() []*GitRepositoryConfiguration { + return c.repositories +} + +// expandOnScanPath scans the scanPath for folders taking them as repositories +// and applying them default configuration. +func (c *ConfigurationRepository) expandOnScanPath(scanPath string, public bool) error { + if !u.FileExist(scanPath) { + return ScanPathErr + } + + entries, err := os.ReadDir(scanPath) + if err != nil { + return err + } + + c.repositories = make([]*GitRepositoryConfiguration, 0) + for _, e := range entries { + if !e.IsDir() { + continue + } + + fullPath := path.Join(scanPath, e.Name()) + c.repositories = append(c.repositories, &GitRepositoryConfiguration{ + Name: e.Name(), + Path: fullPath, + Public: public, + }) + } + return nil +} + +func parse(r io.Reader) (*configuration, error) { block, err := scfg.Read(r) if err != nil { return nil, err @@ -42,9 +141,9 @@ func Parse(r io.Reader) (*Configuration, error) { return config, nil } -func defaultConfiguration() *Configuration { - return &Configuration{ - Scan: &Scan{ +func defaultConfiguration() *configuration { + return &configuration{ + Scan: &scan{ Public: true, Path: "", }, @@ -57,7 +156,7 @@ func setRootReadme(block scfg.Block, readme *string) error { return setString(scanDir, readme) } -func setScan(block scfg.Block, scan *Scan) error { +func setScan(block scfg.Block, scan *scan) error { scanDir := block.Get("scan") err := setString(scanDir, &scan.Path) if err != nil { |