Golang Command Line Interface (CLI) Pt.1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel today we're looking at golang CLI so the first thing we want to do is get set up with a directory to do this go into your go source Gabe comm folder make a project directory and then open that up with a IDE I have chosen Visual Studio code just because there's a really good go extension that integrates really well that once inside you're going to want to open up a new terminal this is so we can do a go mod in its we're going to be using go version one 14.2 in this tutorial so go modules is already sort of baked into that after that you just want to start like writing a CMD folder so we're gonna use like a - a - folder top level directory in this project and inside CMD you just create a main go from here we're just gonna add the package main and to start with we're just gonna do a simple hello world program which is really simple just do a main function and an FM team print line and to run this you just want to do go run CMD slash mean don't go we're just gonna run the main main go far here and it'll print out hello world for you so that's pretty boring so we're now going to start with a well start creating the CLI and so to do this we're going to assign app to CLI dot apps that are going to initialize the CLI itself don't worry about imports just yet if you have the right extension it will automatically do that as well along with go mod when you build when you build the function when you build the file so we're going to pop in name usage and an action so this is just us constructing the CLI itself I'm just going to do a simple print line inside the action just to care something out for those of you who are new to go the initializing the error and the line above this just here why as knowledge sizing is the return value so in go you can have multiple return values from a function in this instance we're just going to return an error if there is an error obviously in our case we're just going to return nil as that and of course we need our leading comments once we've done that we're just going to sort of set up error so error or error is equal to AB don't run so if there's no error then it will never sort of exit it's always going to continuously run yeah obviously if it does fail then we're gonna put a fatal and I did a control us command s and as you can see the imports just automatically updated themselves I don't know I do go tidy there okay I'm on tidy so we're going to do run it again and you'll see that it outputs from the command line instead now so we've got a different different print message you can see if I go over to the go mod file you'll see that it automatically imported your fav slash CLI and the go some file little the imports and that also relies on so that's quite a handy thing whenever you're building your applications that it will always pull in what it can so I'm just gonna do a go run CMD main it'll go - or help and that she's going to pull up the the main CLI itself so you got the name the usage and the commands obviously if I run the hello command then it will automatically print out what was was in the CLI but this is quite cool but we don't necessarily want it to just run hello this is just a very static function that we've got so we want to create a package or PKG folder and we want to create another folder in there called actions and under actions commands don't go so this would be our main folder for the commands that we want in our CLI so what we can do is we can pull all of this out of the main dog or the main function and put this into our new command store go so we're going to want so Micah and now maybe a status command or something so just going to create our main commands function that may not go or so the main function is going to cool and want to put all this code inside of it as you can see it's done the imports automatically do I let al into air oh that's just my lint are saying that they sake this this function is an exported function because it's got capital that's what the beginning so therefore you must comment it now if we look in the main function we're just going to want to import our or want to call the function that's in our new package actions as you can see just a command save that automatically pull it in once typing or once then putting the the function I'm just going to give it another run one more time and then we can just see that our code is still compiling it's still printing out the exact same as before never type help again then you can see that's yeah you can see it's still working so let's go ahead and change the name of the CLI because we don't want one top-level command to just run all the time we want multiple so we're going to name the CLI give it a few sort of attributes and then get rid of the action function because when we run this we don't want it to perform an action we want it to display our commands and some commands or whatever else so let's do them out we just sort of underneath here we're then going to want to add a command an array of cons and so to do this we to do a pokémon's is equal to CLI command now within this we want to create essentially the objects of commands so each command is going to be as own sort of struct or structure so it's going to give give it a name so I'm just going to say status and we're gonna give it a usage and then obviously after that we're gonna give it an action to perform when we cool this command so that's probably as values in and all this action is going to do is again we're just going to do a print line a simple print line just to show that it works and we can see our flow inside the action obviously want to pass through context which I kind of skipped over earlier and all this does is just making sure that these pointers pointing to a memory addresses in golang to say that this is the CLI that we want and it's all going to be sort of followed through all the way through the commands it's not going to be sort of mix and match in golang frames of memory as immutable anyway so you can't change them which kind of gets a bit messy later on but we'll cover that a different video of course we don't mind if you are leading comas and we need a return body which I'm just going to put in now it's got to do that means what to return now again it's the error so Lakoff then what we do is just gonna do we're gonna run the command line itself so we just run help that as you can see we've got very status command so now if I run the status command we gonna get a output saying the statement CLI is true we just put that in our command so that's what we're getting out and there it is this the status command so we've gone from a top level CLI to performing an action straightaway to now having commands within our CLI that we can call the side actions we want to create a status go file because we don't always our commands but go wants to call out to a different you know we want to do all the function and set up elsewhere so we want to create another function file and we're just gonna call it stairs command and I'm gonna take all of this code I say oh let's go two lines of code one line of code and dump it in here and you can print this out and call the function from within our action so this keeps the commands look go file slim because if you have many many commands they can start to get really messy so it's just sort of adding a layer of abstraction again the linters just moaning so add in a comment cool so let's run that one and see what comes out let's see what the status command and if we run it I'll say again so we've got the print line which is cool so that's working the flow-through is now is fine so that's really cool but what if we want to add flags and we wanted to add sort of different parts to this status command so let's just go in and add a sellout up flag and within these flags did this library has got different types of flags we're just going to pop it in a string flag and we're just going to give it a name and under usage just as we did before there's a very same concept we can have sub commands but it's pretty much the same as a command so there'd be no point in really showing you that potentially an alias I'm not sure if that's in the struct of the CLI command don't think it is no no it's not there ever that we can go back this one it'd be a really good way to shorten down our our flag now it's not in that so now we've got a flag we want to pass through the context so that we have access to everything that's happening when we type in our command into the command line interface so as you can see there you know we've typed this out we've now got the status help and you can see the flag test ring so every one of us you context and then obviously it's only failing because it's expecting nothing to come through to this function so we just pop it in here again we're passing through the context so it keeps in line with everything else what we're going to do is we're going to grab the flag so any value that we put through on this flag we're just going to add it in here and pull it out pull that bit of data out of the command sophia's pop it in here and print line and voila so if I just type in status yep put those hyphens in and now we can see that it's printed out hello because I gave it hello so now it's sort of pulled it out and now we've got our CLI built we can now go and build a binary from this they can be built for all platforms so I'm going to build one for Mac OS so Darwin amd64 I'll call it test see a tie I can just see there I seen the current directory and by doing dot slash tests Li you can see it's running it is a built binary now I go make one for Windows but if I ought to do is change the operating system to Windows and a DXE at the end this is how you make binaries for different platforms I'm not doing the go install because I just want to keep this relative to this path going store would put it in a bin directory and that is then accessible from anywhere on your machine if your well your slash bin file should be on your path
Info
Channel: LiamCodes
Views: 1,412
Rating: undefined out of 5
Keywords:
Id: 7qKXxfs7LVY
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Sun May 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.