|
| 1 | +# Read files from a repository without cloning, in several modes. |
| 2 | + |
| 3 | +# Use gh as a credential helper |
| 4 | +exec gh auth setup-git |
| 5 | + |
| 6 | +# Create a private repo with a default branch |
| 7 | +exec gh repo create $ORG/$SCRIPT_NAME-$RANDOM_STRING --private --add-readme |
| 8 | +defer gh repo delete --yes $ORG/$SCRIPT_NAME-$RANDOM_STRING |
| 9 | + |
| 10 | +# Clone the repo and add a text file under a subdirectory |
| 11 | +exec gh repo clone $ORG/$SCRIPT_NAME-$RANDOM_STRING |
| 12 | +cd $SCRIPT_NAME-$RANDOM_STRING |
| 13 | +mkdir docs |
| 14 | +cp $WORK/guide.md docs/guide.md |
| 15 | +exec git add -A |
| 16 | +exec git commit -m 'Add docs/guide.md' |
| 17 | +exec git push origin main |
| 18 | + |
| 19 | +# Add a v2 branch where the same file has different content, to exercise --ref |
| 20 | +exec git checkout -b v2 |
| 21 | +cp $WORK/guide-v2.md docs/guide.md |
| 22 | +exec git add -A |
| 23 | +exec git commit -m 'Update docs/guide.md on v2' |
| 24 | +exec git push -u origin v2 |
| 25 | +exec git checkout main |
| 26 | + |
| 27 | +# Create a small binary file (PNG signature) via the Contents API |
| 28 | +exec gh api -X PUT repos/$ORG/$SCRIPT_NAME-$RANDOM_STRING/contents/assets/logo.png -f message='Add binary' -f content=iVBORw0KGgo= |
| 29 | + |
| 30 | +# Create a text file containing ANSI terminal escape sequences via the Contents API |
| 31 | +exec gh api -X PUT repos/$ORG/$SCRIPT_NAME-$RANDOM_STRING/contents/ansi.txt -f message='Add ansi' -f content=G1szMW1oZWxsbxtbMG0K |
| 32 | + |
| 33 | +# Read a file from the default branch: raw content goes to stdout |
| 34 | +exec gh repo read-file docs/guide.md |
| 35 | +cmp stdout $WORK/guide.md |
| 36 | + |
| 37 | +# Read the same file at a specific ref |
| 38 | +exec gh repo read-file docs/guide.md --ref v2 |
| 39 | +cmp stdout $WORK/guide-v2.md |
| 40 | + |
| 41 | +# Save a file to disk, and confirm --clobber is required to overwrite |
| 42 | +exec gh repo read-file docs/guide.md --output out.txt |
| 43 | +cmp out.txt $WORK/guide.md |
| 44 | +! exec gh repo read-file docs/guide.md --output out.txt |
| 45 | +stderr 'already exists' |
| 46 | +exec gh repo read-file docs/guide.md --output out.txt --clobber |
| 47 | +cmp out.txt $WORK/guide.md |
| 48 | + |
| 49 | +# Save into a directory: a trailing separator writes under the remote basename |
| 50 | +exec gh repo read-file docs/guide.md --output download-dir/ |
| 51 | +cmp download-dir/guide.md $WORK/guide.md |
| 52 | + |
| 53 | +# Save to an explicit path inside a directory, creating it as needed |
| 54 | +exec gh repo read-file docs/guide.md --output download-dir/out.txt |
| 55 | +cmp download-dir/out.txt $WORK/guide.md |
| 56 | + |
| 57 | +# JSON output exposes file metadata |
| 58 | +exec gh repo read-file docs/guide.md --json name,path,size,type --jq '.name' |
| 59 | +stdout '^guide\.md$' |
| 60 | + |
| 61 | +# Read a binary file: the raw bytes written to stdout match the saved copy, |
| 62 | +# and the base64 content is exposed via --json |
| 63 | +exec gh repo read-file assets/logo.png --output downloaded.png |
| 64 | +exists downloaded.png |
| 65 | +exec gh repo read-file assets/logo.png |
| 66 | +cmp stdout downloaded.png |
| 67 | +exec gh repo read-file assets/logo.png --json size,type,content --jq '.content' |
| 68 | +stdout '^iVBORw0KGgo=$' |
| 69 | + |
| 70 | +# A file with terminal escape sequences is refused by default, but readable |
| 71 | +# with --allow-escape-sequences |
| 72 | +! exec gh repo read-file ansi.txt |
| 73 | +stderr 'terminal escape sequences' |
| 74 | +exec gh repo read-file ansi.txt --allow-escape-sequences |
| 75 | +stdout 'hello' |
| 76 | + |
| 77 | +# Error: the path is a directory |
| 78 | +! exec gh repo read-file docs |
| 79 | +stderr 'is a directory' |
| 80 | + |
| 81 | +# Error: the path does not exist |
| 82 | +! exec gh repo read-file does/not/exist.md |
| 83 | +stderr 'Not Found' |
| 84 | + |
| 85 | +-- guide.md -- |
| 86 | +Hello from the default branch! |
| 87 | +-- guide-v2.md -- |
| 88 | +Hello from the v2 branch! |
0 commit comments