← Back to blogYouTube Video

Published November 17, 2025

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

Part 8 of 19 in Syncing Projects with Google Drive

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

Summary

I set up a working Go CLI with Cobra for my Google Drive sync project, moving the folder-upload logic behind a sync command. I create a cmd package with a root command, wire Cobra’s Execute call into main.go, then add a sync subcommand that loops over the configured folders and uploads them to Drive. I land with a usable command—go run . sync—and outline that I’ll add selective-sync arguments and more commands in later parts.

Key takeaways

  • Use Cobra to structure Go CLIs around a root command and subcommands instead of scattering logic in main.go.
  • Keep commands in a dedicated cmd package; put the root command in root.go and call rootCmd.Execute() from main.
  • Attach subcommands in an init function with rootCmd.AddCommand(syncCmd).
  • Move real work into the command’s Run function—my sync command loops over configured folders and uploads each to Google Drive.
  • Design commands with future arguments and flags in mind, such as syncing one project or excluding specific projects.

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

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 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. It's 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 DriveSinker sync 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 gonna say `Cobra.CheckErr`, and then we're gonna pass in the command over here, which is gonna 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 gonna comment all of this out. We're still gonna 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 gonna access the CMD package over here and run the execute function that we defined in root.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 DriveSinker sync 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'm gonna say sync. This will be another command over here. Let's add the package name. Let's say `var syncCommand` equals a reference to `cobra.Command`. `Use` 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 syncing projects to Google Drive." Super simple stuff. And then `Run`, and then the function over here. And then over here, we can actually upload all 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 `rootCommand.AddCommand` and then pass in `syncCommand`. That's pretty much all we need to do. Now, if I run `go run . 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's be honest, I don't 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 to 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. We're going to 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.CheckErr` message and then `rootCommand.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

Hassan Aziz pointing up

Dude, you’re at the bottom of my landing page.

Just book a call already if you’re that interested.

You scrolled all the way here.