Skip to content
Myr

default.yml

This file

Path

docs/content/1.tutorial/default.yml

322 lines · 13.5 KiB · YAML

Last changed by

Copy over changes made to gh-pages directlyDavid Tolnaybefore adoption81f328f

History of this file

Take it

The walk stopped after 200 commits: lines older than 5634467 point at it.

ascii_upcase/0 and ascii_downcase/0pkoppsteinbefore adoption5634467

lines 1–152why, one step back
1headline: Tutorial
2body:3 - text: |45 GitHub has a JSON API, so let's play with that. This URL gets6 us the last 5 commits from the jq repo:78 - command: "curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'"9 result: |10 [11 {12 "sha": "d25341478381063d1c76e81b3a52e0592a7c997f",13 "commit": {14 "author": {15 "name": "Stephen Dolan",16 "email": "mu@netsoc.tcd.ie",17 "date": "2013-06-22T16:30:59Z"18 },19 "committer": {20 "name": "Stephen Dolan",21 "email": "mu@netsoc.tcd.ie",22 "date": "2013-06-22T16:30:59Z"23 },24 "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",25 "tree": {26 "sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40",27 "url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40"28 },29 "url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",30 "comment_count": 031 },32 "url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",33 "html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",34 "comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",35 "author": {36 "login": "stedolan",37 ...3839 - text: |40 Github returns a ton of info here, and it's relatively hard to41 read as it is. To syntax highlight it and order the attributes we pipe42 it through jq, telling jq to just spit the input back at us using43 the expression `.`:4445 - command: "curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'"46 result: |47 [48 {49 "parents": [50 {51 "html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",52 "url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",53 "sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"54 },55 {56 "html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",57 "url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",58 "sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"59 }60 ],61 "committer": {62 "type": "User",63 "received_events_url": "https://api.github.com/users/stedolan/received_events",64 "events_url": "https://api.github.com/users/stedolan/events{/privacy}",65 "repos_url": "https://api.github.com/users/stedolan/repos",66 "organizations_url": "https://api.github.com/users/stedolan/orgs",67 ...6869 - text: |7071 There's still far too much info for our purposes, so we can try72 just grab the first commit:737475 - command: "curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'"76 result: |77 {78 "parents": [79 {80 "html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",81 "url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",82 "sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"83 },84 {85 "html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",86 "url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",87 "sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"88 }89 ],90 "committer": {91 "type": "User",92 "received_events_url": "https://api.github.com/users/stedolan/received_events",93 "events_url": "https://api.github.com/users/stedolan/events{/privacy}",94 "repos_url": "https://api.github.com/users/stedolan/repos",95 "organizations_url": "https://api.github.com/users/stedolan/orgs",96 "subscriptions_url": "https://api.github.com/users/stedolan/subscriptions",97 "starred_url": "https://api.github.com/users/stedolan/starred{/owner}{/repo}",98 "gists_url": "https://api.github.com/users/stedolan/gists{/gist_id}",99 "login": "stedolan",100 "id": 79765,101 "avatar_url": "https://1.gravatar.com/avatar/31de909d8e55dd07ed782d92ece59842?d=https%3A%2F%2Fidenticons.github.com%2Ffc5b6765b1c9cfaecea48ae71df4d279.png",102 "gravatar_id": "31de909d8e55dd07ed782d92ece59842",103 "url": "https://api.github.com/users/stedolan",104 "html_url": "https://github.com/stedolan",105 "followers_url": "https://api.github.com/users/stedolan/followers",106 "following_url": "https://api.github.com/users/stedolan/following{/other_user}"107 },108 "author": {109 "type": "User",110 "received_events_url": "https://api.github.com/users/stedolan/received_events",111 "events_url": "https://api.github.com/users/stedolan/events{/privacy}",112 "repos_url": "https://api.github.com/users/stedolan/repos",113 "organizations_url": "https://api.github.com/users/stedolan/orgs",114 "subscriptions_url": "https://api.github.com/users/stedolan/subscriptions",115 "starred_url": "https://api.github.com/users/stedolan/starred{/owner}{/repo}",116 "gists_url": "https://api.github.com/users/stedolan/gists{/gist_id}",117 "login": "stedolan",118 "id": 79765,119 "avatar_url": "https://1.gravatar.com/avatar/31de909d8e55dd07ed782d92ece59842?d=https%3A%2F%2Fidenticons.github.com%2Ffc5b6765b1c9cfaecea48ae71df4d279.png",120 "gravatar_id": "31de909d8e55dd07ed782d92ece59842",121 "url": "https://api.github.com/users/stedolan",122 "html_url": "https://github.com/stedolan",123 "followers_url": "https://api.github.com/users/stedolan/followers",124 "following_url": "https://api.github.com/users/stedolan/following{/other_user}"125 },126 "comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",127 "html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",128 "url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",129 "commit": {130 "comment_count": 0,131 "url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",132 "tree": {133 "url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40",134 "sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40"135 },136 "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",137 "committer": {138 "date": "2013-06-22T16:30:59Z",139 "email": "mu@netsoc.tcd.ie",140 "name": "Stephen Dolan"141 },142 "author": {143 "date": "2013-06-22T16:30:59Z",144 "email": "mu@netsoc.tcd.ie",145 "name": "Stephen Dolan"146 }147 },148 "sha": "d25341478381063d1c76e81b3a52e0592a7c997f"149 }150151 - text: |152

Copy over changes made to gh-pages directlyDavid Tolnaybefore adoption81f328f

line 153why, one step back
153 For the rest of the examples, I'll leave out the `curl` command - it's not going to change.

ascii_upcase/0 and ascii_downcase/0pkoppsteinbefore adoption5634467

lines 154–205why, one step back
154
155 There's still a lot of info we don't care about there, so we'll156 restrict it down to the most interesting fields.157158159 - command: "jq '.[0] | {message: .commit.message, name: .commit.committer.name}'"160 result: |161 {162 "name": "Stephen Dolan",163 "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"164 }165166 - text: |167168 The `|` operator in jq feeds the output of one filter169 (`.[0]` which gets the first element of the array in the response)170 into the input of another (`{...}` which171 builds an object out of those fields). You can access nested attributes,172 such as `.commit.message`:173174 Now let's get the rest of the commits:175176177 - command: "jq '.[] | {message: .commit.message, name: .commit.committer.name}'"178 result: |179 {180 "name": "Stephen Dolan",181 "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"182 }183 {184 "name": "Stephen Dolan",185 "message": "Reject all overlong UTF8 sequences."186 }187 {188 "name": "Stephen Dolan",189 "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."190 }191 {192 "name": "Stephen Dolan",193 "message": "Fix example in manual for `floor`. See #155."194 }195 {196 "name": "Nicolas Williams",197 "message": "Document floor"198 }199200 - text: |201202 `.[]` returns each element of the array returned in the response,203 one at a time, which are all fed into `{message: .commit.message, name: .commit.committer.name}`.204205

Copy over changes made to gh-pages directlyDavid Tolnaybefore adoption81f328f

line 206why, one step back
206 Data in jq is represented as streams of JSON values - every jq

ascii_upcase/0 and ascii_downcase/0pkoppsteinbefore adoption5634467

lines 207–210why, one step back
207 expression runs for each value in its input stream, and can
208 produce any number of values to its output stream.209210 Streams are serialised by just separating JSON values with

Copy over changes made to gh-pages directlyDavid Tolnaybefore adoption81f328f

line 211why, one step back
211 whitespace. This is a `cat`-friendly format - you can just join

ascii_upcase/0 and ascii_downcase/0pkoppsteinbefore adoption5634467

lines 212–322why, one step back
212 two JSON streams together and get a valid JSON stream.
213214 If you want to get the output as a single array, you can tell jq to215 "collect" all of the answers by wrapping the filter in square216 brackets:217218 - command: "jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'"219 result: |220 [221 {222 "name": "Stephen Dolan",223 "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"224 },225 {226 "name": "Stephen Dolan",227 "message": "Reject all overlong UTF8 sequences."228 },229 {230 "name": "Stephen Dolan",231 "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."232 },233 {234 "name": "Stephen Dolan",235 "message": "Fix example in manual for `floor`. See #155."236 },237 {238 "name": "Nicolas Williams",239 "message": "Document floor"240 }241 ]242 - text: |243244 - - -245246 Next, let's try getting the URLs of the parent commits out of the247 API results as well. In each commit, the GitHub API includes information248 about "parent" commits. There can be one or many.249250 "parents": [251 {252 "html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",253 "url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",254 "sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7"255 },256 {257 "html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a",258 "url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",259 "sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a"260 }261 ]262263 We want to pull out all of the "html_url" fields inside that array of parent264 commits and make a simple list of strings to go along with the265 "message" and "author" fields we already have.266267268269 - command: "jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'"270 result: |271 [272 {273 "parents": [274 "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",275 "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a"276 ],277 "name": "Stephen Dolan",278 "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161"279 },280 {281 "parents": [282 "https://github.com/stedolan/jq/commit/ff48bd6ec538b01d1057be8e93b94eef6914e9ef"283 ],284 "name": "Stephen Dolan",285 "message": "Reject all overlong UTF8 sequences."286 },287 {288 "parents": [289 "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7"290 ],291 "name": "Stephen Dolan",292 "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences."293 },294 {295 "parents": [296 "https://github.com/stedolan/jq/commit/3dcdc582ea993afea3f5503a78a77675967ecdfa"297 ],298 "name": "Stephen Dolan",299 "message": "Fix example in manual for `floor`. See #155."300 },301 {302 "parents": [303 "https://github.com/stedolan/jq/commit/7c4171d414f647ab08bcd20c76a4d8ed68d9c602"304 ],305 "name": "Nicolas Williams",306 "message": "Document floor"307 }308 ]309310 - text: |311312 Here we're making an object as before, but this time the `parents`313 field is being set to `[.parents[].html_url]}]`, which collects314 all of the parent commit URLs defined in the parents object.315316 - text: |317318 - - -319320 Here endeth the tutorial! There's lots more to play with, go321 read [the manual](../manual/) if you're interested, and [download322 jq](../download/) if you haven't already.