This is Why You NEED to Use This Golang CLI Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you a tool that you absolutely must need to use if you're interested in building goang CLI applications ever since I released go blueprint everybody's been telling me I did such a good job of making these CLI look great Q go blueprint right here somewhere you'll see on the screen CI there's colors it's it's nice there's options too but I really want to showcase the tools that are open source and are free that really help make go blueprint look so good and be as successful as it is right now so the very first tool I need you guys to look at is is by charm the tool is called Bubble te and as it says the fun function and stap way to build terminal apps a go framework based on the elm architecture bubble is well suited for simple and complex term applications either inline full window or a mix of both but let me dive deeper into what this offers it's a framework it's a framework that gives you the ability to do whatever it is you want with their model structure you can see here I'm going through so many different examples you can put a credit card form there is this composable views I'm not even sure what that means a debound EXA Go full screen glamour this looks beautiful like look glamour it up bang look at that but you can even go more and reuse the model structure they provide and the methods built into the framework to do whatever it is you really want to do but the main kind of selling point of bubble te the framework is it provides you this model structure that you create this text input model and there's different models for everything you want to build it could be text input and Multiple List exiting a fancy list charm will provide those for you in their bubbles package and then you use the T from bubble T framework to actually instantiate and pass those Frameworks through your application with some very important methods like the update method and the view method and of course the init method and another really good functional tool that goes so well with bubble te is lip gloss lip gloss as they say style definitions for nice terminal layouts built with Tui in mind essentially the way I think of this is like the CSS for your CI you know you can create a clii using bubble T that is super functional there some really cool stuff but you want it to pop you want to give it that Pizzaz and lip gloss is the tool to do it for you let me quickly Show an example of what you can create using bubble tea lip gloss and all the other fundamental tools here we'll have go blueprint create and bang there you go you can put some askr It'll ask what's the name of your project you know you can say hi YouTube That's the name of the project you can pick your library whatever but the Highlight is the functionality here the different colors the color pattern I'm using the Bolding of some the list I choose here for example we'll use Chi for for this framework we'll go to Next Step you know what database driver you want use again it doesn't matter we'll just say post we'll press y to confirm and there you go now we'll have the actual project but this is what you can build using these tools so let's actually build something and let me show you can get started now I want to preface this that we're not going to be writing every line of code line by line this is going to be more so an introductory of how you can copy and paste to get started in writing functional things for your program and what you can couple bubble tea and lip gloss with Cobra to really start making some really cool applic appc you can contribute to or make your own and you know put them open source or close Source whatever it could be that you want to do so here I have an open director there's nothing in here you can LS just a go mod cuz I a go mod in it but what we're going to actually do is use Cobra CLI in it and I've made a whole video on COBRA before on my YouTube channel editor please put my YouTube channel like you scroll through it this is like all my videos you know watch them they're good I think they're good I think I make good videos but what do you think I make good videos I mean you have to say yes I mean we'll go back here we'll just do Cobra C in it and your Cobra application is ready at this location now if we do a code Dot and open up our Visual Studio code you can see here that no longer do we just have that go dood but we actually have a bunch of stuff here like this root. go uh that really will kick off our application but now we can get started by installing uh bubble te and installing charm as well all right so the very first thing that I want to do is actually add a new file called create. go and we going to put this in the package CMD now to get started we're going to pull in our first import which is github.com spf13 Cobra we can do a go mod TI it will install it for us and then we're going to actually structure the project Okay cool so this is what Cobra expects the very bare bone of your inflamation to look like and now we're going to really add a lot more to this but first let's actually install lip gloss and create some variables for lip gloss to kind of highlight over our title the things that look kind of nice so if you go flip back here this is kind of like how we're going to Define these Styles right here okay cool so there's some Styles I put for logo sty tip as message style and ending message style this is just you know pretty bare bone lip gloss imp implementation the only thing I changed with the color here to match my favorite color which is that teal that you saw the first thing I want to do is actually do an a function in it which is going to bind our Command to the existing Cobra command here so you do root CMD and this is defined in the rot. gofile when you did Cobra CLI in it you do add command and you're going to put in the variable they declared over on line three or relative line three line 18 of create command now every time you do Cobra or or the name of your application- create it will run whatever is defined in this function all right and uh let's go ahead and create two types we're going to make one type which going to be our list of options uh this is going to be you know nothing too fancy this the way I like to write my go project it's going to be an item of options that are just a slice of strings and we're going to do another one which is just project options which also going to be a struct and it's going to contain the project name we're going to put that as a string for now we're going to change that just a bit and project type which would also be a string now these are just going to be Place holders because now we're going to actually bring bring in bubble T and create two separate folders for our text input and the multi- input that we're going to implement let's go ahead and make a new folder called UI make another folder called text input we going to make another one called multi-input and in each of them we're going to make a multi-input dogo go in there package multiinput perfect and then go back create a new one called text input. go and create a new file called package text input so if we quickly flip back over to our examples we're essentially just going to be copy and past pasting the example here for text input and again for multi- input we're going to do some slight modifications I'll talk you through I just don't want to write the entire code line by line uh you'll have this code available for you on GitHub I think it's more important to understand the concepts that this framework offers as opposed to kind of just watching me type it out and then you kind of regurgitating it I think it's easier if you just kind of look at it have a place to start understand how it conceptually works and then you can do your own thing all right so I'm back and I have written out the code I mostly copy and paste it you can see it's very similar I did add a lip gloss style here again this is just lip gloss I'm kind of just adding what I want with the color you can modify this you can look at lip gloss documentation of what you want to add but I highly recommend you use lip gloss I've have a few types here that Define for the error message the output uh but really the main thing you want to focus on and you can you can see I like my init method my update method here I have my view method you can even go back you can view them here the main thing that you want to focus on is defined here online 52 which is what this is accepting which is the t. model struct and that is what's going to hold hold kind of the logic surrounding any sort of thing that you use from Bubble te in the framework uh and what these methods do essentially is this initial text input model or you know whatever you call this is the Constructor of your instantiation of the framework for this component so for here this is text input I instantiate it it's my Constructor and it returns this model struct which typically takes in a text input error output header this give you whatever you want but what's most important is to focus on this text input SL model this is imported from charm bracelet bubbles textt input package now this charm bracelet bubbles package contains all of these methods for all of the different examples I showed earlier at the beginning of the video where we went through the example to Bubble te showcasing so the construct just Returns the model takes in some parameters here like you can instantiate a text input. new telling it you know this is going to be a text input type functionality you can add Focus character limit the width and then you're going to put in and return new model here next you're going to have the init method this is just basically a simple method that t expects to be begin the functionality of the model or of the text input in this case and then we have the update and view methods Now update is basically a real time call back that updates the value you're doing so because it's text input every time I type on my keyboard what I want to provide my CLI it's going to take that and update the method that we see and the view method this is the most interesting one this is basically the renderer of what we're using and what we're seeing this renders out the logic from the text input component from the bubble te framework into something that we can visually see on our CLI so these are the key methods for any sort anything you want to implement now how do you implement this back into the root of our application how do we then see it and add it into our function here well this is where the project name comes into play because again this is just a placeholder you can name it whatever you want however it does have to have a specific type it doesn't it can't just be a string type now we're going to actually modify this by bringing in our text input package that we just worked on into our create. go and our CMD package we go here and type in charm SL the name of the project or what's you define as a go mod in it and it's in CMD SL ui/ text input you can see it's grade out so it's imported but just not used and then here in our project type name the thing that we need to focus on is the initialization function okay so now I'm going to talk how you can actually bring in our text input package and use it and you can actually see when you run this program so the very first thing you need to do is actually import your package so I called my go mod charm CLI it's going to be under CMD ui/ text input you can see there it's grayed out and then all I need to do is basically follow what the initialization function here what my Constructor is expecting essentially it's really expecting just a type output struct and a string as the header value so here instead of keeping this as a string like I said it is a placehold holder value what you can actually do is put text input input and don't forget we do want this to be a pointer reference because we are going to modify this value we're not going to just pass in uh you know just a copy value like go does and so here next thing we're going to do is just call the initialization function and so now the next thing is we're actually going to declare an options and going to be declaring as an option struct I'm going to instantiate this and just put project name is text input output there you go and now it's going to complaint it's not being used but we'll take care of that just in a second let me just add some more space here so we're going to declare something called T program and this going to be from the bubble T package t. new program and now this is where you pass in your Constructor method so you're going to do text input and there you go new initial text input model it's going to expect options. project name and then the header this could be you know what is the name of your project boom there you go it's going to complain that t program is not used so then you're going to call run and handle the error from T program so now we have everything to work for our text input method and now I'm going to show you how it works to go ahead and open a terminal do this the first thing that you want to do is actually build this you can do a go build and then do a go and you can see here there's actually a binary file now this is going to bind it to your go path and I think that's the coolest part about CLR it's always going to need a prefix something that your CL recognizes to run the command or add Flags to the initial command and just to give some feedback if you don't know how to actually run this look at your root. go when you ran Cobra CLI in it whatever command is here is going to be that initial kind of path binding variable to your go path and in the create here is going to be the command so it's going to be create now I know it's going to be charm CLI create and what is the name of your project and here you go it works how I expect it to hi Mom okay now we're almost done but I do really want to showcase the list functionality here as well again you can see go Blue we are very heavily involved in the list I mean I think it's a great feature I mean let's just make everything better and again we're just going to follow the documentation here in the example I think the example from charm on list is actually a little bit over complicated it does again like I said it has the update method the init method The View method and the Constructor method as well but I just think they could have done a bit more a simplistic perspective on it so I'm just going to model it more so towards a text input Constructor that we just built all right I just pasted a ton of code it should look fairly similar cuz again we're using it the same thing that we did in our text input method here it just looks a little different cuz we're handling a list and we're handling options and we want to make sure we render the right thing when the user chooses something so you can see here we still have that same model we have a few things added like the cursor a list of choices this is something you're going to feed in to our initializer uh the selected which is going to be choosing what option that you select a selection choice and a header which is just a string uh now one thing to notice is the selection struct is just a string it's just something that contains a choice which a string and it just has update method which is kind of similar to what's going on here we have an output struct with an output method and just updates the value depending on what your user does so if you're texting you're typing if you're choosing you're selecting now the update method may look a little different because there's a few things that we kind of have to attribute to what the user may do if they choose to go up down enter or select then we want to handle those cases you know if we go up we want to move our cursor up a selection down down the selection if we click enter they want to go through and if we click y we want to make sure that our we processed what our user has selected and we make sure we keep that state for us to use throughout our application and the last is that view again this is just a render method this is what our user users will see but it's important to know how we want to select and how we want to change the option of what our users see so for example here if our cursor is on our option then we're adding this little arrow here on the box option for our simple list and if they check it then we know we want to put an X indicating that they've made their selection on the list that we provide them all right so let's head on over back to our create. go we're almost done but now we're going to actually populate our list options struct and pass it down to our T program I'm just going to make some new lines over here and give myself some space remove that all right going do let's say list of stuff I don't know I'm bad with variable names but you shouldn't be spend some time on it list options and then options going to be a slice of string and we going to put I don't know uh option one subscribe to milky don't be cool perfect perfect perfect all right so now that we've declared our list of stuff it's basically the exact same thing here so we going to put T program we don't have to redeclare it it's be T new program and going to put in our multi input initial modal multi and here we're going to put in the you know you can see there are choices which going to be list of stuff. options we going put our options do project type and then just any kind of you know string here we'll just put options and then the same thing we'll do some error handling let's just take this and paste it here all right so now that we have all that written let's go ahead and run our application now you have to remember you have to rebuild the binder so go build then install that to bind the latest version to our path and do charm CLI create so what is the name of your project I YouTube and then options option one subscribe to mky or don't be cool obviously you want to subscribe to mky hope you all enjoyed this video I know I didn't go into the detail of how I write every single line of code but I more so think it's important to focus on the concepts of what our framework could do you could always watch someone write code and just copy and paste it make sure to comment like And subscribe if you want to see more but I got to leave you off things I don't even know what the first one is go build something cool using bubble tea and lip gloss and Cobra let me know what it is if it's cool enough I'll make a video about it but if not you got to power [Music] it
Info
Channel: Melkey
Views: 22,138
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, golang tutorial, theprimeagen, theopinggg
Id: ncakAFWxIys
Channel Id: undefined
Length: 15min 21sec (921 seconds)
Published: Tue Dec 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.