This Makes Golang CLI Development So MUCH Better

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm a big fan of tools written and go that make writing code easier cleaner but more importantly more fun and today I'm going to talk to you guys about a package that makes running your own custom CLI Ino that much better whether you're a beginner or someone more advanced you know that go is great for creating your own custom CA applicational program that you can use for your internal projects or make it available to the entire go ecosystem all over the internet but I'm going to show you a tool that allows you to create a powerful modern CLI and it takes care of a bunch of boiler plate for you all right so the package I'm talking about is Cobra by spf13 it is one of the more popular packages in the go ecosystem if you've ever written a CLI I'm sure that you've come across this it has 33.7 th000 stars go make sure you start that link in the description below and like it says Cobra is used in many go projects as kubernetes Hugo and get help CI to name a few this list contains a more extensive list of projects using Cobra Cobra is a providing a simple interface to create powerful modern CI interface similar to get and go tools now one of the main reasons why I really like Cobra is because it follows the very very good principle of go which is just keep it simple and focus on one thing at a time and Cobra just focuses on how to make your CLI applications that much better Cobra is built on a structure of modern commands arguments and flags commands repres present actions args are things and flags are modifiers for those actions users intuitively know how to interact with them but let's go through an example to Showcase how powerful is Cobra but before we do there is something else that Cobra has that not a lot of people know about which is the Cobra CLI repository it only has 454 stars and it really makes writing Cobra applications that much easier okay so you can see here I have an open Direct directory and by open I mean empty I don't know why I said open but it's empty there's nothing here so first of all let's go do go mod init password gen and now if you flip back over to the Cobra CLI repository just for a second it says once you've created the application you go into you can do Cobra CLI in it so let's go back boom your Cobra application is ready at this location let's go ahead and open vs code all right so you can see here that the Cobra CLI generated a few things first it has a main.go file go Sun a license and a command folder with a root Dogo file with a bunch of stuff here now I'm not going to go into detail of exactly what's going on here but you can see here that the main.go just executes this function and this execute functions runs the root command execute method as it says root command represents the base command when called without any sub commands you can see here that it has the Cobra struck with a used short and long form description and then here one thing is commented out that says uncomment the following line if your bare application has an action associated with it but just to show you how this works you can go back into your terminal you can run go build you can do a go install and you can do password gen bang a longer description that spans multiple lines and quickly contains examples use of your application for example which is taken right here so in the foll I'm going to create a simple password generator that's going to ask to use for three flags the length of the password if want to include numerical values or is digits and is special characters so should we include special characters in the password that we generate for you all right so I have a new file here called password. go it's going to exist in the existing command package but the first thing I'm going to do is create a new command so the name of the command is just going to be generate now one thing we're going to do differently is we're going to actually add a run parameter to our stru so it's going to call generate password this doesn't exist so let's define it this function is going to imp part three flags for us the length those are user want a digit in their password and does the user want special characters in their password okay so first we going to do length CMD do Flags we're going to do get int and then the name is going to be just link and we're going to do that for is digit so is digit ignore the error CMD flags and this is going to be a bu to get bu and last but not least we're going to do is special characters so next the whole logic of this going to be created from something called a character set which right now is going to be just a list of all the letters in the English alphabet we're going to expand the character set so if is digits our character set is going to be expanding nothing too crazy we're going to do same thing for special characters then our character set is going to include all of these special characters so now let's actually generate our password here going to use the make function and next we're going to populate the password all right perfect so again the logic here isn't too complicated so there's one thing left for us to do and that is to actually hook all of this up any command we do we have to attach it to the root command with this boilerplate structure so we're going to do a funk in it and this going to do something very simple we're just to do root command add command and the name of the command which is just generate CMD so it is the Cobra command struct but there's actually something else that we need to discuss these three lines we are parsing the flag but if we try to use our pen generate command and passion the flags up here well it's not going to work let's go ahead and add that so we're going to do generate command and then the type that this flag is going to be so int P the name of it a Shand form so- l a default value not a minimum value and then a small description the same thing but two more times out Flags false by default and include digits in the generated password and then one more time just to really sing at home and this going to be special characters one eternity later all right so let's go ahead and test out our application finally so we going do go build go install password gen again this is the name of the package that I created in the go mod in at the beginning of the video and let's see what happens if I just run it like so and you can see here our password generates a mix of lower case and bigger case so you can see that it works just fine however what if we do the dash flag gener random passwords with customizable options and we did not have to Define this we just have to really put it into Cobra and Cobra takes care of the rest for us okay but now let's generate our password for real so let's do 12 characters we want digits and we want special characters bang okay what if we don't want special characters just want digits perfect and what about if we don't want digits but we just want special characters and something from the alphabet so you can see here that our CI is super responsive bang we get our passwords generated super easily and it's something that makes writing CIS extremely valuable using the Cobra package all right so I hope you all enjoyed this video this is going down into one of those Series where I explain different go packages that you may or may not have heard of let me know what you guys think in the comment section Below have you used Cobra before if so what's something really great about it that you enjoy if not what's something that you learn about it today the code for this will be available on my GitHub I'm typically not a big fan of posting code from these tutorials but a lot of people want to see it so I will do it for you guys but as always make sure you comment click like And subscribe if you haven't we crossed a 10K but we're aiming for higher higher goals and I got to leave you guys off with two things what go package should I cover next do you have any ideas any suggestions let me know and two you got a pallet wow
Info
Channel: Melkey
Views: 21,231
Rating: undefined out of 5
Keywords: video sharing, video, sharing, computer science, software engineer, silicon valley, computer programming, coding, learn to code, machine learning, artificial intelligence, cloud, python, javascript, how to code, Data scientist, AI developer, Machine Learning, golang, go, programming in go, cobra cli, theprimeagen, theo ping gg
Id: yybzcix10XI
Channel Id: undefined
Length: 7min 34sec (454 seconds)
Published: Tue Oct 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.