How To Create A "Proper" CLI With Shell And Charm Gum

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
sooner or later everyone ends up creating acli or two or three for most of us that is a shell script used to automate the execution of some commands but what if you want to create a CLI that is more than just a bunch of commands what if you want to create a CLI or command line interface that is used by humans it might need to have prettier outputs some inputs a bit of formatting some choices and so on and so forth when we get to that point we often start thinking about using a real language like go or python or whatever you prefer right most of the third-party clis we might be using right now are written in one of those languages Cube cattle gcloud Helen Docker and so on and so forth all those and most of the other clis we use right now are written in one of the real languages when we need something serious we turn away from bash scripting to other languages I am not an exception I wrote my share of shell scripts but also quite a few clis lately mostly in go shell scripts work great when a limited number of commands should be executed without any interaction CLI is written in other languages are a better choice when there is a greater complexity or greater user interaction but and here's a big but it does not have to be that way as a matter of fact there are quite a few cases when I need user interaction but I do not want to go through the trouble of writing acli in go or python or whichever language I or You might prefer so today I want to explore ways to create shell scripts that look and feel like proper clis I need something relatively simple but also something that can be used to create a CLI that is used by humans like the one watching this video right now I want to create proper CLI experience by writing shell scripts I want the way to do it fast and easy but with awesome awesome user experience [Music] foreign let me give you a bit of background among many other things I produce videos on YouTube you already know that right you're watching one of them right now right now most of the videos come with just split into three sections setup demo and Destroy I exclude the boring part of September instruction from the videos but they do provide the instructions how to do that in gists there is one in the description of this video as well so go and check it out depending on the topic the setup and these three sections can be only a command or two or they can be quite quite longer there are cases when there are hundreds of lines of instructions like in the case of the video I'm working on right now which you should be able to watch a week after this video is published maybe two something like that there are commands you can copy and paste there are instructions to do certain operations through consoles there are input prompts and so on and so forth it's a bit of a mess and your experience might not be the best so I decided to do something about it I wanted to give you a better experience I wanted to create clis for that and all the upcoming videos so instead of following often lengthy instructions you could just run a command and let the CLI do the rest however and this is a big one creating a CLI for each video in go would be a overkill on the other hand it would be just as complicated to create the CLI in bar since it requires things that cannot easily be done in bash I want pretty outputs I want to be able to ask for user inputs I want to be able to format the outputs I want to allow you to choose from some options and so on and so forth and that's where charm gum comes in swoops in Trump gum is one of the projects within the charm family and charm is a collection of tools for building and I'm quoting from their website beautiful command line interfaces and it's probably one of the most underrated groups of projects I know of everything everything they do is absolutely amazing charm is my first stop whenever I need to do something extra related to command line interfaces VHS for example allows me to create animated gifs by executing a command or a script soft serve is a simple git server I can launch from the command line charm Libs is a collection of go libraries that convert boring clis into absolutely awesome ones and so on and so forth charm is a collection of quite a few tools that make your life easier when it comes to creating awesome clis or working from a command line but today I will not have time to go through everything the charm has instead I want to focus on one of the projects called charm Gum as a way to create glamorous shell scripts so let's start by seeing it in action first look at this script it's a normal script sh script yet at the very beginning I ask people for inputs hey are you ready to start say yes or no I have a table of requirements nicely formatted with name required and more info I have another question hey do you want do you have those not do you want do you have those tools installed which by the way is something I should solve as well but that would be a separate subject I also have some choices instead of uh yes or no hey do you want to use Google or AWS or Azure there are user inputs like what is the name of your GitHub organization and so on and so forth so this is a simple shell script yet it looks and it behaves like a proper CLI with inputs with outputs some commands executed in between all these and some choices giving you to choose from and everything nicely output then present it on a screen is on and so forth from the user perspective or from the perspective of somebody executing these scripts this looks and fills like a proper like an awesome CLI even though it's just a shot script so how do they do that well let me show you actually I do not even need to show you the script I can show you the commands that I used and later on we can jump back into the script so let's take a look at some of the commands if not all the charmgum offers so let's say that I want to show some output but nicely formatted something that looks good and not like an average uh you know output from a terminal screen so let's say that I want to have different colors that I want to have a frame around some text so that it's clear that this is something important how would they do that well it's actually simple I can use charm style that has certain arguments like foreground color is 212 and Border background is also 212 and I want double border and I want margins and I want some paddings and the text is here is some text etc etc etc right and when I execute that command I have things nicely formatted now there are many other arguments that I could have used but you get the point right I can provide text with certain stylish features if I need some input like yes or no true false I can execute something like gum I want you to confirm something with the text are you ready and then the output of that can be passed to an end and Echo he's ready pipe pipe Echo he's not ready meaning that the it's either true and then this will be the set of commands and this is just simple echo or the output is false and then it's something else something else is executed so let's see it in action right if I say yes the output is he's ready and if I execute the same command and say no the output is he is not ready in other words I have true false yes no type of prompt and depending on the outcome I can execute one set of commands or another set of commands or maybe exit the script I can also use markdown or I can use markdown format to get nice output so let's say that I want to have a text with some header like you will need following tools installed and then markdown formatted table and if I pipe all that to gum format if you figure it out and format it properly as any other markdown viewer and you can see here that I have a header and I have a table nicely formatted table in the output I can also ask for user input and assign values of that input to variables like in this case hey GitHub org equals the output of the command gum input and with the text GitHub organization I'm asked for some input I type the input and the command exits and now if I output Echo the value of the GitHub org I get that same uh input or output depending on how you look at it or instead of free text input I can give my users some choices and also store it in a variable so let's say hyperscaler equals whatever is the output of the command gum I want you to choose between Google AWS Azure and then user is allowed to choose one of those and the result is stored in yet another variable in this case hyperscaler if we have some processes that take a while to execute we can also give nice indication that something is happening and people should wait so let's say let's say I can execute gum Spin and then choose the type of the spinner in this case line you will see it soon some text or some title and pass the command in this case it's sleep for five seconds but it could be any Command right so instead of me trying to figure out what's going gone as a user of that CLI I see a message that says we're waiting and then there are nice animation over there whichever animation you choose now I do not use those commands that I showed you quite a few others directly instead I put them all into a simple typical shell script so the script the setup script for one of the videos that are coming very very soon is like this and like this means let's output setup.sh and you can see this is normal script except that it has gum commands in it mixed with normal commands that I would normally have in a shell script you can see here I'm using those same commands I showed before there is gum style because I want to start the process the experience with a nice message and then I ask my users to confirm whether they are ready to start or not I show them nice table with all the requirements that are assumed and ask them to confirm again whether they have those tools installed I give them choices between different hyperscalers and so on and so fourth it's absolutely awesome but let's let's talk let's get away from the demo part and talk about charm what are the good things what are the bad things should you use it what's the experience with it so um what can I say about charm gum here it goes it's bloody awesome I love it it's one of the greatest little tiny tools that very few know of actually everything the charm does is awesome and charm gum and what I'm showing today is absolutely brilliant it is small and it does what it's supposed to do there are no cones there are no negative things I can think of if you want to create an interactive CLI from a shell script and this charm gum is based on the same libraries that you can also import into your goal if you want to go beyond shell scripts and choose to uh write the proper CLI and go but until that moment you can use just gum commands directly in your shell scripts so trumpgam is not a replacement of a CLI you would normally write in go or some other real language but if you need interactions and you need formatting and a few other things but you do not want to make it any more complicated than writing a shell script then charm is a great option there are no cons I can think of use it thanks so much for watching see you next time cheers foreign
Info
Channel: DevOps Toolkit
Views: 11,115
Rating: undefined out of 5
Keywords: devops, devops toolkit, review, tutorial, viktor farcic, k8s, kubernetes, charmbracelet, charmgum, cli, bash, shell, zhs, script, scripting, gum, charm
Id: U8zCHA-9VLA
Channel Id: undefined
Length: 14min 45sec (885 seconds)
Published: Mon May 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.