justforfunc #32: CLI tools with Cobra

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

FWIW, this is Cobra, not CORBA.

/shudders

👍︎︎ 3 👤︎︎ u/ericzhill 📅︎︎ Apr 03 2018 🗫︎ replies

Instead of using init it can be better to use PreRun or PersistentPreRun or the ..E variants to initialize common things. only a little better though because things still end up as global, but you do have more control over execution.

👍︎︎ 1 👤︎︎ u/Justinsaccount 📅︎︎ Apr 04 2018 🗫︎ replies
Captions
hi I'm from system boy and this is just a fun welcome to episode number 32 of Joseph funk today we're going to modify the previous code that command-line tool that we developed to manage tasks to use Cobra lots of people asked about this so since I like to please I decided to go with that so what we're gonna do is get the code that we had before and just let it there I'm gonna create a new directory that is going to be using again the G RPC client that we use before and if you don't know what that is do not worry it just looks like calling a function from here so that's fine but if you want to know more you should watch the episode from two weeks ago and then we're gonna do is just provide a more organized way of organizing your source code and Cobra is really good for that it forces you to follow some structure which is sometimes you really don't want to follow it can be a little bit painful but when you know when you're not constraining you can do whatever you want cover is a great project to write small tools and even large ones darker or kubernetes all of like if you do cube cattle die a command-line some people pronounce that cube CTL which is wrong but if you run cube cattle you will see that that is actually using Cobra two so you're gonna get to learn how to use the thing dad docker in kubernetes and all of these cool kids use so that's got some fun let's get started so let's talk first a little bit about Cobra Cobra is according to documentation which you can find on github and there will be a link in the description so go check it out according to documentation from Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications in a common in command files which is an interesting description is pre accurate the only thing that I care about is the fact that it uses two different factors right just like two different things according to these a library and also a program I prefer to call them like a program or a tool sure whatever a binary that you're gonna be running and also you're gonna have a framework it's more of a framework than a library I'd say just because basically when you call the binary that will create a skeleton and then you're gonna be adding code in between which is more what you do with a framework than a library but you know the the line is somehow blurred around those edges so let's say the library or framework whatever would you prefer Alcala framework there's two things in order to install it is as simple as doing go get because it's a go program so of course it's go gettable Steve is a good gopher so of course he does the firm's go gettable the cool thing is that while we're gonna be using it for a very small example just to do app with just a couple of super commands some other large tools use it so here we have a list of kubernetes and hugo but also docker what else cocoa TV and go for Jas and some others that you might know the cool thing is that this is actually very simple way to get started and to ride nice tools forces it to crank to keep your code quite clean so it's a good option is it better than writing everything by yourself I'd say that yes it is because most of the time when you're running a command line there's more interesting things than figuring out how to organize your code or how to parse flags and that's what Cobra does for you so that's enough description let's get started and let's get let's get using it okay so the first thing you're going to do is to run Cobra in it and when you do Co brain it is going to initialize the project create all the basic skeleton were you going to be at when you're going to be adding your code now you when you do that it will create a main door go entry point for the program and it will create it exactly where you are when you run Cobra in it which is not what I want here because then I will get a binary name 32 - Cobra which is not necessarily a very good name so instead I'm going to create a new directory going set of it and then to Cobra in it now your core application is ready cool and you can run it so you can do simply go go run main the go and look at this it actually runs and it shows things that's cool we didn't do much and already we got all of this right so you create a main the curve license in a route door go inside of the command directory the main dagger function doesn't do much and that's a feature everything it does it just calls the execute so executed function in the command package and the client package is defined here there's only one file so far bruiser go and it contains the route command and the way it works is that basically a command can have as many sub commands as you want and those sub commands can have soup commands on it at its at own time right so it is can Akoo there's a lot of things that the the route command does it manages for instance all the power of configuration and stuff like that in this episode to keep it short I will not be talking about configuration at least not today not right now so I'm just going to ignore all of this code I will leave it there but basically it's a nice way of if you want to add things like an environment variable to decide where the database is stored or whatever things like this it's a nice way to do it will not talk about that today if you want me to talk about another day you know what to do leave a comment send me an email send me a message on Twitter or whatever you know how to reach me anyway the important thing is that execute that function that we're coming from the main function calls the execute method of the root command and brute command is a global variable that package variable if you wish that is of type pointer to Cobra command so what it has is the use which is the name of the command itself is the route we're gonna call it just to do a description of what this does so to do is let's call it a blog chain power to do manager task manager now of course this is false but muscle people that say they're working on blockchain they're also lying so who cares so now we can add a longer description to do is the most awesome project you ever see and just to show and you can say that in multiple lines yay okay then if we want the root to do something then we can add things we can add this run function it is not the case our when we one when we do to do you should not do anything you should ask you what do you want to do right so which is going to leave it like this you can ignore everything down here just ignore it everything we're doing is calling execute in the root command cool so when they do going so that generates it to do binary and now when I run it it says to do is the most awesome project you'll ever see that a pen because finally these decided that a pen was supposed to be added at the end there sure let's go install and to do okay and you can say that in multiple lines yay so that's that's pretty useful that's pretty much we want to do now this is cool but what about we want to add more commands so you could write all of the code for you but that Canada fits the purpose of Cobra with Cobra what you can do is simply do Cobra add and you can do Cobra help add to show you all the things that you can do but add Cobra add will create a new command with a license on the appropriate structure for a Cobra base CLI application and register it to its parent default command root command so when you do so let's say that you want to list all of the other tasks right so we're gonna call it list so what you can do is Cobra add and then the name of the command list now when they do this without modifying anything else if I do go install and run to do again you will see that now we have extra things and this starts to get interesting because I love sign it says the description that we gave before sure but also the usage it's to do in a command those command that the command can be either can either be help or list and then there is some flags - the for toggle which is help message for to only health for it to do those flags are defined here and we don't really care about them that much so I'm just going to comment this out you why not so I'm going to command these communities you know what I'm going to delete all that that way it's less confusing there's no none of that code matters we just have a root command and now when we did Cobra add you will see that there's an e list this list command also comes with the description so lists all of the tasks and I'm not going to provide a long description so when you run it if he needs a long description it will just get the short one which is good and then what we're going to do here is just print list call now the interesting thing is this part here root command is the command that we define before and we are calling add command and adding a new command right so this is what is building that tree of commands of commands of command so command etc that's that function call right there that we do it in it we might do some flag later but for now let's just remove all of this okay so so right now as you can see everything we have is we just defined the list command we say that whenever we call it going to do a list called and we added to the route and in the init function so let's go stop and then to do to do what to do list and to do list says to do list called cool what about to do help you print the same thing as before but you can also do to do help list and it prints the help specifically for the lists of command which is really cool this is where if you have provided a long description it would appear instead of a short one which is pretty nice and also the specific flag for this command which there's not there's nothing so just - H cool let's leave it as is for now and add just a couple more commands so we're gonna do Cobra add so we have list we want to have a new one for new and one for creating it and you create it so now again I'm not gonna change anything and just to do and now get help listen ooh this is really cool so now let's go and modify that new go so creates a new task with the given text going to drop of this a new coal and then I'm going to drop all of this job so again we're just going to the bare minimum so going so to do so help help about any command lists list all the tasks I don't like that this one doesn't have a dot at the end and the others ooh so let's fix that this is the kind of thing that you know I've been very confused in the past so that's why I pay attention to little things like that okay so going so to do to help list and to do help new and all of this is just there without having to really create much so that's cool okay so what I'm going to do is I'm going to now migrate the code that we had before for episode 31 the one talking about G RPC and my grade that into these new program so without thinking too much about it and just going to copy paste the code so this is the part right the ad and list which do the things we want to do the rest is all about deciding the like passing flags connecting to stuff and things like this which we don't really care about that much because we're using Cobra and it will manage it by itself so let's start with lists and simply move that to list or go so I'm going to add a here at the end and what we want to do is call this so list context context the bag brown let's let's pass in a client we're gonna need also a client the client can be created with this okay so we pass the client and that returns an error so we want to do is really return that error but when you return an error here these will not be happy because we never saw that we're returning errors and by the way here I like to instead return an error here too okay so we need these to return an error but of course nowadays not happy because run it's supposed to be a function that doesn't return an error luckily for us there's one e that is exactly a function that that's the same thing but just handles the error return which is pretty cool G RPC is not defined that's a problem also we're gonna need that to do package there okay so now all of this is pretty happy so let's go here okay so I'm going to start running the server that we use the last time exactly the same so I'm going to do go install of command server and then run server and that should listen on on something you know remember what port eight eight eight eight many many eighths Baba Baba I'm learning Chinese okay so that's running and so if we do going stop and we do it to do list there you go we got them that's that's awesome right now we're able to see the list that we have before what about add so we're gonna do something similar go to the client and get add and so simply added to add so two new so add client out add to do text text text text that looks pretty good that look that looks pretty good we just need a client again so that's gonna be the same colors in here we're gonna move it here that's gonna return an error it's gonna be run E and we're going to call add context up background what else the client and the text and the text is the result of concatenating all of the arguments we're passing so that can't be strings dot join arts with the space okay will this work that's right so going stop to do list to do new and it just a funk task and it successfully to the list cool we have it right there perfect okay okay one last thing that I'm not very happy with this for instance the fact that here we're repeating this piece of over and over something that I think might be nicer to do is to do it and in it on route so when do when we do route here when we do in it since this was not doing anything else anymore we can do this thing client and have a variable client which is to do client and Nita's tasks clients or tasks climb tasks client and if that fails then we're gonna do we're gonna go back to what we had before or f OS the air and OS x 0 1 so this what we had before f print f okay and client it's to do cannot use test starts this line okay this is not a pointer there's an interface i keep on forgetting that every single time okay but now we know that if we get in here at all we know it is because we have a client so we do not need to pass this client anymore instead that client will be the global variable so list we're gonna do the same we're gonna just remove all of this and we're not going to pass the client anymore we're going to pass the client here work instead we're just going to use it directly so let's see that works so going so and then to do list and to add sleep a little bit maybe a little bit maybe to do new to the new oh and now if we do to the list sleep a little bit maybe perfect so all of these appears I'm it's pretty nice at some point we should add the fact that we can do a task right now we're just having had more and more tasks and we never delete them and we never finish them which is you know accurate had this one of my life but not necessarily what we want to do with that with the task manager so to finish let's add a flag just to show you how it works and we're going to add a flag the specific sub command so in this case let's think we we can do it with lists let's say that we want to filter the tasks that are already done in our case we don't have any down tasks so that that is kind of a silly test but you know why not so how do we do that well we are going to go back here and when we do in the route command dot add we can also do before that I like it before list command dodd flags dodd add flag so boo-boo people's lies boo P and I will give a name so I'm not very good with with long names done only D the valley is gonna be false by default and these will do show only tasks that are hmm to do only okay these show only times that are not completed yet okay so by just doing this now what we can do is go here and let's say that we're going to pass that as a as a verbal here filter filtered on which is boolean and we're gonna say that if it's done if a filter done is true then we're going to continue I'm just gonna go to next desk so that should work yeah that should work and now what value do we pass there well we simply going to command flags get bull to do only and that actually returns an error too so let's move here so filter down and error if the air is the nail we're gonna return the air otherwise we're going to pass filter Dom okay so let's see if these did anything interesting so when they do go install again if they do to do this doesn't show any extra flags here but that's normal because the flag was not added to the route command or to it's not a persistent flag that you can use everywhere it's flag specifically for sub command so we do to-do list this list of that but if you do too help list now you can see that there's a new flag and these new flag has two forms - t so to do - the list to do lace - t sorry and or - - to do only and they're both exactly the same and these how linux commands really work right like you have there one - and one letter or two dashes in something longer so for instance with game here you have - p or - - paginate they're both exactly the same thing right what's the difference well one is sure that one's longer so when now you do - do - do help of lists you see that you have - H or - - help in - tea or - - to do only which is very cool so with that I think that we've basically shown the basics of cobra how to add commands how to share information across multiple commands basically move it to route and pass parameters and also how to figure out how to use flags we've seen both flags that were at the command at the root level which is that toggle that we have before but oh and that was a persistent flag so you can use anywhere but also you can be fine flags at a command level and you do that directly from this command and then you do command flag get bull and that's pretty much it so with these we got to the end of the episode I hope you enjoyed it but before you go I wanted to remind you of a couple things one of them is thanks to all the people that are donating on patreon I'm able to increase a little bit the quality of videos since I left Google my budget is different as you can imagine so talking about cheap budget this like super cheap super cheap mirror but I hope that you'll be able to see and I'm gonna try to do some video editing there so here you can see the two big lights that I got which make me look much better I think so thanks so much to all of you I'm still recording with the phone soon I'll try to get a camera but that's next also I wanted to remind you that you can give me all the proposals or semi doubts or whatever that you might want to do on for Joseph uncom also just I read all the comments down here on YouTube the good ones and the bad ones so you know I'll read them so leave a comment if you want and they want to tell me anything also you can find me on Twitter as France asked and let that but not least I wanted to leave you with a question so we've learned how to do CLI tools cool but what does the I stand for in CLI right so with that an IgG Matic question this is the end of the episode thanks so much for watching please subscribe tell all of your friends and see you all in two weeks [Music]
Info
Channel: justforfunc: Programming in Go
Views: 29,977
Rating: undefined out of 5
Keywords: golang, go, justforfunc, programming, CLI, command line interface, cobra
Id: WvWPGVKLvR4
Channel Id: undefined
Length: 27min 8sec (1628 seconds)
Published: Tue Apr 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.