diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-24 00:06:45 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-24 00:06:45 +0300 |
| commit | e637f4fbb06b1c0661d2e77ce79d0d5149ac5c47 (patch) | |
| tree | 2c975d88f76b6d7664c6ebef9261566a43637cbb | |
| parent | c3837b71a6c6dbb0292091922d209679533ec90d (diff) | |
Ensure proper exit codes for all error conditions
- Exit with code 1 when no action is specified (show usage)
- Remove redundant failedRepos tracking since we stop on first error
- Simplify code by removing unnecessary failure tracking arrays
- Add test script to verify exit code behavior
- All errors now consistently exit with code 1
- Successful operations exit with code 0
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
| -rw-r--r-- | cmd/gitsyncer/main.go | 30 | ||||
| -rwxr-xr-x | test/test_exit_codes.sh | 65 |
2 files changed, 70 insertions, 25 deletions
diff --git a/cmd/gitsyncer/main.go b/cmd/gitsyncer/main.go index 95305df..b7b974d 100644 --- a/cmd/gitsyncer/main.go +++ b/cmd/gitsyncer/main.go @@ -224,7 +224,7 @@ func main() { } syncer := sync.New(cfg, workDir) - failedRepos := []string{} + successCount := 0 for i, repo := range cfg.Repositories { fmt.Printf("\n[%d/%d] Syncing %s...\n", i+1, len(cfg.Repositories), repo) @@ -245,14 +245,7 @@ func main() { fmt.Printf("Stopping sync due to error.\n") os.Exit(1) } - } - - if len(failedRepos) > 0 { - fmt.Printf("\nFailed to sync %d repository(ies):\n", len(failedRepos)) - for _, repo := range failedRepos { - fmt.Printf(" - %s\n", repo) - } - os.Exit(1) + successCount++ } fmt.Printf("\nSuccessfully synced all %d repositories!\n", len(cfg.Repositories)) @@ -329,7 +322,6 @@ func main() { fmt.Printf("\nStarting sync of %d repositories...\n", len(repoNames)) syncer := sync.New(cfg, workDir) - failedRepos := []string{} successCount := 0 // Get Codeberg repos for description @@ -369,13 +361,6 @@ func main() { fmt.Printf("\n=== Summary ===\n") fmt.Printf("Successfully synced: %d repositories\n", successCount) - - if len(failedRepos) > 0 { - fmt.Printf("Failed to sync: %d repositories\n", len(failedRepos)) - for _, repo := range failedRepos { - fmt.Printf(" - %s\n", repo) - } - } } // End of if !dryRun if !syncGitHubPublic { @@ -442,7 +427,6 @@ func main() { fmt.Printf("\nStarting sync of %d repositories...\n", len(repoNames)) syncer := sync.New(cfg, workDir) - failedRepos := []string{} successCount := 0 // Get GitHub repos for description @@ -465,13 +449,6 @@ func main() { fmt.Printf("\n=== Summary ===\n") fmt.Printf("Successfully synced: %d repositories\n", successCount) - - if len(failedRepos) > 0 { - fmt.Printf("Failed to sync: %d repositories\n", len(failedRepos)) - for _, repo := range failedRepos { - fmt.Printf(" - %s\n", repo) - } - } } // End of if !dryRun os.Exit(0) @@ -499,4 +476,7 @@ func main() { fmt.Println(" --dry-run Show what would be done without doing it") fmt.Println("\nGitHub Token:") fmt.Println(" Set via: config file, GITHUB_TOKEN env var, or ~/.gitsyncer_github_token file") + + // Exit with error code when no action was specified + os.Exit(1) }
\ No newline at end of file diff --git a/test/test_exit_codes.sh b/test/test_exit_codes.sh new file mode 100755 index 0000000..742e2ed --- /dev/null +++ b/test/test_exit_codes.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# Test script to verify gitsyncer exit codes +echo "Testing gitsyncer exit codes..." + +GITSYNCER="../gitsyncer" + +# Test 1: Successful operation should exit 0 +echo -n "Test 1 - Successful operation (--version): " +$GITSYNCER --version >/dev/null 2>&1 +if [ $? -eq 0 ]; then + echo "PASS (exit code 0)" +else + echo "FAIL (expected 0, got $?)" +fi + +# Test 2: No arguments should exit 1 +echo -n "Test 2 - No arguments (show usage): " +$GITSYNCER >/dev/null 2>&1 +if [ $? -eq 1 ]; then + echo "PASS (exit code 1)" +else + echo "FAIL (expected 1, got $?)" +fi + +# Test 3: Invalid config file should exit 1 +echo -n "Test 3 - Invalid config file: " +$GITSYNCER --config /nonexistent/config.json --list-orgs >/dev/null 2>&1 +if [ $? -eq 1 ]; then + echo "PASS (exit code 1)" +else + echo "FAIL (expected 1, got $?)" +fi + +# Test 4: Sync non-existent repo should exit 1 +echo -n "Test 4 - Sync non-existent repo: " +$GITSYNCER --sync nonexistentrepo123456 >/dev/null 2>&1 +if [ $? -eq 1 ]; then + echo "PASS (exit code 1)" +else + echo "FAIL (expected 1, got $?)" +fi + +# Test 5: Successful list operation should exit 0 +echo -n "Test 5 - Successful list operation: " +$GITSYNCER --list-orgs >/dev/null 2>&1 +if [ $? -eq 0 ]; then + echo "PASS (exit code 0)" +else + echo "FAIL (expected 0, got $?)" +fi + +# Test 6: Test GitHub token without token should exit 1 +echo -n "Test 6 - Test GitHub token (invalid): " +GITHUB_TOKEN="invalid" $GITSYNCER --test-github-token >/dev/null 2>&1 +if [ $? -eq 1 ]; then + echo "PASS (exit code 1)" +else + echo "FAIL (expected 1, got $?)" +fi + +echo "" +echo "Summary: Exit codes are used correctly" +echo "- Exit 0: Successful operations" +echo "- Exit 1: Errors and invalid usage"
\ No newline at end of file |
