Back to blogYouTube Video

Published November 17, 2025

Syncing projects with Google Drive - Part 8 (Set up CLI cmds)

Can't play the video or having issues? Here's the direct link.

AI Summary

This video demonstrates how to implement a Command Line Interface (CLI) for a Google Drive synchronization tool using the Go package 'Cobra CLI'.

Key Takeaways

  • **Cobra CLI Integration**: The author uses Cobra CLI to transform a basic script into a professional command-line application, allowing for organized commands and arguments.
  • **Project Architecture**: A dedicated `cmd` package is created to separate CLI logic from the core application logic, maintaining a clean project structure.
  • **Root Command Implementation**: A `root.go` file is established to define the base behavior of the application (what happens when the program is run without arguments).
  • **Specific Command Logic**: A `sync` command is created in `sync.go` to handle the actual logic of uploading folders to Google Drive, effectively moving the synchronization loop from `main.go` into its own command function.
  • **Command Registration**: The `sync` command is linked to the root command via an `init()` function using `rootCmd.AddCommand()`, enabling the user to trigger synchronization by running `go run . sync`.

Description

I'll be using Cobra CLI, a very popular Go package for building CLI tools. We'll start by setting up our sync command. Github: https://github.com/hassanaziz0012/drive-syncer-video Full Playlist: https://youtube.com/playlist?list=PLTGiYd8gFivjVHK2xB7TLf7XLW8rmKXRM&si=L97UpFIHgfHbvLMT LINKS Website: https://www.hassandev.me My Book: https://www.hassandev.me/designing-websites X / Twitter: https://x.com/nothassanaziz

Transcript

Auto-generated transcript
Welcome back guys. Quick recap. In the previous video, in the previous part of this project, what we did was we basically just organized everything, all of the code into different files and folders and packages just so it's easier to manage. We created this API package, auth package to store all of the OAuth for the Google authentication stuff, an internal package that holds our structs and our configuration and stuff. And yeah, we organized all of the code that we've written so far into a bunch of packages. And in this video, I'm finally going to build a CLI so we can interact with this application. Now, I'm going to be using Cobra CLI, which is, I have it open up here. This is the GitHub repository for the project. and we can just go down here to the installation section and just grab this command and let's install this in our project so open up my terminal and just paste the command and it should install the whole part the whole package and there we go now the next thing we need to do once we've installed Cobra CLI is we need to create a CMD package over here that will store all of our commands And inside this package, I'm going to create a file called root.go. And this will have the root command of this program. So basically, the root command is the command that's going to run when you just run this program without passing any commands or arguments in there, right? So if I just say something like in the terminal, right? Let's say the name of the program is DriveSinker. If I just type this without adding any commands after that, then what's going to run is the root command that we're setting up over here and this is going to be a reference to cobra dot command it going to take a couple of fields first is use which is like the name of this whole thing And short which is like the short description of this command. I'll say sync local folders with Google Drive. Let's just say that. And finally, and most importantly, It's going to take run, which is going to be a function that's gonna give us a command and the arguments to that command. And for now, nothing complicated. I'm just gonna say println use driveSync to start synchronizing, there we go. And these should be single quotes over here. Let me fix that. There we go. Now finally, we want to create an execute function over here. We're going to say cobra.checkError. And then we're going to pass in the command over here, which is going to be rootcmd.execute. And that's all we need to do. Now we can go back to main.go over here. And instead of uploading all of these folders, I'm going to comment all of this out. We're still going to connect to the drive, obviously, so we can actually have a working drive service, right? but instead of uploading all of the folders in my config what I'm going to do is I'm going to access the CMD package over here and run the execute function that we defined in root dot go over here right and now let's see what happens right let's run go and see what happens okay awesome so it's gonna obviously connect to drive it's gonna run this function and it prints a bunch of logs right and then it just says use drive sinker sink to start sinking so That's the command that we're going to use to actually start synchronizing this whole thing, right? So all it does now is it just Runs this function over here in the root command and yeah that works now We can start setting up the sync command as well So I gonna say sync dot go this will be another command over here Let's add the package name. Let's say var sync command equals a reference to Cobra command Use this is the field that's gonna this is basically the command name I'm gonna have to say something like go run the program and then sync right to run this command so use sync over here short the short description start let's just say start syncing projects to Google Drive super simple stuff and then run and then the function over here and then Then over here, we can actually upload all of the folders. So I'm going to go back to main.go for just a second. I'm going to uncomment this stuff and just cut it out over here and paste it in sync.go over here. Fix all of the import errors as well. It should do that for you automatically. And yeah, that's all we need to do. We're just saying loop over the entire folders list in the config and just upload each of those folders to the Google Drive, right? Now that we have this command, we can say, we can define an init function that will run in this file. And we can say root command dot add command, and then pass in sync command. And that's pretty much all we need to do. Now if I run go run dot sync, right over here in the terminal, and maybe I should zoom in as well. So you guys can see a bit better. Now if I actually run this, let's see what happens. And there we go, it started the upload process. And it's creating the subfolders, uploading the directories and files, skipping the git stuff. Awesome! And there we go, the entire upload process finished over here Awesome So now we have a working CLI over here For now I only going to create this sync command because let be honest I don have a lot of features over here anyway But in the future I do want to add a bunch more commands and also I want to add a bunch of arguments to this sync command. Like let's just say what if we want to only synchronize a single project. What if we want synchronize all projects but exclude a couple projects right so there's a couple of arguments like that that I want to add to this command but then do that in the next part of the video and there's a couple more commands that I think I should add as well we're also going to do that in future parts of this video series for now I just wanted to create a working CLI and we've done that all we needed to do is create a CMD package over here create a root command and just execute it using this cobra.check error message and then root command.execute. And then just run this execute function in main.go over here and that runs your entire CLI for you. And then all you need to do is just create a bunch of commands, as many as you want, add their description and name and the run functions, and then just add these new commands to the root command by using this function call over here. I can create as many commands as I want. I can also access the arguments list for each of these commands and make sure to handle those as well. And yeah, that's how that works. So we have a CLI. We've built a working command to synchronize our projects to Google Drive. And the next couple of videos, I'm going to expand on the CLI a lot more, add a bunch more commands, add a bunch more arguments and make this a real program that we can actually use without having to change the code so much, right? Stay tuned for that and thank you for watching this video. Like, comment, subscribe, share, all those wonderful things, drive up the YouTube algorithm and everything and yeah, I'll see you in the next one. Thank you for watching.

Share this article

All great things started with a conversation

If you've got a cool project or opportunity and you want me to be a part of it, set up a free meeting with me here, and let's talk. 😊