aboutsummaryrefslogtreecommitdiff
path: root/pkg/config
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/config')
-rw-r--r--pkg/config/config.go115
-rw-r--r--pkg/config/config_test.go14
2 files changed, 114 insertions, 15 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 {
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index c8cd887..7afbaef 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -8,17 +8,17 @@ import (
"github.com/google/go-cmp/cmp"
)
-func TestConfig(t *testing.T) {
+func TestFileParsing(t *testing.T) {
testCases := []struct {
name string
config string
- expectedConfig *Configuration
+ expectedConfig *configuration
}{
{
name: "minimal scan",
config: `scan "/srv/git"`,
- expectedConfig: &Configuration{
- Scan: &Scan{
+ expectedConfig: &configuration{
+ Scan: &scan{
Public: true,
Path: "/srv/git",
},
@@ -29,8 +29,8 @@ func TestConfig(t *testing.T) {
config: `scan "/srv/git" {
public false
}`,
- expectedConfig: &Configuration{
- Scan: &Scan{
+ expectedConfig: &configuration{
+ Scan: &scan{
Public: false,
Path: "/srv/git",
},
@@ -41,7 +41,7 @@ func TestConfig(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := strings.NewReader(tc.config)
- config, err := Parse(r)
+ config, err := parse(r)
if err != nil {
t.Fatalf("Error parsing config %s", err.Error())
}