Discover How Cloud Dev Environments Accelerate Developer Velocity and Drive Innovation by 50%.
Download eBook today!

Making Your CLI More Accessible Using Fig

No matter how many times I use kubectl or docker in a day, I just can't seem to remember the exact subcommands and what they do. I often find myself using --help and then reading through the output, trying to figure out what I need to run. This was getting exhausting, but I had come to terms with it, telling myself that there was no other way! That was until I got introduced to Fig.

Fig has been a game-changer in terms of how I use CLI tools. Since learning about it, not only have I been using it daily, but we've also added Fig support for Okteto CLI. Let's see what Fig does and how we integrated it with Okteto CLI so you, too, can leverage it and make working with CLIs a breeze.

So What’s the Hype About?

To quote their website, "Fig adds VSCode-style autocomplete to your existing terminal". And that is precisely what Fig does! Let me show you:

As soon as you install Fig, you get autocomplete suggestions for almost all the common CLI tools you might be using. As you saw in the demo above, this list of tools now also includes Okteto CLI 😎

I really like the fact that now each time I write okteto, not only can I go through the list of subcommands but also see a description for what each of them does and how to use it. The same is true for all the flags which each subcommand supports.

fig command descriptions

But it doesn't just stop here, another really cool thing I liked about Fig was "Generators". Generators load up autocomplete suggestions for a command by running another relevant command behind the scenes for you. For example, if you were to type okteto context use, Fig would run okteto context list behind the scenes and load up autocomplete suggestions based on its output for you. Here's how this looks:

Pretty cool, right? Now that you see all the cool stuff Fig can do, let me take you behind the scenes and show you how we added Fig integrations for Okteto CLI so you too can leverage it for your CLI!

Behind the Scenes

Fig's documentation does a pretty good job of explaining how you can add support for your CLI in Fig. But what surprised me was how simple this process is if your CLI is built with Cobra. Don't take my word for it, just look how Ramiro was able to integrate Fig with Okteto CLI following this doc - LIVE in less than 15 minutes!

After this, all we had to do was raise a pull request to withfig/autocomplete with the generated typescript file. That covered most of the autocomplete suggestions you see for Okteto CLI. But we didn't want to stop here. Remember those super cool Generators we talked about above? Fig made implementing those pretty simple too!

We just had to specify which command we wanted to run to generate the suggestions and how we wanted to process the output of that command to show the final suggestions. For example, in the case of okteto namespace use, we know that the user wants to switch their namespace, so we specified okteto namespace list as the command Fig should run in the background and then wrote some code telling Fig how to process the output.

const namespaces: Fig.Generator = {
  script: "okteto namespace list",
  cache: {
    ttl: 1000 * 60 * 30, // 30 minutes
  },
  postProcess: (output) => {
    return output
      .split("\n")
      .slice(1)
      .map((namespace, ind) => {
        namespace = namespace.split(" ")[0];
        return {
          name: namespace.replace("*", "").trim(),
          description: "Namespace",
          icon: "fig://icon?type=okteto",
        };
      });
  },
};

Another cool thing Fig allowed us to do was to cache the results of these commands we used to load the autocomplete suggestions so that the suggestions shown are almost instantaneous.

And that was it! Yes, it was this simple integrating Fig with Okteto CLI. If you're using Okteto CLI daily, do give Fig a try and let us know if you find it as useful as we do or not.

To conclude, I would say that Fig understands that a lot of people aren't as comfortable with the terminal as we usually like to believe. And even if you are on your terminal every day, the sheer amount of tools and subcommands to know keeps increasing every passing day. It is this very problem Fig solves - and they do a wonderful job of solving it. The trap that most developer tools fall into is that the solution they provide ends up being more complicated than the original problem they intended to solve. I was very glad to see Fig not falling down this hole and providing a simple yet extremely useful addition to my arsenal of daily tools!

Arsh SharmaDeveloper Experience Engineer / EmojiologistView all posts

Related content

Developing Microservices by Hot Reloading on Kubernetes Clusters

Let me jog your memory with how we used to develop applications not so long ago. We would bring up the application, write some code, hit save, see our...

August 11, 2022
Avatar of Arsh SharmaAvatar of Arsh SharmaArsh Sharma

Announcing the Launch of Okteto CLI 2.0!

I think you'll agree that, as developers, we enjoy innovating and working on exciting new features more than anything else. We'd get some coffee and play...

April 13, 2022
Avatar of Pablo Chico de GuzmanAvatar of Pablo Chico de GuzmanPablo Chico de Guzman