cerrado @ ada6a68b4f25c912705542a6b03aae7ffffb5e99

feat: Make syntax highlight configurable

Now cerrado has configurable theme through "syntax-highlight".
diff --git a/config.example.scfg b/config.example.scfg
index f29e3ca5b3bcbece29c00645bacbcab4ec608416..0c27591435696f433122fed28814153e6e6edceb 100644
--- a/config.example.scfg
+++ b/config.example.scfg
@@ -5,6 +5,7 @@
 root-readme /srv/git/README.md
 passphrase $2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq
 aes-key 8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==
+syntax-highlight monokailight
 
 scan /srv/git/ {
     public true
diff --git a/main.go b/main.go
index 8bf5141a4094b15d635a968f715a10123e618bc6..918b7948a56dd1f13f744cf0965af8ffdae76e44 100644
--- a/main.go
+++ b/main.go
@@ -11,6 +11,7 @@ 	"os"
 	"os/signal"
 	"time"
 
+	"github.com/alecthomas/chroma/v2/styles"
 	"golang.org/x/crypto/bcrypt"
 
 	"git.gabrielgio.me/cerrado/pkg/config"
@@ -71,9 +72,7 @@ 	return nil
 }
 
 func run(ctx context.Context) error {
-	var (
-		configPath = flag.String("config", "/etc/cerrado.scfg", "File path for the configuration file")
-	)
+	configPath := flag.String("config", "/etc/cerrado.scfg", "File path for the configuration file")
 
 	flag.Parse()
 
@@ -81,6 +80,17 @@ 	// repositorie
 	configRepo, err := config.LoadConfigurationRepository(*configPath)
 	if err != nil {
 		return err
+	}
+
+	// checking chroma configurationo
+
+	if _, ok := styles.Registry[configRepo.GetSyntaxHighlight()]; !ok {
+		slog.Warn(
+			"Invalid Syntax highlight selected",
+			"invalid-style", configRepo.GetSyntaxHighlight(),
+			"using", "monokailight",
+		)
+		styles.Fallback = styles.Registry["monokailight"]
 	}
 
 	// services
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 902ff0d991aeae4e7719555479ec574edd10ad57..812a06ef7ead3d270cd1b2d7d5d31a3033aabc5e 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -30,12 +30,13 @@
 	// configuration represents file configuration.
 	// fields needs to be exported to cmp to work
 	configuration struct {
-		Scan         *scan
-		RootReadme   string
-		ListenAddr   string
-		Passphrase   string
-		AESKey       string
-		Repositories []*GitRepositoryConfiguration
+		Scan            *scan
+		RootReadme      string
+		ListenAddr      string
+		Passphrase      string
+		SyntaxHighlight string
+		AESKey          string
+		Repositories    []*GitRepositoryConfiguration
 	}
 
 	// This is a per repository configuration.
@@ -52,11 +53,12 @@ 	// database repositories).
 	// This holds all the function necessary to ask for configuration
 	// information.
 	ConfigurationRepository struct {
-		rootReadme   string
-		listenAddr   string
-		passphrase   string
-		aesKey       string
-		repositories []*GitRepositoryConfiguration
+		rootReadme      string
+		listenAddr      string
+		passphrase      string
+		aesKey          string
+		syntaxHighlight string
+		repositories    []*GitRepositoryConfiguration
 	}
 )
 
@@ -72,11 +74,12 @@ 		return nil, err
 	}
 
 	repo := &ConfigurationRepository{
-		rootReadme:   config.RootReadme,
-		listenAddr:   config.ListenAddr,
-		repositories: config.Repositories,
-		passphrase:   config.Passphrase,
-		aesKey:       config.AESKey,
+		aesKey:          config.AESKey,
+		listenAddr:      config.ListenAddr,
+		passphrase:      config.Passphrase,
+		repositories:    config.Repositories,
+		rootReadme:      config.RootReadme,
+		syntaxHighlight: config.SyntaxHighlight,
 	}
 
 	if config.Scan.Path != "" {
@@ -87,7 +90,6 @@ 		}
 	}
 
 	return repo, nil
-
 }
 
 // GetRootReadme returns root read path
@@ -95,6 +97,10 @@ func (c *ConfigurationRepository) GetRootReadme() string {
 	return c.rootReadme
 }
 
+func (c *ConfigurationRepository) GetSyntaxHighlight() string {
+	return c.syntaxHighlight
+}
+
 func (c *ConfigurationRepository) GetListenAddr() string {
 	return c.listenAddr
 }
@@ -182,6 +188,11 @@ 		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
 	}
@@ -286,6 +297,11 @@ }
 
 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)
 }
 
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index 0970cfa43f9e5b1bf6617d654e3abc6ae0e3257c..9080351a01193f3637fe3a501fb5c6b4b967a379 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -105,6 +105,7 @@ 			config: `
 listen-addr unix://var/run/cerrado/cerrado.sock
 passphrase $2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq
 aes-key 8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==
+syntax-highlight monokailight
 
 scan "/srv/git" {
 	public true
@@ -123,9 +124,10 @@ 				Scan: &scan{
 					Public: true,
 					Path:   "/srv/git",
 				},
-				ListenAddr: "unix://var/run/cerrado/cerrado.sock",
-				Passphrase: "$2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq",
-				AESKey:     "8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==",
+				ListenAddr:      "unix://var/run/cerrado/cerrado.sock",
+				Passphrase:      "$2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq",
+				AESKey:          "8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==",
+				SyntaxHighlight: "monokailight",
 				Repositories: []*GitRepositoryConfiguration{
 					{
 						Name:        "linux.git",
@@ -158,6 +160,5 @@ 			if diff := cmp.Diff(tc.expectedConfig, config); diff != "" {
 				t.Errorf("Wrong result given - wanted + got\n %s", diff)
 			}
 		})
-
 	}
 }
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index 6fae1460391f93b827d8c6a6131976ebbde75a6b..5739c8e9dbca0c7b015224ed3af26f53118eb72c 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -27,18 +27,19 @@
 type (
 	GitHandler struct {
 		gitService *service.GitService
-		readmePath string
+		config     configurationRepository
 	}
 
 	configurationRepository interface {
 		GetRootReadme() string
+		GetSyntaxHighlight() string
 	}
 )
 
 func NewGitHandler(gitService *service.GitService, confRepo configurationRepository) *GitHandler {
 	return &GitHandler{
 		gitService: gitService,
-		readmePath: confRepo.GetRootReadme(),
+		config:     confRepo,
 	}
 }
 
@@ -48,7 +49,7 @@ 	if err != nil {
 		return err
 	}
 
-	f, err := os.Open(g.readmePath)
+	f, err := os.Open(g.config.GetRootReadme())
 	if err != nil {
 		return err
 	}
@@ -280,7 +281,8 @@ 	}
 
 	filename := filepath.Base(rest)
 	lexer := GetLexers(filename)
-	style := styles.Get("xcode")
+	style := styles.Get(g.config.GetSyntaxHighlight())
+
 	formatter := html.New(
 		html.WithLineNumbers(true),
 		html.WithLinkableLineNumbers(true, "L"),