HomeTalksPanels
 
  

How awesome is cookiecutter?

July 4, 2021

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 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.

Quickstart

There are several options to install cookiecutter but we will go with pip:

~ 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 project using the cookiecutter-golang template:

~ 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:

~ 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 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 as well as the source code and add one more tool to your automation arsenal.

Ricardo Castro

Ricardo Castro

Software Engineering, DevOps, SRE, Taekwondo and Metal

 

 

© 2021