How awesome is cookiecutter?
Published: 2021-07-07 17:05:31

Originally published on [mccricardo.com](https://mccricardo.com/cookiecutter/).

Have you ever been in the sittuation of trying to structure a project but having no idea how? Maybe you're new to a specific technology and have no clue how to organize things. Or maybe you start projects often and keep repeating the same set of tasks over and over again.

If you read [Backstage Software Templates](https://mccricardo.com/backstage-software-templates/) maybe you noticed something called **cookiecutter**. So, what is it? Apart from being awesome, **cookiecutter** is a command-line utility that is able to create projects from templates. This allows us and our teams to specify templates for projects, use those templates and share them among teams. We can even embed it into other tools, like [Backstage](https://backstage.io/).

## Quickstart
There are [several options](https://cookiecutter.readthedocs.io/en/1.7.2/installation.html) to install **cookiecutter** but we will go with [pip](https://pypi.org/project/pip/):
```console
~ pip install --user cookiecutter
```

Now that we have **cookiecutter** installed, we can define our templates and start using them. Even better is the fact that we can use publicly available templates and start coding right away. For our purposes, we'll be creating a [Go](https://golang.org/) project using the [cookiecutter-golang](https://github.com/lacion/cookiecutter-golang) template:

```console
~ cookiecutter https://github.com/lacion/cookiecutter-golang.git
full_name [Luis Morales]: Ricardo Castro
github_username [lacion]: mccricardo
app_name [mygolangproject]: demo
project_short_description [A Golang project.]: A demo
docker_hub_username [lacion]: mccricardo
docker_image [lacion/alpine-base-image:latest]: golang:1.16.5-apline3.14
docker_build_image [lacion/alpine-golang-buildimage]: golang:1.16.5-apline3.14
Select docker_build_image_version:
1 - 1.13
2 - 1.12.9
3 - 1.11.9
Choose from 1, 2, 3 [1]: 1
Select go_mod_or_dep:
1 - mod
2 - dep
Choose from 1, 2 [1]: 1
use_docker [y]: y
use_git [y]: y
use_logrus_logging [y]: y
use_viper_config [y]: y
use_cobra_cmd [y]: y
Select use_ci:
1 - travis
2 - circle
3 - none
Choose from 1, 2, 3 [1]: 3
Initialized empty Git repository in /home/mccricardo/test/demo/.git/
[master (root-commit) 2e82ac9] Initial Commit.
 13 files changed, 597 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 AUTHORS.md
 create mode 100644 CONTRIBUTING.md
 create mode 100644 Dockerfile
 create mode 100644 Makefile
 create mode 100644 README.md
 create mode 100644 cmd/root.go
 create mode 100644 cmd/version.go
 create mode 100644 config/config.go
 create mode 100644 go.mod
 create mode 100644 log/log.go
 create mode 100644 main.go
 create mode 100644 version/version.go
```

After **cookiecutter** has finished doing its _magic_ we can take a look at the result:
```console
~ demo git:(master) ls
AUTHORS.md  cmd  config  CONTRIBUTING.md  Dockerfile  go.mod  log  main.go  Makefile  README.md  version
~ demo git:(master) cat main.go 
package main

import (
	"github.com/mccricardo/demo/cmd"
)

func main() {
    cmd.Execute()	
}
```

As we can confirm, based on the [cookiecutter-golang](https://github.com/lacion/cookiecutter-golang) template, **cookiecutter** went ahead and created a folder strucutre as well as several files with boilerplate code.

Go ahead and take a look at the [documentation](https://cookiecutter.readthedocs.io/en/1.7.2/) as well as the [source code](https://github.com/cookiecutter/cookiecutter) and add one more tool to your automation arsenal.