<?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: Khalid Elassaad</title>
    <description>The latest articles on DEV Community by Khalid Elassaad (@khalidelassaad).</description>
    <link>https://dev.to/khalidelassaad</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%2F909277%2Fba70a7b7-543a-4495-9c8c-3f8ffaff743a.jpeg</url>
      <title>DEV Community: Khalid Elassaad</title>
      <link>https://dev.to/khalidelassaad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khalidelassaad"/>
    <language>en</language>
    <item>
      <title>DevTool Intro: The Algolia CLI!</title>
      <dc:creator>Khalid Elassaad</dc:creator>
      <pubDate>Mon, 15 Aug 2022 19:11:00 +0000</pubDate>
      <link>https://dev.to/algolia/devtool-intro-the-algolia-cli-28c5</link>
      <guid>https://dev.to/algolia/devtool-intro-the-algolia-cli-28c5</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Wake up Neo...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need you to reset the configuration for our pharmaceutical products index! We were messing around with the highlighting settings, and now results for the query “red pills” are highlighted blue, which is really very confusing…&lt;/p&gt;

&lt;p&gt;Also, we’re a little concerned about the possibility of all experienced reality being a simulation run by machines to keep humanity enslaved!&lt;/p&gt;

&lt;p&gt;Please fix the index first though!&lt;/p&gt;

&lt;p&gt;— &lt;/p&gt;

&lt;p&gt;Oh man, don’t you all just love getting woken up before sunrise on a Monday for stuff like this? In an ideal world, this would be a quick fix. With a flourish of my black trench coat, I would roll out of bed and land in a crouch in the middle of the room, laptop in hand. I’d press enter to skip the white rabbit and ignore the knocking at my door. With one hand, I’d adjust my too-dark pince-nez glasses while the other hand fires off a command in seconds. Something short and sweet and to the point. Something like this…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia settings import prod_pharmaceuticals_index -F pharm_settings_snapshot.ndjson
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A glance at the terminal reveals the cheerful response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✓ Imported settings on prod_pharmaceuticals_index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nice… Now what was that other thing about machines enslaving humanity?&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  Meet the Algolia CLI!
&lt;/h3&gt;

&lt;p&gt;If you’re anything like me, the command line is your friend and trusted ally. Whether I’m testing a quick API call or coding in my favorite IDE, working with the command line keeps me iterating at a smooth clip. Additionally, I love that anything I do through a command line interface (CLI) can be easily scripted and scheduled to run however and whenever I like. With a strong set of tools in my CLI toolbelt, solving problems and automating solutions is a walk in the park. &lt;/p&gt;

&lt;p&gt;That’s why we are so excited to announce the Public Beta launch for the brand new Algolia CLI Tool, with the full release coming soon!&lt;/p&gt;

&lt;p&gt;Algolia CLI makes uploading an index, automating common dashboard operations, or saving and reloading snapshots of your configurations, possible right from the command line! No API client needed!&lt;/p&gt;

&lt;p&gt;So how do I start? Don’t worry, you won’t have to visit the oracle for this one… Getting started with the Algolia CLI is a breeze!&lt;/p&gt;

&lt;p&gt;If you’re on MacOS, simply use homebrew to install the tool by running this command in terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install algolia/algolia-cli/algolia
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want to poke at the code and build it yourself? The Algolia CLI is fully open-source, under an MIT license, and lives in a public github repository (which you can &lt;a href="https://github.com/algolia/cli"&gt;find here&lt;/a&gt;)!&lt;/p&gt;

&lt;p&gt;Releases for Linux and Windows coming soon!&lt;/p&gt;

&lt;p&gt;Let’s take a look at  some specific use cases to see how the Algolia CLI can make your life easier, so you can focus on saving the world, you star!&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  Example #0: Set your active Algolia application with profiles
&lt;/h3&gt;

&lt;p&gt;Any Algolia CLI command can be called with the &lt;code&gt;--admin-api-key [string]&lt;/code&gt; and &lt;code&gt;--application-id [string]&lt;/code&gt; flags to specify which application to interact with. To enable you to fire off commands without having to lug around api-keys and app-ids in your clipboard all the time, you may use the following command to register a default profile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia profile add --name [string] --app-id [string] --admin-api-key [string] --default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✓ Profile 'test_pharm_app' (##APP#ID##) successfully added and set as default.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;--default&lt;/code&gt; flag tells the CLI which app to use with future commands when no profile or API key/app ID pair are provided. Only one application can be set as default at a time. Taking time to register profiles makes future interactions fluid, especially when working with 2 or more Algolia apps. &lt;/p&gt;

&lt;p&gt;You can also interactively provide the fields for this command by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia profile add
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tool will then prompt you for each field, one at a time, and add the application as specified.&lt;/p&gt;

&lt;p&gt;To see a list of all added applications, simply run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia profile list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME                  APP ID      NUMBER OF INDICES  DEFAULT
test_pharm_app        #APP#ID#1#  2                  ✓
playground_pharm_app  #APP#ID#2#  1
prod_pharm_app        #APP#ID#3#  15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome! We see here that we have multiple Algolia apps added, and that the app &lt;code&gt;test_pharm_app&lt;/code&gt; is our default!&lt;/p&gt;

&lt;p&gt;Note: app profiles are saved in &lt;code&gt;~/.config/algolia/config.toml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let’s upload some data!&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  Example #1: Create an index and upload records from a file
&lt;/h3&gt;

&lt;p&gt;I want to create a new index, give it a relevant name, and upload the records from a file on my local system. Can the CLI handle that? Of course it can!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia objects import new_index_name -F ./path_to_file.ndjson
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you notice that we didn’t specify an app ID, api key, nor app profile? In this case, the CLI will act upon the default application (which is &lt;code&gt;test_pharm_app&lt;/code&gt; as indicated in the previous step).&lt;/p&gt;

&lt;p&gt;We can also pass content to the CLI for upload via stdin by replacing the &lt;code&gt;-F&lt;/code&gt; flag with &lt;code&gt;-&lt;/code&gt;. Let’s pipe some commands together! The following command produces the same result as the previous one(read file and upload contents to specified index), but uses cat to pass the contents of the file to the CLI tool’s stdin as opposed to passing the file path as an argument. Neat!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat ./path_to_file.ndjson | algolia objects import new_index_name -F -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="http://ndjson.org/"&gt;What is ndjson?&lt;/a&gt; Newline delimited JSON is the format the Algolia CLI reads from and writes to files. This means that any command that passes ndjson formatted data as output or accepts it as input can be piped together with an Algolia CLI command! We’ll see more of this in the next example&lt;/p&gt;

&lt;p&gt;– &lt;/p&gt;

&lt;h3&gt;
  
  
  Example #2: Managing index settings snapshots
&lt;/h3&gt;

&lt;p&gt;So far, we’ve connected our CLI to our Algolia apps, and we’ve uploaded some data to an index! Let’s take it a step further by snapshotting an index’s settings so we can restore them to a healthy checkpoint in the future.&lt;/p&gt;

&lt;p&gt;Which indices exist in our default app, &lt;code&gt;test_pharm_app&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia index list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME               ENTRIES  SIZE    UPDATED AT  CREATED AT  LAST BUILD DURATION  PRIMARY  REPLICAS
pills_treatments   1,000    100 kB  1 day ago   1 day ago   2s                            []
pills_cures        0        0 B     2 days ago  2 days ago  3s                            []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s save the settings for index &lt;code&gt;pills_treatments&lt;/code&gt; to a file on our system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia settings get pills_treatments &amp;gt; ./pt_settings_snapshot.ndjson
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Snapshot created! Now, reverting to the snapshot is as easy as…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia settings import pills_treatments -F ./pt_settings_snapshot.ndjson
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✓ Imported settings on pills_treatments
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also transfer settings from one index to another in one line! Let’s copy the settings for the &lt;code&gt;pills_treatments&lt;/code&gt; index over to the &lt;code&gt;pills_cures&lt;/code&gt; index! In the import command, we’ll use the &lt;code&gt;-&lt;/code&gt; value after the &lt;code&gt;-F&lt;/code&gt; flag to tell the CLI to read input from &lt;code&gt;stdin&lt;/code&gt; instead of a specified file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia settings get pills_treatments | algolia settings import pills_cures -F -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✓ Imported settings on pills_cures
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy! One more scenario… we’ve tested this out on all our indices in the test app, now we’re ready to migrate these settings to our production app! We’ll target the &lt;code&gt;pills_treatments_prod&lt;/code&gt; index in the &lt;code&gt;prod_pharm_app&lt;/code&gt; profile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia settings get pills_treatments | algolia settings import pills_treatments_prod -F - -p prod_pharm_app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✓ Imported settings on pills_treatments_prod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’re beginning to believe, aren’t you?&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  Example #3: CI/CD pipelines that execute Algolia tasks
&lt;/h3&gt;

&lt;p&gt;This is where things start to get really exciting. Consider the following workflow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A database contains your data.&lt;/li&gt;
&lt;li&gt;An automated script runs nightly to generate an index from the data in the database.&lt;/li&gt;
&lt;li&gt;An automated script uploads that index to Algolia.
Neat! Cool! Convenient! Let’s change that. We’re gonna mess things up a bit. How about we push a bug to prod?&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Engineer Anderson is tasked with performing a database migration. He creates a change!&lt;/li&gt;
&lt;li&gt;The change is reviewed by Engineer Becky, who says LGTM!&lt;/li&gt;
&lt;li&gt;The change is submitted!&lt;/li&gt;
&lt;li&gt;The CI/CD pipeline applies the change to the test database. No errors! Database migration tests pass!&lt;/li&gt;
&lt;li&gt;The change is applied to the prod database. No errors! Database migration tests pass!&lt;/li&gt;
&lt;li&gt;Engineer Anderson signs off with a smile on his face, goes home, and helps his landlady take out the garbage.
…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That night, while Engineer Anderson is sleeping peacefully and dreaming about dangling below a helicopter in flight, disaster strikes!&lt;/p&gt;

&lt;p&gt;…&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The automated index script runs at night, generating an index from the data in the database.&lt;/li&gt;
&lt;li&gt;However, the database migration transformed fields consumed by this script.&lt;/li&gt;
&lt;li&gt;The output index is missing an important search attribute, but uploads successfully.&lt;/li&gt;
&lt;li&gt;Searching the malformed index produces inaccurate and irrelevant results. Oh no!&lt;/li&gt;
&lt;li&gt;It isn’t until 3 days later, as search metrics drop, that the problem is even detected. It takes Engineer Anderson another day to roll back the changes and fix the index.
What can be done about this? How do you prevent this mess from ever happening again?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What if DB changes ran a few simple query test cases during the CI/CD pipeline? Each change would immediately generate an index and fire a series of test queries against it. Engineer Anderson’s change would have failed the CI/CD pipeline for producing different query results than expected. All it would take is the creation of a handful of test cases and a simple script to run the Algolia CLI’s &lt;code&gt;algolia search&lt;/code&gt; command against those test cases to detect this issue in advance!&lt;/p&gt;

&lt;p&gt;Let’s configure one such test case! When the index is working properly, we can execute a test query and save the results. The saved data is our “expected results”, against which we’ll compare the results of subsequent query tests. We’ll search an index from the previous example for “blue pills”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia search pills_treatments_prod -p prod_pharm_app --query "blue pills" &amp;gt; blue_pills_expected_results.ndjson
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool! We have a test case for the query “blue pills”! Let’s run this on the &lt;code&gt;pills_treatments&lt;/code&gt; index in the test app, &lt;code&gt;test_pharm_app&lt;/code&gt;, and see if it matches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;algolia search pills_treatments -p test_pharm_app --query "blue pills" &amp;gt; blue_pills_actual_results.ndjson &amp;amp;&amp;amp; diff blue_pills_expected_results.ndjson blue_pills_actual_results.ndjson | wc -m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





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

&lt;/div&gt;



&lt;p&gt;We query the test index and save the results. Then, we compare the expected results with the actual results by invoking the &lt;code&gt;diff&lt;/code&gt; command, which outputs only the difference between the two files. Finally, we pipe the output of &lt;code&gt;diff&lt;/code&gt; to the &lt;code&gt;wc -m&lt;/code&gt; command, which counts the number of characters in the output from &lt;code&gt;diff&lt;/code&gt;. We observe an output of 0, meaning there is no difference between the two files, which indicates that our query produced the same results in both indices and that our query test passed! &lt;/p&gt;

&lt;p&gt;Along those lines, a non-zero output would mean that there is some difference in the two files, and our query test failed to produce identical output across both indices.&lt;/p&gt;

&lt;p&gt;This is just one example of how you could integrate the Algolia CLI within a CI/CD pipeline. Imagine using the index settings management workflow to promote dashboard-configured settings set by your marketing team to a production app. The promotion can then invoke a run of query testing to validate the changes, and voila! The CLI becomes a force multiplier for your business users as well!&lt;/p&gt;

&lt;p&gt;Use the Algolia CLI to automate tasks, improve your workflows, and keep your search experiences running seamlessly, even as you change and develop the content under the hood!&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Involved!
&lt;/h3&gt;

&lt;p&gt;We’ve seen that the Algolia CLI can handle almost anything, from simple operations like index upload to more complex workflows like snapshot restoration. Stay tuned as we push updates out and further expand the CLI’s capabilities to make your life as an Algolia developer even easier!&lt;/p&gt;

&lt;p&gt;Remember, the Algolia CLI is in Public Beta! On MacOS, install with Homebrew:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install algolia/algolia-cli/algolia
Windows and Linux installers coming soon! 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, &lt;a href="https://github.com/algolia/cli"&gt;clone the GitHub repo&lt;/a&gt; and build it yourself!&lt;/p&gt;

&lt;p&gt;Want to learn more? Check out the &lt;a href="https://www.algolia.com/doc/tools/cli/get-started/overview/"&gt;Official CLI Documentation&lt;/a&gt; for a guided tutorial!&lt;/p&gt;

&lt;p&gt;And don’t forget to mark your calendars for September 14-15! The &lt;a href="https://www.algolia.com/devcon/"&gt;Algolia DevCon&lt;/a&gt; fast approaches. We’ll be spotlighting the CLI tool and demonstrating its capabilities! The Developer Experience team will be there to host live Q&amp;amp;A and answer all your questions. You won’t want to miss it!&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deprecation Advisory&lt;/strong&gt; - Before this CLI was created, we had an &lt;a href="https://www.npmjs.com/package/@algolia/cli"&gt;npm Algolia CLI package&lt;/a&gt; that served similar functionality. The Legacy CLI is &lt;strong&gt;DEPRECATED&lt;/strong&gt; - We will not be maintaining/updating/supporting it going forward. However, we will not remove the package from npm (since customers are still using it). Instead, we’ve announced the deprecation in the &lt;a href="https://github.com/algolia/algolia-cli-old"&gt;Legacy CLI GitHub repo&lt;/a&gt; (and renamed the repo to “algolia-cli-&lt;strong&gt;old&lt;/strong&gt;”), and are redirecting customers to the &lt;a href="https://github.com/algolia/cli"&gt;New CLI GitHub repo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>algolia</category>
      <category>cli</category>
      <category>shell</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
