diff options
Diffstat (limited to 'internal/cli/description_sync.go')
| -rw-r--r-- | internal/cli/description_sync.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/internal/cli/description_sync.go b/internal/cli/description_sync.go index 4904b56..317bcdf 100644 --- a/internal/cli/description_sync.go +++ b/internal/cli/description_sync.go @@ -2,6 +2,10 @@ package cli import ( "fmt" + "os" + "os/exec" + "path" + "path/filepath" "strings" "codeberg.org/snonux/gitsyncer/internal/codeberg" @@ -105,8 +109,75 @@ func syncRepoDescriptions(cfg *config.Config, dryRun bool, repoName, knownCBDesc } } + syncBackupDescriptions(cfg, dryRun, repoName, canonical) + // Update cache if cache != nil { cache[repoName] = canonical } } + +func syncBackupDescriptions(cfg *config.Config, dryRun bool, repoName, canonical string) { + if cfg == nil || canonical == "" { + return + } + + for i := range cfg.Organizations { + org := &cfg.Organizations[i] + if !org.BackupLocation { + continue + } + + supported, err := syncBackupDescription(org, repoName, canonical, dryRun) + if err != nil { + fmt.Printf(" Warning: Failed to update backup description on %s: %v\n", org.Host, err) + continue + } + if supported && !dryRun { + fmt.Printf(" Updated backup description for %s on %s\n", repoName, org.Host) + } + } +} + +func syncBackupDescription(org *config.Organization, repoName, description string, dryRun bool) (bool, error) { + if org == nil || !org.BackupLocation { + return false, nil + } + + description = strings.TrimSpace(description) + if description == "" { + return false, nil + } + + if strings.HasPrefix(org.Host, "file://") { + descriptionPath := filepath.Join(strings.TrimPrefix(org.Host, "file://"), repoName+".git", "description") + if dryRun { + fmt.Printf(" [DRY RUN] Would update backup description for %s on %s -> %q\n", repoName, org.Host, description) + return true, nil + } + return true, os.WriteFile(descriptionPath, []byte(description+"\n"), 0644) + } + + if strings.TrimSpace(org.DescriptionSyncHost) == "" || strings.TrimSpace(org.DescriptionSyncRoot) == "" { + return false, nil + } + + descriptionPath := path.Join(org.DescriptionSyncRoot, repoName+".git", "description") + if dryRun { + fmt.Printf(" [DRY RUN] Would update backup description for %s on %s -> %q\n", repoName, org.Host, description) + return true, nil + } + + cmd := exec.Command("ssh", org.DescriptionSyncHost, fmt.Sprintf("cat > %s", shellSingleQuote(descriptionPath))) + cmd.Stdin = strings.NewReader(description + "\n") + output, err := cmd.CombinedOutput() + if err != nil { + return true, fmt.Errorf("ssh write failed: %w\n%s", err, strings.TrimSpace(string(output))) + } + + return true, nil +} + +func shellSingleQuote(value string) string { + return "'" + strings.ReplaceAll(value, "'", `'\''`) + "'" +} |
