Adds initial gitlocal CLI and core functionality

Introduces the `gitlocal` command-line tool for managing nested Git repositories.

Includes the following main commands:
- `convert`: Renames `.git` to `.gitlocal`, allowing a parent repository to ignore the converted child repository. Supports recursive scanning and dry-run options. Tracks converted repositories in a global configuration.
- `revert`: Restores `.gitlocal` to `.git`. Includes an option to revert all tracked repositories.
- `status`: Displays a list of all repositories currently tracked by `gitlocal`, showing their path, conversion time, and original remote/branch.

Establishes internal modules for Git operations, configuration management, and recursive repository scanning.
Adds a comprehensive test suite covering core command logic and utility functions.
Initializes Go module and basic project `.gitignore`.
This commit is contained in:
2026-04-11 14:48:01 +01:00
parent c9867f410f
commit b5f1495680
16 changed files with 2263 additions and 0 deletions

463
cmd/cmd_test.go Normal file
View File

@@ -0,0 +1,463 @@
package cmd
import (
"bytes"
"os"
"strings"
"testing"
"git.membo.co.uk/dtomlinson/gitlocal/internal/config"
"git.membo.co.uk/dtomlinson/gitlocal/internal/git"
"git.membo.co.uk/dtomlinson/gitlocal/internal/testutil"
)
// setupTestConfig creates a temporary home directory and config for testing
func setupTestConfig(t *testing.T) (cleanup func()) {
t.Helper()
originalHome := os.Getenv("HOME")
tempHome := t.TempDir()
os.Setenv("HOME", tempHome)
return func() {
os.Setenv("HOME", originalHome)
}
}
func TestConvertSingleRepo(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
repoDir := testutil.CreateTempGitRepoWithRemote(t, "git@github.com:test/repo.git")
// Run convert command
convertCmd.Flags().Set("recursive", "false")
convertCmd.Flags().Set("dry-run", "false")
err := runConvert(convertCmd, []string{repoDir})
if err != nil {
t.Fatalf("convert command failed: %v", err)
}
// Verify .git was renamed to .gitlocal
if git.IsGitRepo(repoDir) {
t.Error("expected .git to be converted")
}
if !git.IsGitLocalRepo(repoDir) {
t.Error("expected .gitlocal to exist")
}
// Verify config was updated
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != 1 {
t.Fatalf("expected 1 repo in config, got %d", len(cfg.Repos))
}
if cfg.Repos[0].Path != repoDir {
t.Errorf("expected path %s, got %s", repoDir, cfg.Repos[0].Path)
}
if cfg.Repos[0].OriginalRemote != "git@github.com:test/repo.git" {
t.Errorf("expected remote to be saved in config, got %s", cfg.Repos[0].OriginalRemote)
}
}
func TestConvertDryRun(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
repoDir := testutil.CreateTempGitRepo(t)
// Run convert with dry-run
dryRun = true
defer func() { dryRun = false }()
convertCmd.Flags().Set("dry-run", "true")
err := runConvert(convertCmd, []string{repoDir})
if err != nil {
t.Fatalf("convert dry-run failed: %v", err)
}
// Verify nothing was actually converted
if !git.IsGitRepo(repoDir) {
t.Error("expected .git to still exist (dry-run)")
}
if git.IsGitLocalRepo(repoDir) {
t.Error("expected .gitlocal to not exist (dry-run)")
}
// Verify config was not updated
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != 0 {
t.Errorf("expected config to be empty (dry-run), got %d repos", len(cfg.Repos))
}
}
func TestConvertRecursive(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
rootDir, nestedDirs := testutil.CreateNestedRepoStructure(t)
// Run convert recursively
recursive = true
defer func() { recursive = false }()
convertCmd.Flags().Set("recursive", "true")
err := runConvert(convertCmd, []string{rootDir})
if err != nil {
t.Fatalf("convert recursive failed: %v", err)
}
// Verify all nested repos were converted
for _, nestedDir := range nestedDirs {
if git.IsGitRepo(nestedDir) {
t.Errorf("expected %s to be converted", nestedDir)
}
if !git.IsGitLocalRepo(nestedDir) {
t.Errorf("expected %s to have .gitlocal", nestedDir)
}
}
// Verify config has all repos
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != len(nestedDirs) {
t.Errorf("expected %d repos in config, got %d", len(nestedDirs), len(cfg.Repos))
}
}
func TestConvertAlreadyConverted(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
repoDir := testutil.CreateTempGitRepo(t)
// First convert
convertCmd.Flags().Set("recursive", "false")
err := runConvert(convertCmd, []string{repoDir})
if err != nil {
t.Fatalf("first convert failed: %v", err)
}
// Try to convert again - should register it
err = runConvert(convertCmd, []string{repoDir})
if err != nil {
t.Errorf("expected no error when re-converting, got: %v", err)
}
// Should still only have 1 entry in config
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != 1 {
t.Errorf("expected 1 repo in config, got %d", len(cfg.Repos))
}
}
func TestConvertNoGitRepo(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
nonGitDir := t.TempDir()
err := runConvert(convertCmd, []string{nonGitDir})
if err == nil {
t.Error("expected error when converting non-git directory")
}
if !strings.Contains(err.Error(), "no .git directory") {
t.Errorf("expected 'no .git directory' error, got: %v", err)
}
}
func TestRevertSingleRepo(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
repoDir := testutil.CreateTempGitRepo(t)
// Convert first
convertCmd.Flags().Set("recursive", "false")
if err := runConvert(convertCmd, []string{repoDir}); err != nil {
t.Fatalf("convert failed: %v", err)
}
// Now revert
err := runRevert(revertCmd, []string{repoDir})
if err != nil {
t.Fatalf("revert command failed: %v", err)
}
// Verify .gitlocal was renamed back to .git
if !git.IsGitRepo(repoDir) {
t.Error("expected .git to exist after revert")
}
if git.IsGitLocalRepo(repoDir) {
t.Error("expected .gitlocal to be gone after revert")
}
// Verify config was updated
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != 0 {
t.Errorf("expected config to be empty after revert, got %d repos", len(cfg.Repos))
}
}
func TestRevertAll(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
// Create and convert multiple repos
repo1 := testutil.CreateTempGitRepo(t)
repo2 := testutil.CreateTempGitRepo(t)
repo3 := testutil.CreateTempGitRepo(t)
for _, repo := range []string{repo1, repo2, repo3} {
convertCmd.Flags().Set("recursive", "false")
if err := runConvert(convertCmd, []string{repo}); err != nil {
t.Fatalf("convert failed for %s: %v", repo, err)
}
}
// Verify all were converted
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != 3 {
t.Fatalf("expected 3 repos in config, got %d", len(cfg.Repos))
}
// Revert all
revertAll = true
defer func() { revertAll = false }()
revertCmd.Flags().Set("all", "true")
err = runRevert(revertCmd, []string{})
if err != nil {
t.Fatalf("revert --all failed: %v", err)
}
// Verify all were reverted
for _, repo := range []string{repo1, repo2, repo3} {
if !git.IsGitRepo(repo) {
t.Errorf("expected %s to have .git after revert --all", repo)
}
if git.IsGitLocalRepo(repo) {
t.Errorf("expected %s to not have .gitlocal after revert --all", repo)
}
}
// Verify config is empty
cfg, err = config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != 0 {
t.Errorf("expected config to be empty after revert --all, got %d repos", len(cfg.Repos))
}
}
func TestRevertNoGitLocal(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
nonGitDir := t.TempDir()
err := runRevert(revertCmd, []string{nonGitDir})
if err == nil {
t.Error("expected error when reverting directory without .gitlocal")
}
if !strings.Contains(err.Error(), "no .gitlocal directory") {
t.Errorf("expected 'no .gitlocal directory' error, got: %v", err)
}
}
func TestStatus(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
// Create and convert a repo
repoDir := testutil.CreateTempGitRepoWithRemote(t, "git@github.com:test/repo.git")
convertCmd.Flags().Set("recursive", "false")
if err := runConvert(convertCmd, []string{repoDir}); err != nil {
t.Fatalf("convert failed: %v", err)
}
// Capture output
var buf bytes.Buffer
originalStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err := runStatus(statusCmd, []string{})
w.Close()
os.Stdout = originalStdout
buf.ReadFrom(r)
output := buf.String()
if err != nil {
t.Fatalf("status command failed: %v", err)
}
// Verify output contains repo info
if !strings.Contains(output, repoDir) {
t.Errorf("expected output to contain repo path %s", repoDir)
}
if !strings.Contains(output, "git@github.com:test/repo.git") {
t.Error("expected output to contain remote URL")
}
}
func TestStatusEmpty(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
// Capture output
var buf bytes.Buffer
originalStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err := runStatus(statusCmd, []string{})
w.Close()
os.Stdout = originalStdout
buf.ReadFrom(r)
output := buf.String()
if err != nil {
t.Fatalf("status command failed: %v", err)
}
if !strings.Contains(output, "No converted repositories") {
t.Error("expected 'No converted repositories' message")
}
}
func TestConvertCurrentDirectory(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
repoDir := testutil.CreateTempGitRepo(t)
// Change to repo directory
originalWd, _ := os.Getwd()
os.Chdir(repoDir)
defer os.Chdir(originalWd)
// Run convert without arguments (should use current directory)
convertCmd.Flags().Set("recursive", "false")
err := runConvert(convertCmd, []string{})
if err != nil {
t.Fatalf("convert current directory failed: %v", err)
}
// Verify conversion
if !git.IsGitLocalRepo(repoDir) {
t.Error("expected current directory to be converted")
}
}
func TestIntegrationFullWorkflow(t *testing.T) {
cleanup := setupTestConfig(t)
defer cleanup()
// Create a root repo with nested repos
rootDir, nestedDirs := testutil.CreateNestedRepoStructure(t)
// Step 1: Convert all recursively
recursive = true
defer func() { recursive = false }()
convertCmd.Flags().Set("recursive", "true")
if err := runConvert(convertCmd, []string{rootDir}); err != nil {
t.Fatalf("recursive convert failed: %v", err)
}
// Verify all converted
for _, nestedDir := range nestedDirs {
if !git.IsGitLocalRepo(nestedDir) {
t.Errorf("expected %s to be converted", nestedDir)
}
}
// Step 2: Check status
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != len(nestedDirs) {
t.Errorf("expected %d repos in config, got %d", len(nestedDirs), len(cfg.Repos))
}
// Step 3: Revert one repo
revertAll = false
if err := runRevert(revertCmd, []string{nestedDirs[0]}); err != nil {
t.Fatalf("revert single repo failed: %v", err)
}
// Verify it was reverted
if !git.IsGitRepo(nestedDirs[0]) {
t.Error("expected reverted repo to have .git")
}
// Step 4: Revert all remaining
revertAll = true
revertCmd.Flags().Set("all", "true")
if err := runRevert(revertCmd, []string{}); err != nil {
t.Fatalf("revert all failed: %v", err)
}
// Verify all reverted
for _, nestedDir := range nestedDirs {
if !git.IsGitRepo(nestedDir) {
t.Errorf("expected %s to be reverted", nestedDir)
}
}
// Verify config is empty
cfg, err = config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if len(cfg.Repos) != 0 {
t.Errorf("expected empty config, got %d repos", len(cfg.Repos))
}
}

219
cmd/convert.go Normal file
View File

@@ -0,0 +1,219 @@
package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"git.membo.co.uk/dtomlinson/gitlocal/internal/config"
"git.membo.co.uk/dtomlinson/gitlocal/internal/git"
"git.membo.co.uk/dtomlinson/gitlocal/internal/scanner"
"github.com/spf13/cobra"
)
var (
recursive bool
dryRun bool
)
var convertCmd = &cobra.Command{
Use: "convert [path]",
Short: "Convert .git to .gitlocal",
Long: `Convert renames .git to .gitlocal in the specified directory.
If no path is provided, uses the current directory.
Use --recursive to scan and convert all nested git repositories.`,
Args: cobra.MaximumNArgs(1),
RunE: runConvert,
}
func init() {
convertCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "Recursively convert all nested repos")
convertCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Show what would be converted without making changes")
}
func runConvert(cmd *cobra.Command, args []string) error {
// Determine target path
targetPath := "."
if len(args) > 0 {
targetPath = args[0]
}
// Get absolute path
absPath, err := filepath.Abs(targetPath)
if err != nil {
return fmt.Errorf("failed to get absolute path: %w", err)
}
// Load config
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if recursive {
return convertRecursive(absPath, cfg)
}
return convertSingle(absPath, cfg)
}
func convertSingle(path string, cfg *config.Config) error {
// Check if already converted
if git.IsGitLocalRepo(path) {
// Check if already in config
if existingRepo := cfg.FindRepo(path); existingRepo != nil {
fmt.Printf("⊘ Already converted and tracked: %s\n", path)
return nil
}
// Add to config
gitLocalPath := filepath.Join(path, git.GitLocalDir)
remoteURL := git.GetRemoteURL(gitLocalPath)
branch := git.GetCurrentBranch(gitLocalPath)
cfg.AddRepo(config.Repo{
Path: path,
ConvertedAt: time.Now(),
OriginalRemote: remoteURL,
OriginalBranch: branch,
})
if err := cfg.Save(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
fmt.Printf("✓ Registered already-converted repo: %s\n", path)
if remoteURL != "" {
fmt.Printf(" Remote: %s\n", remoteURL)
}
if branch != "" {
fmt.Printf(" Branch: %s\n", branch)
}
return nil
}
if !git.IsGitRepo(path) {
return fmt.Errorf("no .git directory found at %s", path)
}
if dryRun {
fmt.Printf("[DRY RUN] Would convert: %s\n", path)
return nil
}
// Get git info before converting
gitPath := filepath.Join(path, git.GitDir)
remoteURL := git.GetRemoteURL(gitPath)
branch := git.GetCurrentBranch(gitPath)
// Convert
if err := git.Convert(path); err != nil {
return err
}
// Update config
cfg.AddRepo(config.Repo{
Path: path,
ConvertedAt: time.Now(),
OriginalRemote: remoteURL,
OriginalBranch: branch,
})
if err := cfg.Save(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
fmt.Printf("✓ Converted: %s\n", path)
if remoteURL != "" {
fmt.Printf(" Remote: %s\n", remoteURL)
}
if branch != "" {
fmt.Printf(" Branch: %s\n", branch)
}
return nil
}
func convertRecursive(rootPath string, cfg *config.Config) error {
repos, err := scanner.FindNestedGitRepos(rootPath)
if err != nil {
return fmt.Errorf("failed to scan for nested repos: %w", err)
}
if len(repos) == 0 {
fmt.Println("No nested git repositories found")
return nil
}
fmt.Printf("Found %d nested git repositories\n\n", len(repos))
converted := 0
registered := 0
for _, repoPath := range repos {
if git.IsGitLocalRepo(repoPath) {
// Check if already in config
if existingRepo := cfg.FindRepo(repoPath); existingRepo != nil {
fmt.Printf("⊘ Skipped (already converted and tracked): %s\n", repoPath)
continue
}
// Register already-converted repo
gitLocalPath := filepath.Join(repoPath, git.GitLocalDir)
remoteURL := git.GetRemoteURL(gitLocalPath)
branch := git.GetCurrentBranch(gitLocalPath)
cfg.AddRepo(config.Repo{
Path: repoPath,
ConvertedAt: time.Now(),
OriginalRemote: remoteURL,
OriginalBranch: branch,
})
fmt.Printf("✓ Registered already-converted: %s\n", repoPath)
registered++
continue
}
if dryRun {
fmt.Printf("[DRY RUN] Would convert: %s\n", repoPath)
continue
}
// Get git info before converting
gitPath := filepath.Join(repoPath, git.GitDir)
remoteURL := git.GetRemoteURL(gitPath)
branch := git.GetCurrentBranch(gitPath)
// Convert
if err := git.Convert(repoPath); err != nil {
fmt.Fprintf(os.Stderr, "✗ Failed to convert %s: %v\n", repoPath, err)
continue
}
// Update config
cfg.AddRepo(config.Repo{
Path: repoPath,
ConvertedAt: time.Now(),
OriginalRemote: remoteURL,
OriginalBranch: branch,
})
fmt.Printf("✓ Converted: %s\n", repoPath)
converted++
}
if !dryRun && (converted > 0 || registered > 0) {
if err := cfg.Save(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
fmt.Printf("\nSuccessfully converted %d repositories", converted)
if registered > 0 {
fmt.Printf(", registered %d already-converted", registered)
}
fmt.Println()
}
return nil
}

129
cmd/revert.go Normal file
View File

@@ -0,0 +1,129 @@
package cmd
import (
"fmt"
"os"
"path/filepath"
"git.membo.co.uk/dtomlinson/gitlocal/internal/config"
"git.membo.co.uk/dtomlinson/gitlocal/internal/git"
"github.com/spf13/cobra"
)
var revertAll bool
var revertCmd = &cobra.Command{
Use: "revert [path]",
Short: "Revert .gitlocal back to .git",
Long: `Revert renames .gitlocal back to .git in the specified directory.
If no path is provided, uses the current directory.
Use --all to revert all tracked repositories from the config.`,
Args: cobra.MaximumNArgs(1),
RunE: runRevert,
}
func init() {
revertCmd.Flags().BoolVarP(&revertAll, "all", "a", false, "Revert all tracked repositories")
}
func runRevert(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if revertAll {
return revertAllRepos(cfg)
}
// Determine target path
targetPath := "."
if len(args) > 0 {
targetPath = args[0]
}
// Get absolute path
absPath, err := filepath.Abs(targetPath)
if err != nil {
return fmt.Errorf("failed to get absolute path: %w", err)
}
return revertSingle(absPath, cfg)
}
func revertSingle(path string, cfg *config.Config) error {
if !git.IsGitLocalRepo(path) {
return fmt.Errorf("no .gitlocal directory found at %s", path)
}
if git.IsGitRepo(path) {
return fmt.Errorf(".git already exists at %s", path)
}
// Revert
if err := git.Revert(path); err != nil {
return err
}
// Remove from config
cfg.RemoveRepo(path)
if err := cfg.Save(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
fmt.Printf("✓ Reverted: %s\n", path)
return nil
}
func revertAllRepos(cfg *config.Config) error {
if len(cfg.Repos) == 0 {
fmt.Println("No repositories to revert")
return nil
}
fmt.Printf("Reverting %d repositories\n\n", len(cfg.Repos))
reverted := 0
failed := 0
// Create a copy of repos to iterate over, since we'll be modifying the config
repos := make([]config.Repo, len(cfg.Repos))
copy(repos, cfg.Repos)
for _, repo := range repos {
if !git.IsGitLocalRepo(repo.Path) {
fmt.Fprintf(os.Stderr, "⊘ Skipped (no .gitlocal found): %s\n", repo.Path)
// Remove from config even if .gitlocal doesn't exist
cfg.RemoveRepo(repo.Path)
continue
}
if git.IsGitRepo(repo.Path) {
fmt.Fprintf(os.Stderr, "⊘ Skipped (.git exists): %s\n", repo.Path)
continue
}
if err := git.Revert(repo.Path); err != nil {
fmt.Fprintf(os.Stderr, "✗ Failed to revert %s: %v\n", repo.Path, err)
failed++
continue
}
cfg.RemoveRepo(repo.Path)
fmt.Printf("✓ Reverted: %s\n", repo.Path)
reverted++
}
if err := cfg.Save(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
fmt.Printf("\nSuccessfully reverted %d repositories", reverted)
if failed > 0 {
fmt.Printf(" (%d failed)", failed)
}
fmt.Println()
return nil
}

31
cmd/root.go Normal file
View File

@@ -0,0 +1,31 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "gitlocal",
Short: "Manage nested git repositories",
Long: `gitlocal helps you manage nested git repositories by converting .git directories
to .gitlocal, allowing parent repos to track nested project files.
This is useful when you have a knowledge base or monorepo that contains
multiple smaller projects, some of which use git locally.`,
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func init() {
rootCmd.AddCommand(convertCmd)
rootCmd.AddCommand(revertCmd)
rootCmd.AddCommand(statusCmd)
}

84
cmd/status.go Normal file
View File

@@ -0,0 +1,84 @@
package cmd
import (
"fmt"
"time"
"git.membo.co.uk/dtomlinson/gitlocal/internal/config"
"github.com/spf13/cobra"
)
var statusCmd = &cobra.Command{
Use: "status",
Short: "Show all converted repositories",
Long: `Display a list of all repositories that have been converted from .git to .gitlocal.`,
RunE: runStatus,
}
func runStatus(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if len(cfg.Repos) == 0 {
fmt.Println("No converted repositories")
return nil
}
fmt.Printf("Converted Repositories (%d):\n\n", len(cfg.Repos))
for _, repo := range cfg.Repos {
fmt.Printf(" %s\n", repo.Path)
fmt.Printf(" Converted: %s\n", formatTime(repo.ConvertedAt))
if repo.OriginalRemote != "" {
fmt.Printf(" Remote: %s\n", repo.OriginalRemote)
}
if repo.OriginalBranch != "" {
fmt.Printf(" Branch: %s\n", repo.OriginalBranch)
}
fmt.Println()
}
return nil
}
func formatTime(t time.Time) string {
now := time.Now()
diff := now.Sub(t)
switch {
case diff < time.Minute:
return "just now"
case diff < time.Hour:
mins := int(diff.Minutes())
if mins == 1 {
return "1 minute ago"
}
return fmt.Sprintf("%d minutes ago", mins)
case diff < 24*time.Hour:
hours := int(diff.Hours())
if hours == 1 {
return "1 hour ago"
}
return fmt.Sprintf("%d hours ago", hours)
case diff < 7*24*time.Hour:
days := int(diff.Hours() / 24)
if days == 1 {
return "1 day ago"
}
return fmt.Sprintf("%d days ago", days)
case diff < 30*24*time.Hour:
weeks := int(diff.Hours() / 24 / 7)
if weeks == 1 {
return "1 week ago"
}
return fmt.Sprintf("%d weeks ago", weeks)
default:
months := int(diff.Hours() / 24 / 30)
if months == 1 {
return "1 month ago"
}
return fmt.Sprintf("%d months ago", months)
}
}