diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-29 17:02:21 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-29 17:02:21 +0300 |
| commit | a956a672859e92190149506197ad0fa682e964e2 (patch) | |
| tree | a5f168aef3302ad9d633f1bca56f16b320fe3e19 /internal/config | |
| parent | 649a41dc0f0cbfa697408db2f006d106ba655933 (diff) | |
feat(showcase): uq move cgit host and output dir into config
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 60 | ||||
| -rw-r--r-- | internal/config/config_test.go | 49 |
2 files changed, 104 insertions, 5 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index c2084b1..afccf33 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,6 +8,10 @@ import ( "strings" ) +const ( + defaultShowcaseCgitHost = "https://cgit.f3s.buetow.org" +) + // Organization represents a git organization with its host and name type Organization struct { Host string `json:"host"` @@ -27,6 +31,8 @@ type Config struct { WorkDir string `json:"work_dir,omitempty"` // Working directory for cloning repositories ExcludeFromShowcase []string `json:"exclude_from_showcase,omitempty"` // Repository names to exclude from showcase ShowcaseStatsBranches map[string]string `json:"showcase_stats_branches,omitempty"` // Repository names mapped to the branch used for showcase stats/code snippets + ShowcaseOutputDir string `json:"showcase_output_dir,omitempty"` // Directory where showcase files and assets are written + ShowcaseCgitHost string `json:"showcase_cgit_host,omitempty"` // Base URL for cgit links in showcase output // SkipReleases maps a repository name to a list of tag names for which // releases should NOT be created on any platform (GitHub/Codeberg) SkipReleases map[string][]string `json:"skip_releases,omitempty"` @@ -77,13 +83,20 @@ func Load(path string) (*Config, error) { cfg.WorkDir = filepath.Join(home, "git", "gitsyncer-workdir") } - // Expand home directory in WorkDir if needed - if strings.HasPrefix(cfg.WorkDir, "~/") { - home, err := os.UserHomeDir() + // Expand home directory in WorkDir if needed. + expandedWorkDir, err := expandHomePath(cfg.WorkDir) + if err != nil { + return nil, err + } + cfg.WorkDir = expandedWorkDir + + // Expand home directory in showcase output directory if configured. + if strings.TrimSpace(cfg.ShowcaseOutputDir) != "" { + expandedShowcaseOutputDir, err := expandHomePath(cfg.ShowcaseOutputDir) if err != nil { - return nil, fmt.Errorf("failed to get home directory: %w", err) + return nil, err } - cfg.WorkDir = filepath.Join(home, cfg.WorkDir[2:]) + cfg.ShowcaseOutputDir = expandedShowcaseOutputDir } return &cfg, nil @@ -195,3 +208,40 @@ func (o *Organization) IsSSH() bool { return !o.IsGitHub() && !o.IsCodeberg() && !strings.HasPrefix(o.Host, "file://") && (strings.Contains(o.Host, "@") || strings.Contains(o.Host, ":")) } + +// GetShowcaseOutputDir returns the configured showcase output directory when +// present, otherwise the default output location. +func (c *Config) GetShowcaseOutputDir() (string, error) { + if c != nil && strings.TrimSpace(c.ShowcaseOutputDir) != "" { + return expandHomePath(c.ShowcaseOutputDir) + } + + home, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("failed to get home directory: %w", err) + } + return filepath.Join(home, "git", "foo.zone-content", "gemtext", "about"), nil +} + +// GetShowcaseCgitHost returns the configured cgit host, or the default host. +func (c *Config) GetShowcaseCgitHost() string { + if c != nil { + host := strings.TrimSpace(c.ShowcaseCgitHost) + if host != "" { + return strings.TrimRight(host, "/") + } + } + return defaultShowcaseCgitHost +} + +func expandHomePath(path string) (string, error) { + if strings.HasPrefix(path, "~/") { + home, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("failed to get home directory: %w", err) + } + return filepath.Join(home, path[2:]), nil + } + + return path, nil +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 45b8024..eb81626 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "path/filepath" "strings" "testing" ) @@ -68,3 +69,51 @@ func TestFindOrganization_ReturnsPointerToStoredElement(t *testing.T) { t.Fatalf("Organizations[0].Name = %q, want %q", cfg.Organizations[0].Name, "after") } } + +func TestGetShowcaseOutputDir_Default(t *testing.T) { + homeDir := t.TempDir() + t.Setenv("HOME", homeDir) + + cfg := &Config{} + got, err := cfg.GetShowcaseOutputDir() + if err != nil { + t.Fatalf("GetShowcaseOutputDir() error = %v", err) + } + + want := filepath.Join(homeDir, "git", "foo.zone-content", "gemtext", "about") + if got != want { + t.Fatalf("GetShowcaseOutputDir() = %q, want %q", got, want) + } +} + +func TestGetShowcaseOutputDir_OverrideAndExpandHome(t *testing.T) { + homeDir := t.TempDir() + t.Setenv("HOME", homeDir) + + cfg := &Config{ + ShowcaseOutputDir: "~/custom/showcase", + } + got, err := cfg.GetShowcaseOutputDir() + if err != nil { + t.Fatalf("GetShowcaseOutputDir() error = %v", err) + } + + want := filepath.Join(homeDir, "custom", "showcase") + if got != want { + t.Fatalf("GetShowcaseOutputDir() = %q, want %q", got, want) + } +} + +func TestGetShowcaseCgitHost_DefaultAndOverride(t *testing.T) { + t.Parallel() + + defaultCfg := &Config{} + if got := defaultCfg.GetShowcaseCgitHost(); got != "https://cgit.f3s.buetow.org" { + t.Fatalf("GetShowcaseCgitHost() default = %q, want %q", got, "https://cgit.f3s.buetow.org") + } + + customCfg := &Config{ShowcaseCgitHost: "https://example.test/cgit/"} + if got := customCfg.GetShowcaseCgitHost(); got != "https://example.test/cgit" { + t.Fatalf("GetShowcaseCgitHost() override = %q, want %q", got, "https://example.test/cgit") + } +} |
