diff options
Diffstat (limited to 'pkg/config/config.go')
-rw-r--r-- | pkg/config/config.go | 219 |
1 files changed, 186 insertions, 33 deletions
diff --git a/pkg/config/config.go b/pkg/config/config.go index fd19808..c00586b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -4,10 +4,12 @@ import ( "errors" "fmt" "io" + "log/slog" "os" "path" "path/filepath" "strconv" + "strings" "git.gabrielgio.me/cerrado/pkg/u" "git.sr.ht/~emersion/go-scfg" @@ -19,6 +21,16 @@ var ( ErrInvalidProperty = errors.New("Invalid property") ) +type OrderBy int + +const ( + Unordered OrderBy = iota + AlphabeticalAsc + AlphabeticalDesc + LastCommitAsc + LastCommitDesc +) + type ( // scan represents piece of the scan from the configuration file. @@ -30,10 +42,16 @@ type ( // configuration represents file configuration. // fields needs to be exported to cmp to work configuration struct { - Scan *scan - RootReadme string - ListenAddr string - Repositories []*GitRepositoryConfiguration + AESKey string + ListenAddr string + OrderBy string + Passphrase string + RemoveSuffix bool + Repositories []*GitRepositoryConfiguration + RootReadme string + Scans []*scan + SyntaxHighlight string + Hostname string } // This is a per repository configuration. @@ -50,9 +68,15 @@ type ( // This holds all the function necessary to ask for configuration // information. ConfigurationRepository struct { - rootReadme string - listenAddr string - repositories []*GitRepositoryConfiguration + aesKey []byte + listenAddr string + orderBy OrderBy + passphrase []byte + removeSuffix bool + repositories []*GitRepositoryConfiguration + rootReadme string + syntaxHighlight string + hostname string } ) @@ -68,20 +92,27 @@ func LoadConfigurationRepository(configPath string) (*ConfigurationRepository, e } repo := &ConfigurationRepository{ - rootReadme: config.RootReadme, - listenAddr: config.ListenAddr, - repositories: config.Repositories, + aesKey: []byte(config.AESKey), + listenAddr: config.ListenAddr, + passphrase: []byte(config.Passphrase), + repositories: config.Repositories, + rootReadme: config.RootReadme, + hostname: config.Hostname, + syntaxHighlight: config.SyntaxHighlight, + removeSuffix: config.RemoveSuffix, + orderBy: parseOrderBy(config.OrderBy), } - if config.Scan.Path != "" { - err = repo.expandOnScanPath(config.Scan.Path, config.Scan.Public) - if err != nil { - return nil, err + for _, scan := range config.Scans { + if scan.Path != "" { + err = repo.expandOnScanPath(scan.Path, scan.Public) + if err != nil { + return nil, err + } } } return repo, nil - } // GetRootReadme returns root read path @@ -89,10 +120,34 @@ func (c *ConfigurationRepository) GetRootReadme() string { return c.rootReadme } +func (c *ConfigurationRepository) GetHostname() string { + return c.hostname +} + +func (c *ConfigurationRepository) GetOrderBy() OrderBy { + return c.orderBy +} + +func (c *ConfigurationRepository) GetSyntaxHighlight() string { + return c.syntaxHighlight +} + func (c *ConfigurationRepository) GetListenAddr() string { return c.listenAddr } +func (c *ConfigurationRepository) GetPassphrase() []byte { + return c.passphrase +} + +func (c *ConfigurationRepository) GetBase64AesKey() []byte { + return c.aesKey +} + +func (c *ConfigurationRepository) IsAuthEnabled() bool { + return len(c.passphrase) != 0 +} + // 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 { @@ -128,8 +183,14 @@ func (c *ConfigurationRepository) expandOnScanPath(scanPath string, public bool) fullPath := path.Join(scanPath, e.Name()) if !c.repoExits(fullPath) { + + name := e.Name() + if c.removeSuffix { + name = strings.TrimSuffix(name, ".git") + } + c.repositories = append(c.repositories, &GitRepositoryConfiguration{ - Name: e.Name(), + Name: name, Path: fullPath, Public: public, }) @@ -155,7 +216,7 @@ func parse(r io.Reader) (*configuration, error) { config := defaultConfiguration() - err = setScan(block, config.Scan) + err = setScan(block, &config.Scans) if err != nil { return nil, err } @@ -165,11 +226,41 @@ func parse(r io.Reader) (*configuration, error) { return nil, err } + err = setHostname(block, &config.Hostname) + if err != nil { + return nil, err + } + err = setListenAddr(block, &config.ListenAddr) if err != nil { return nil, err } + err = setPassphrase(block, &config.Passphrase) + if err != nil { + return nil, err + } + + err = setAESKey(block, &config.AESKey) + if err != nil { + return nil, err + } + + err = setSyntaxHighlight(block, &config.SyntaxHighlight) + if err != nil { + return nil, err + } + + err = setOrderby(block, &config.OrderBy) + if err != nil { + return nil, err + } + + err = setRemoveSuffix(block, &config.RemoveSuffix) + if err != nil { + return nil, err + } + err = setRepositories(block, &config.Repositories) if err != nil { return nil, err @@ -230,18 +321,20 @@ func setRepositories(block scfg.Block, repositories *[]*GitRepositoryConfigurati func defaultConfiguration() *configuration { return &configuration{ - Scan: defaultScan(), + Scans: defaultScans(), RootReadme: "", + Hostname: defaultHostname(), ListenAddr: defaultAddr(), Repositories: make([]*GitRepositoryConfiguration, 0), } } -func defaultScan() *scan { - return &scan{ - Public: false, - Path: "", - } +func defaultHostname() string { + return "https://localhost:8080" +} + +func defaultScans() []*scan { + return []*scan{} } func defaultAddr() string { @@ -263,23 +356,62 @@ func setRootReadme(block scfg.Block, readme *string) error { return setString(scanDir, readme) } +func setHostname(block scfg.Block, hostname *string) error { + scanDir := block.Get("hostname") + return setString(scanDir, hostname) +} + +func setPassphrase(block scfg.Block, listenAddr *string) error { + scanDir := block.Get("passphrase") + return setString(scanDir, listenAddr) +} + +func setAESKey(block scfg.Block, listenAddr *string) error { + scanDir := block.Get("aes-key") + return setString(scanDir, listenAddr) +} + +func setSyntaxHighlight(block scfg.Block, listenAddr *string) error { + scanDir := block.Get("syntax-highlight") + return setString(scanDir, listenAddr) +} + +func setOrderby(block scfg.Block, orderBy *string) error { + scanDir := block.Get("order-by") + return setString(scanDir, orderBy) +} + func setListenAddr(block scfg.Block, listenAddr *string) error { scanDir := block.Get("listen-addr") return setString(scanDir, listenAddr) } -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 +func setRemoveSuffix(block scfg.Block, remove *bool) error { + scanDir := block.Get("remove-suffix") + return setBool(scanDir, remove) +} + +func setScan(block scfg.Block, scans *[]*scan) error { + for _, scanDir := range block.GetAll("scan") { + s := &scan{} + if scanDir == nil { + return nil + } + err := setString(scanDir, &s.Path) + if err != nil { + return err + } + + public := scanDir.Children.Get("public") + err = setBool(public, &s.Public) + if err != nil { + return err + } + + *scans = append(*scans, s) } - public := scanDir.Children.Get("public") - return setBool(public, &scan.Public) + return nil } func setBool(dir *scfg.Directive, field *bool) error { @@ -301,3 +433,24 @@ func setString(dir *scfg.Directive, field *string) error { } return nil } + +func parseOrderBy(s string) OrderBy { + switch s { + case "": + return LastCommitAsc + case "unordered": + return Unordered + case "alphabetical-asc": + return AlphabeticalAsc + case "alphabetical-desc": + return AlphabeticalDesc + case "lastcommit-asc": + return LastCommitAsc + case "lastcommit-desc": + return LastCommitDesc + default: + slog.Warn("Invalid order-by using default unordered") + return LastCommitAsc + + } +} |