<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Miha Filej</title>
    <description>The latest articles on DEV Community by Miha Filej (@mfilej).</description>
    <link>https://dev.to/mfilej</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F188742%2F18ada090-1eab-4bdc-9a7a-3a35f4fae918.jpg</url>
      <title>DEV Community: Miha Filej</title>
      <link>https://dev.to/mfilej</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mfilej"/>
    <language>en</language>
    <item>
      <title>Quickly format and manipulate JSON in vim using jq</title>
      <dc:creator>Miha Filej</dc:creator>
      <pubDate>Tue, 24 Jan 2023 20:45:20 +0000</pubDate>
      <link>https://dev.to/mfilej/quickly-format-and-manipulate-json-in-vim-using-jq-2963</link>
      <guid>https://dev.to/mfilej/quickly-format-and-manipulate-json-in-vim-using-jq-2963</guid>
      <description>&lt;p&gt;Ever needed to check whether the JSON you just wrote is valid? Or wanted to quickly rename keys in a JSON array?&lt;/p&gt;

&lt;p&gt;There are two components worth understanding that we will later put together.&lt;/p&gt;

&lt;p&gt;First, a brief introduction to &lt;code&gt;jq&lt;/code&gt; — a wonderful tool for manipulating JSON on the command line — &lt;em&gt;like &lt;code&gt;sed&lt;/code&gt; for JSON data&lt;/em&gt; as the &lt;a href="https://stedolan.github.io/jq/" rel="noopener noreferrer"&gt;official website&lt;/a&gt; describes it. It is truly powerful and able to manipulate JSON in a myriad of ways. When run with no arguments, it simply formats the given document:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ echo '{ "foo":   "bar"     }' | jq
{
  "foo": "bar"
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Second, vim allows you to pipe the contents of the current buffer into a unix command. The command for passing the current buffer through &lt;code&gt;jq&lt;/code&gt; would be:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

:% !jq


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Combining the two, we get a convenient way to format JSON, or really, manipulate the contents of a buffer in any way (another example I use often is sorting through &lt;code&gt;!sort&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fahas6xbfkwq3vyps7tcf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fahas6xbfkwq3vyps7tcf.gif" alt="A screen recording showing the described technique in action"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How about manipulating JSON? Let's look at two simple examples using the same JSON document. The following command will rename the &lt;code&gt;"size"&lt;/code&gt; key to &lt;code&gt;"variant"&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

:%!jq '.items |= map(.variant = .size | del(.size))'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;How about uppercasing every value in an array? Easy enough:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

:%!jq '.items |= map(.variant |= ascii_upcase)' 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Savvy vim users will recognize that &lt;code&gt;%&lt;/code&gt; is in essence just a way to tell vim to use every line in a buffer (see &lt;code&gt;:help :%&lt;/code&gt;). We can just as easily use a subset of the buffer, either by specifying the line numbers explicitly or using visual selection (&lt;code&gt;shift-v&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbeotyt7n732xovzhfuc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbeotyt7n732xovzhfuc.gif" alt="A screen recording showing how to pipe part of a buffer to a unix command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it! I hope this technique can streamline your workflow and make you a more efficient vim user.&lt;/p&gt;

&lt;p&gt;Bonus: a GPT-generated haiku about vim and the unix pipeline:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Vim sharp as a knife&lt;br&gt;
Unix pipeline, ever wise&lt;br&gt;
Together they thrive&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>vim</category>
      <category>json</category>
      <category>jq</category>
    </item>
    <item>
      <title>Back up Fly Postgres with GitHub Actions</title>
      <dc:creator>Miha Filej</dc:creator>
      <pubDate>Sat, 21 Jan 2023 17:09:16 +0000</pubDate>
      <link>https://dev.to/mfilej/back-up-fly-postgres-with-github-actions-ggf</link>
      <guid>https://dev.to/mfilej/back-up-fly-postgres-with-github-actions-ggf</guid>
      <description>&lt;p&gt;&lt;a href="https://fly.io/docs/postgres/" rel="noopener noreferrer"&gt;Fly Postgres&lt;/a&gt; stores its data on volumes, and the fine folks at Fly.io set up automatic snapshots for us. However, I sleep better at night knowing that my databases are backed up with &lt;code&gt;pg_dump&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I found surprisingly few results when I searched for a quick solution to this problem. &lt;a href="https://www.advantch.com/blog/automate-postgres-database-backups-on-fly-dot-io/" rel="noopener noreferrer"&gt;One of the solutions&lt;/a&gt; was a python script, which seemed overkill for such a simple task, but it was a useful starting point. In the end, I came up with a relatively short backup.yml that does the job:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The task will run every day at 4AM. &lt;code&gt;workflow_dispatch&lt;/code&gt; is there so that the task can be run manually as well.&lt;/p&gt;

&lt;p&gt;Don't forget to set the correct &lt;code&gt;region&lt;/code&gt;, update &lt;code&gt;PGUSER&lt;/code&gt;and &lt;code&gt;PGDATABASE&lt;/code&gt;, as well as replace &lt;code&gt;your-fly-db-app-name&lt;/code&gt;, &lt;code&gt;your-s3-bucket&lt;/code&gt;, and &lt;code&gt;your-path&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You will also need to visit your repository settings and set the following secrets: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FLY_API_TOKEN&lt;/code&gt; — A Fly personal access token (&lt;a href="https://fly.io/user/personal_access_tokens" rel="noopener noreferrer"&gt;generate one here&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PGPASSWORD&lt;/code&gt; — the password for the database being backed up&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And finally, don't forget: a backup is not a backup if you've never tried restoring it!&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
