From 02614b3781f6acdfc6df0e7b07d856b2779c4ac7 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 9 Jun 2024 19:35:34 +0200 Subject: feat: Per repository configuration --- pkg/config/config.go | 134 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 111 insertions(+), 23 deletions(-) (limited to 'pkg/config/config.go') diff --git a/pkg/config/config.go b/pkg/config/config.go index 419d49d..3e539f7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -6,6 +6,7 @@ import ( "io" "os" "path" + "path/filepath" "strconv" "git.gabrielgio.me/cerrado/pkg/u" @@ -13,8 +14,9 @@ import ( ) var ( - ScanPathErr = errors.New("Scan path does not exist") - RepoPathErr = errors.New("Repository path does not exist") + ScanPathErr = errors.New("Scan path does not exist") + RepoPathErr = errors.New("Repository path does not exist") + InvalidPropertyErr = errors.New("Invalid property") ) type ( @@ -26,16 +28,19 @@ type ( } // configuration represents file configuration. + // fields needs to be exported to cmp to work configuration struct { - Scan *scan - RootReadme string + Scan *scan + RootReadme string + Repositories []*GitRepositoryConfiguration } // This is a per repository configuration. GitRepositoryConfiguration struct { - Name string - Path string - Public bool + Name string + Path string + Description string + Public bool } // ConfigurationRepository represents the configuration repository (as in @@ -60,13 +65,17 @@ func LoadConfigurationRepository(configPath string) (*ConfigurationRepository, e } repo := &ConfigurationRepository{ - rootReadme: config.RootReadme, + rootReadme: config.RootReadme, + repositories: config.Repositories, } - err = repo.expandOnScanPath(config.Scan.Path, config.Scan.Public) - if err != nil { - return nil, err + if config.Scan.Path != "" { + err = repo.expandOnScanPath(config.Scan.Path, config.Scan.Public) + if err != nil { + return nil, err + } } + return repo, nil } @@ -104,22 +113,32 @@ func (c *ConfigurationRepository) expandOnScanPath(scanPath string, public bool) 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, - }) + if !c.repoExits(fullPath) { + c.repositories = append(c.repositories, &GitRepositoryConfiguration{ + Name: e.Name(), + Path: fullPath, + Public: public, + }) + } } return nil } +func (c *ConfigurationRepository) repoExits(path string) bool { + for _, r := range c.repositories { + if path == r.Path { + return true + } + } + return false +} + func parse(r io.Reader) (*configuration, error) { block, err := scfg.Read(r) if err != nil { @@ -138,16 +157,82 @@ func parse(r io.Reader) (*configuration, error) { return nil, err } + err = setRepositories(block, &config.Repositories) + if err != nil { + return nil, err + } + return config, nil } +func setRepositories(block scfg.Block, repositories *[]*GitRepositoryConfiguration) error { + blocks := block.GetAll("repository") + + for _, r := range blocks { + if len(r.Params) != 1 { + return fmt.Errorf( + "Invlid number of params for repository: %w", + InvalidPropertyErr, + ) + } + + path := u.FirstOrZero(r.Params) + repository := defaultRepisotryConfiguration(path) + + for _, d := range r.Children { + // under repository there is only single param properties + if len(d.Params) != 1 { + return fmt.Errorf( + "Invlid number of params for %s: %w", + d.Name, + InvalidPropertyErr, + ) + } + + switch d.Name { + case "name": + if err := setString(d, &repository.Name); err != nil { + return err + } + case "description": + if err := setString(d, &repository.Description); err != nil { + return err + } + case "public": + if err := setBool(d, &repository.Public); err != nil { + return err + } + } + } + + *repositories = append(*repositories, repository) + } + + return nil +} + func defaultConfiguration() *configuration { return &configuration{ - Scan: &scan{ - Public: true, - Path: "", - }, - RootReadme: "", + Scan: defaultScan(), + RootReadme: "", + Repositories: make([]*GitRepositoryConfiguration, 0), + } +} + +func defaultScan() *scan { + return &scan{ + Public: false, + Path: "", + } + +} + +func defaultRepisotryConfiguration(path string) *GitRepositoryConfiguration { + return &GitRepositoryConfiguration{ + Path: path, + Name: filepath.Base(path), + Description: "", + Public: false, } } @@ -158,6 +243,9 @@ func setRootReadme(block scfg.Block, readme *string) error { func setScan(block scfg.Block, scan *scan) error { scanDir := block.Get("scan") + if scanDir == nil { + return nil + } err := setString(scanDir, &scan.Path) if err != nil { return err @@ -182,7 +270,7 @@ func setBool(dir *scfg.Directive, field *bool) error { func setString(dir *scfg.Directive, field *string) error { if dir != nil { - *field, _ = u.First(dir.Params) + *field = u.FirstOrZero(dir.Params) } return nil } -- cgit v1.2.3