Home
Sign in Buy now

Developer Experience

Justfile

Completed
Master Fullstack Golang Logo

Watch for free

Create a free account to watch this video.

Start for free

Summary

In this episode, we introduce Justfiles as a modern alternative to Makefiles that simplifies running verbose commands for tools like Goose and SQLC. We walk through the project's Justfile, explaining how it automatically loads environment variables, provides convenient aliases for database migrations and query compilation, and uses Air to watch for file changes and auto-reload the application. The parallel command runs both view compilation and the app server simultaneously, making development much more efficient. With the Justfile configured, we now have everything needed to start building out the actual application.

Transcript

Okay, so we have added our tools now, but these tools can be quite verbose to call. For example, when we want to generate a new migration file and apply the migration file, we need to point Goose to where our migrations live and where they should output. And the same with SQLC. We need to point it where the SQL files are, where everything should be placed. So this can become quite tedious. To solve this, we are going to be using a tool called Just, or Justfiles. Just is simply a modern version of Makefiles. And if you don't know about Makefiles, they are pretty much what we see here. It's just a way to group or make calling certain commands easier. For example, the "test all" build that you see right here combines two commands into one. So we first build and then we test. This makes our life a lot easier when dealing with these commands. So go ahead, install Just, and then we are going to jump into the Justfile for this project. I'm not going to type it out. I'm just going to walk through it so you get an idea of these Just commands and how they work and why they make our life that much easier. I will link the commands in the episode notes so you can copy paste them, or you can just grab it from the repository. Right. So the first command we have here is `set dotenv-load`. And if you're not familiar with dotenv, how you load dotenv into a shell is that normally we would have to either export them manually one by one, or we would in our dotenv file add export and then we could source it. This works on Unix-based systems. It doesn't work on Windows-based systems. So doing this all the time can be a little bit tedious. And you might also forget and spend time debugging it. And also, if you have some people on Unix-based systems and others on Windows, it will work differently for each one. So by doing this `set dotenv-load`, we automatically load our dotenv file into our session, into our terminal, and we can start using the environment variables. Next, we have a bunch of aliases, but we're not going to touch upon them now. I want to jump down here and show you the fix views command. So this is what we would have to type out ourselves if we did not have this command. The same here with generate, with watch, with compile. With compile queries, you can see `go tool sqlc compile -f`, and then the path to the SQLC config file. And then we do the same thing down here with generate queries. And then we get to migrations here, where we specify the path to the migrations, the type of the database engine, the database connection URI. And then we also have to provide it with what we want to do, the name, and so on and so forth. So you can see how these commands just make it so much easier to just have to write `just up migration`, `just down migration`, `just compile queries`, `just generate queries`, so on and so forth. We can also see we have this `run app` that uses the tool Air that we added. We specify our build command, where to put it, what to exclude, what to include, what to do when there's an error, and also what to do when we stop. So this `run app` will run our application. It will watch for changes, but not in node_modules. It will ignore that. It will look for file changes, including Go and CSS and JavaScript files. Now, this we technically don't need. I just want to add it there so you know you can exclude certain things. You might want to add node_modules. We're going to add node_modules to this project at one point. And it's really good to have this exclude directory for node_modules so we don't watch for folder changes in that, because it's going to be a huge folder. Finally, we have this `parallel` command. And this just runs `watch views` and `run app`. And `watch views`, we saw it quickly here, where we just call `go tool templ generate` and then we pass the watch flag. So this will just continuously watch for changes in the `.templ` files. And then it will compile every time you make a change. And then once we make a change, the `run app` command here will pick up that some Go files have been changed, and then that will recompile so we get the latest version of the views or can see the latest changes. And these two run in parallel. Okay, this is a very quick rundown of the Just commands, but it's literally a way where we can give a command a name and then have it do something. And we can group them together. Here, we could say, whenever we compile views, we also want to add fix views. So they will first run fix views, and then compile views. We can add multiple things here—we could run one command and then do something else. So this is really powerful. And you can add a lot of commands to these files that will make your life a lot easier. Finally, we have the aliases that should be kind of self-explanatory. For example, we have fix views that has an alias to FV. So we can actually go out and say `just FV`, which will run the fix views command. We can also say `just fix views`, and then it runs the same command. Just think of the Justfile here as a way to make your life easier and put commands in a place so we don't have to write all of this tedious syntax whenever we use the tools. Great. Now we basically have all we need to start building out the application. So in the next part, we will actually begin building our app and start to use all of these tools as well as start to add on to the application structure that we have created.

Episode Notes

Get the Full Course

Unlock all episodes and get lifetime access to course updates.

What others say

"With this course, I'm learning Golang + datastar while building my personal blog hitting several birds with one stone. It's a fantastic course."

Testimonial from ignacio
Ignacio Barceló
Bought Early Access

Video Completed!

Great job finishing this episode

Up Next

Building the home view
The first migration: articles table