From Bash to Golang: Elevate Your Terminal Experience with Tview

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
dot go a 64 gigabytes get available memory we're sys admining it up let's freaking go too high too high for a basic system admin app so as a Dev and then on top of that as a Dev that uses neovim I spend even more time in the terminal and on top of that as a Dev who uses neovim and also specializes in the cloud and devopsy space I mean I'm basically I'm in a terminal like all the time now as a consequence of this I end up interfacing with some sort of Linux C shell whether it's on my Mac or on a Linux box or whatever extremely often because I mean that's how you interface with the terminal in the first place so as I'm interfacing with the terminal I kind of have developed this feel for hey if I'm just gonna write a little bash script that's just a few lines with a few commands and I and I kind of have this feel for when that becomes a little too many commands right like if I if I need some features from a general programming language like my Pro programming language of choice which would be golang I think we all kind of do this as devs like especially if you even use bash at all there's a lot of people who don't really use bash but if you're in the devops in Cloud space you you have to touch it at some point like if you're deploying machines and stuff like that but we all kind of try to avoid it at a certain point because there are multiple reasons why bash kind of sucks I mean stick with me because obviously this is about bash right and I ended up writing bash so stick with me but we all understand bash sucks because you know it has a lot less modern programming kind of constructs that we're used to uh error handling is kind of meh the syntax is kind of meh like anytime you kind of get to the point where you need to start writing functions you probably should just use another language this video is about having a good time and learning some things in my other video I created this graphical user interface basic like I mean that's it's really aggressive to say graphical user interface but it is technically that in bash in in my show it was really fun it was a cool experience so I was curious what it would be like to create this same thing but in a better programming language like go and what would be the differences like what does it actually look like is it more lines of code less lines of code all kinds of stuff so at the end we're going to actually grade these things side by side and we're going to do that based on a few basic criteria one I mean the obvious we're just gonna see which looks better the in line with the whole saying I don't know if you all heard it before Beauty works better I like things that look good I like things that look good so we're gonna do that the next one we're going to gauge is just kind of time it took to write it how long was it how long did it take to write it what were the actual lines of code difference for similar functionality and then we have a few things like around the scalability thing I want to think about how would if I was doing this on a team how easy would it be to add features because that's a big problem with like these long bash scripts that you'll find in these old smelly organizations it's yeah it gets gross really fast and it's really hard to maintain which is why you go to another program language amongst a bunch of other reasons so yeah if you stay to the end we'll talk about some of those things but for right now let's go ahead and jump into it all right so for those of you that didn't see the last video on actually creating this basic GUI with bash this is it I just have a few functions that do some basic stuff like get some info on our disk understand how much memory our system has and what user is currently logged in with some basic Echo statements that display our menu read user input and then we have this case statement down here which actually evaluates all of our potential responses throw that in a loop you know continuously read that and now we have the semblance of a graphical user interface let's go ahead and run that real quick I can just do the slash menu and you see it pops up here cool I can hit two to see my available memory pops up there on top left great 64 gigs awesome hit enter to continue and I can keep kind of going through this and press zero to exit cool what I want to do is write a similar program in golang so when I was looking around I saw this t view go length thing let's throw that up for you real quick goal line situation here which is basically a similar type of thing except way more advanced than my bash script as far as capabilities go but allows me to kind of use these components to build these terminal based user interfaces these graphical user interfaces in our terminal this is what we want to do so I was like hey you know we'll try it out I it's funny this was definitely a lot more involved and I think it's it states kind of the point of when you use bash scripts because it was just really simple to do what I needed to do in the moment it just very quickly becomes annoying as you have to build out a bash script but this and like with a lot of stuff like this it kind of gives you a ton up front even though you maybe only need a little piece of it so let's go ahead and to jump into riding this and then we'll test it out and show you what's up all right so first thing that I want to do is I'm just going to make a directory here we'll just call it CLI again CD into CLI then I'm going to do a little initialization here with go mod init and then do GitHub slash is it called t view do that thing and then we'll create our main.go awesome hit it with the package main main lead up on this is a thing do a test go run it I go uh the next thing that we really have to do here is import our t view situation so let's go ahead and do that you can do that with go get looking at the link here github.com Revo slash TV that thing fantastic and then we're going to open back up our main.go now we can start actually messing with our TV situation this is where star to get fun first thing I'm going to do is I'm going to create and initialize my TV app just by doing app assigning that to tview dot new application all right that should import to view Suite then I can do a bunch of stuff here but what what I kind of isolated as I was going through this was they have this list capability that's really good that I think will serve our purposes because again our goal is hey we're taking a look at this from assist admin Point View I want to see how much memory is available I want to see how much disk space I have available and what users currently logged in and doing the thing so tview has this list element in the wiki you can see list here this is kind of what we want I want to be able to choose some options right and then do something based on those options and we have an example here that we can do so I think we should take this and then start iterating on that all right you know a little copy paste but now we have to figure out what it is we actually want here right because we need to fix this so the first item we can have this be kind of a placeholder I guess so just do like uh like tell the people what they need to do select an item from below type of thing do a little ellipses there and then I'm Gonna Leave the explanatory text done and I'm gonna change this to M as kind of like a menu I still want an a though so I'll copy that throw the a in there and then we'll say uh get available memory we'll do that as an option as a and then I'll leave the explanatory text link again for now so we want to get available memory we want to get available disk space so I want to get available memory get available disk space and then the last thing I think I wanted was to just get the current and golang has different ways of doing this than the simple commands in bash so we'll have to figure out what the go of way of doing these things are which there's should be standard libraries for that stuff and then we have this quit down here which I like and we don't really need this this four item should be able to delete that now this is interesting this add item if I go on that it says okay yep accepts those things add item calls insert item with an index of negative one I remember reading I think that's the last thing like it calls add item and it adds it basically just appends it to the end of the list and it looks like what's nil for all of these other AD items this one actually has the Callback function here and has given us an example of stopping the application which is what we need for that quit functionality anyway so that looks good let's go ahead and run it and test it you go run main.go this looks pretty good now if I start selecting stuff like a b c who quits out that makes sense so now we have to actually do some stuff so as I go along I kind of just want to you know make my code a little cleaner this is essentially our options menu so I want to to take that and put that in its own function so let's just throw that in a function up here we'll say function I don't need function let's just call it menu I guess keep it simple and then I'll paste that in there list here and then we'll return the list yes I could do that but then again I need the app I should probably take in the app or I can make it Global I mean we'll probably we can just take it in it's fine but we also need to do a return here so let's grab that return to tv.list might not even need to return it but we'll just do it right now change it as we go and then here we'll take in an app and that is of type corner to tvu.application do that too that should work now list here I should be able to do lists and then equals menu menu then passing up boom that looks like it's happy now anyway we just keep it a little simpler and we'll make changes to our menu I have a feeling we're definitely gonna have to separate some files I'm trying to keep it simple because again the bash script I think it was like 51 lines but the bash script was all in one file but I can already tell that we're kind of going to end up with multiple files with this to go wrong main.go all right sweet still works A B C all right cool yeah now the next thing I really need to do is when I hit this add item that callback function looks promising maybe it'll allow me to maybe allow me to actually do something but I think there's another better way to do it with this that I saw which is this set changed function so if I do I can do list dot set change to function which is pretty cool you can see here a set change function sets the function which is called when the user navigates the list item that's really what we want to do and we need some sort of function here that does that so the type of function we need is this guy here and this is really the purpose of this is to set the response for selecting an item so I'll just say set response and then this is what we got that should work and then I should just be able to say set response here now in my set response I have some data in my parameters I'm getting the index where our list item actually is I'm getting the main text of secondary text all the stuff that we set up top which makes sense and that shortcut item now I'm thinking this I mean to keep this simple we could really just do like a case statement here a switch statement case statement whatever you want to call it but I say let's do switch and then we can switch do we want to switch no uh we could switch the index but I say switch let's switch on the because I think that'll end up reading a little better so we'll say switch shortcut and then we'll do opening of my case case first one was M I gotta do single quotes because roon and then I should be able to do like I think there's some type there's probably some type of Setter I would think you know I could use a global on that list I mean you know I don't love doing it but I like I'm going to want to have to do something here like list dot set blah blah blah and right now it's just at the scope of my main function so I might just do this I might say list there and and then this is what my tv.list and we'll just declare this globally I mean you know cry about it if you want but this all right so let's do uh VAR list boom as of type 2 list then we're setting it without the colon here great that works so now I should be able to access my list this dot yeah should be able to do and I bet they have a bunch of Setters yeah they have they have Setters here set item text that looks promising there at the bottom so Adam text sets an item's Main in secondary text panics of index is out of range okay so we're not setting the secondary text right now what we could do is we could set the secondary text with the result of what we're trying to have so let's do that we'll set the and we'll take in the index which we have access to that now so I'll be able to do index here we'll do the main string which we'll just pass in that main text because I want to keep that and then the secondary we'll say this is so this is for m um you could probably just leave I'll leave that I mean well let's just test it out first so let's say uh select that could work and then we'll do a default I think the default case right which is let's do fmt.friendline uh that is not an available this might work this might not let's see should work go run may.go okay so if I said M I'll see you said that's not an available option oh but it worked there I had to like I clicked off of it and then I hit M again let's add the available let's let's add the other statements and see if there's still a problem do a little troubleshooting so I'll do this we'll say case because really you should never have to hit the first one it should just be placeholder text that's what that's what I feel like replaceable but you know whatever that's the stride out so here we got main text and then I'm just gonna say what I want to implement like Implement what is this one available memory yeah available but it does feel better it feels like I'm doing something I'm actually supposed to do and then C is get current logged in use of implement all right let's test it out go run main.go we're in here A B that works it's just when I press M if I press M first that seems to be a problem we'll deal with that that later because for the most part it works so let's uh go okay we have our options here now what we need so I found this memory kind of helper Library we're gonna use that we'll say go get github.com okay good that's added well probably like I probably feel like I should have that hander Handler function a little Handler function right we'll do uh Funk mem Handler maybe that works and then we'll do m equal total amount and I want to convert that to gigabytes because I I know this gives back bytes so let's we want we want to convert that to gigabytes which is basically just dividing by 10 24 3 3 times we'll do a little helper function we'll just say bytes two bytes uh we'll take in and a number and that'll be a V4 this is turning and then we'll return and I think it's just let me just return I divided by 10 divided by 10. there's probably way better ways of doing that but I think that's what happens if I want to get gigabytes so I can get gigabytes now I should be able to by saying bytes gigabytes uh M and then yeah I should be able to just return B and then that should be four so that works so now should be able to do this I had my item go to my case statement and I should be able to run here like I forget what I said already our mem Handler mem Handler right for um equals that up right now it wants a string so we'll just do a little little Sprint F left into the Sprint F boom hit him with the mem and do a little format string boom that value because whatever now if I press a boom 64. okay so now prettify that a little bit go here I will do a little you know just add gigabytes there from main.go a 64 gigabytes get available memory we're CIS admining it up let's freaking go too high too high for a basic system admin app I've got a couple more handlers I I think my file is already getting big so right we have to do this disk one I anticipate the usual one being easy because I think I've done that something like that before and go but I don't think the disc one is going to be that easy like we might actually have to Define our own types and stuff like that so I'm gonna just do that separately preemptive strike here uh just open main.go yeah we'll do this space next open up disk package main just run it with that star the star.go that start I go and we're gonna do some things we're gonna do some things here this should be fun all right so first thing because I want really I want the status of my disc I want status and we can use syscall so let me create this I'm going to create a disk status drug we're going to put all for all of our available disk space and that's going to be 164. we'll do used which will also be a unit 64. and we'll do the free which will be a uint and I mean we should be able to knock this out in one function let's do Funk and we'll say d which is pointing to disk status so we'll have to declare a disk status and then this will be a method function for that we'll say disk usage and provide I know you have to provide a path what's this call but we'll we'll verify that in a second all right so we have FS we're going to do FS equals syscall boom dot stat FS underscore T yay declare that bad boy all right now this point I'll do error we'll do syscall not stat FS the path which uh yeah yeah we're gonna need this yep because it requires that path and then it's that so I should be able to do path which I don't have currently coming in and then pass FS probably a reference to FS then that should be good so then I should be a path string that'll be fine and then the basic you know if a nil then return error let's go I probably should return if I want to return error huh makes sense now we get to set some stuff so I should be able to set now D dot all which is going to reference FS dot blocks and we're gonna multiply that by 64 pieces okay and for all of you that are wondering yeah I do have this part that I wrote before just just so you know like this is an all off the top I felt like that just had to be said like I wrote it this morning all right eat up free equals FS dot b free the reason why though I'm typing this out like this is because I feel like it helps me when I watch coding stuff where people are actually building it out so that's why I'm I decided to do it this way let me know in the comments if that's not ideal but it helps me when I'm watching people code so I'm gonna do this we're gonna get the free space dude I used I did freestyle some of the stuff at the beginning it's a little different than my reference code uh but yeah I'd say probably at least 70 percent up in referencing back and forth and of course we can get to use this space by uh subtracting the free space from all of the space makes sense and then I should be able to return now boom great so now we'll go back to my main.goal my goal and I look at dish Handler I don't have a discount there actually I have a user Handler it will keep this pattern though of doing like a disc Handler a Handler for every case statement so I can keep this case statement as simple as possible so let's do that I'll do a funk disc Handler and then this status uh disc close the reference disk status boom instance the disk now I can do my error and call my disc got disk usage with the path which is going to be my root path I'm gonna pass in there then normal go laying stuff of error equal no blah blah blah blah dot fatal all the things all the things being my ear I did do some things with constants to make the conversion from like the bytes to gigabytes a little bit easier so we'll add that now even though it's funny because I'm still using the other helper function but we'll say bytes equal one let's say key KB equals 10 25 times B and megabytes 10 24 times KB and we'll do gigabytes equals 10 24 times MB we're going to use these cons to actually convert all of my disk stuff you'll see here in a second all right so I can say disk now and I can just do colon equals disk Handler call that batboy it returns my disk status then I can do my Sprint F again all right so we're gonna display all we're gonna play all of our kind of information we're gonna say hey this is the disc all space all the space available let me convert this to gigabytes then we're going to display our u space to another percent beat and that's in gigabytes and then we're going to do the same for our free space do the bytes now I just pass in all the things so this is about to get real all right bear with me so I'll do an INT that should work let's go ahead and test that out quick quit I go oh didn't like it oh because I got to do a go run start I go because I create another file all right let's see a should work four gigs b boom that's freaking go I don't know why like I wrote this this morning and this is like I'm still excited about it now we only have one more we only have one more to implement I know the video is getting long but bear with me we got one more then now it's expecting me to return a string so I I just want the username I can get my username by doing user dots username boom now all I have to do is return my username love it so now this should be pretty easy go down to my disk use my user Handler you Handler turns my string I should just be able to say Tina Sprint F I'll scroll down a little bit you guys can see the fmt that's F and then do uh yeah current user V and then pass in my user which would be you how many lines of code is this just in this file is 95. there's way more than our basket but let's go ahead and run it go run star go all right hit a it's memory B I got yeah that's right 926 gigs u713 for 12. that's cool and see my login user generally I speak wheat it works and I mean I think it looks better but let's go ahead we'll get into the grading and let me know how you all will grade this in the comments as well because I think it could be some interesting conversation especially for those who are like heavy into the bash group all right so I mean it was a lot it was honestly a lot more okay but let's let's break this down let's break down the grading based on kind of what I've seen when it comes to Beauty I think the go app kind of takes the cake it just looks a lot nicer it looks a lot more well put together right if you look back at what I showed you at the beginning it was you know it was a little rough but that it was so simple it was only like what 51 lines right so there's the trade-off and that's the thing with all this Tech stuff is you're going to have trade-offs the next thing is kind of like how easily could I add features to both of them the bash script is going to be a lot more challenging to continuously add features let alone add them as a team right it's just there's not a lot of good language constructs it's going to get messy really quickly there's no need to really do it like I think there's there is some Modern things there are some Modern things that you can do to make bash a little more scalable but I I still give it to go or any general purpose programming language here just because of the annoyance right and plus you don't have a lot of people that are really experts in bash right which took longer the right uh honestly the bash script wins hands down I mean it was like half the code what was the stats the stats ended up being the goaling up was like 124 lines of code and the battleship was 51 lines of code which is really I think the most powerful case for bash in the simple script all all day long like you're not going to have to write as much to get a very similar thing done it might not look as pretty but it'll get the job done until you actually get to a point where you need something more scalable like a general purpose programming language yeah those are my thoughts that's kind of how great I think I would lean still lean go but I'm not gonna give up bash just because it like I said it's just really useful at certain times to just throw some commands in a file and get some stuff done but I'm super excited that I learned about this TV thing I think I'm gonna mess with that a lot more because that's been really cool anyway let me know your thoughts in the comments like comment subscribe all that fun stuff see you on the next one thank you foreign [Music]
Info
Channel: Gerald Yerden II
Views: 7,463
Rating: undefined out of 5
Keywords: golang, terminal user interface, cli, app development, linux, introduction to golang, golang tutorial, learn golang, golang tutorial for beginners, golang for beginners, golang beginner tutorial, go programming
Id: 9bDN2rrf-Pw
Channel Id: undefined
Length: 26min 2sec (1562 seconds)
Published: Mon Sep 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.