1
0
mirror of https://github.com/go-task/task.git synced 2024-12-16 10:59:23 +02:00
task/website/versioned_docs/version-latest/styleguide.mdx

228 lines
2.9 KiB
Plaintext
Raw Normal View History

---
slug: /styleguide/
sidebar_position: 10
---
2024-02-22 02:18:04 +02:00
# Style guide
2019-10-27 22:28:12 +02:00
2024-02-22 02:18:04 +02:00
This is the official style guide for `Taskfile.yml` files. It provides basic
instructions for keeping your Taskfiles clean and familiar to other users.
2019-10-27 22:28:12 +02:00
2024-02-22 02:18:04 +02:00
This guide contains general guidelines, but they do not necessarily need to be
followed strictly. Feel free to disagree and do things differently if you need
or want to. Any improvements to this guide are welcome! Please open an issue or
create a pull request to contribute.
2019-10-27 22:28:12 +02:00
2024-02-22 02:18:04 +02:00
## Use the suggested ordering of the main sections
2019-10-27 22:28:12 +02:00
2024-02-22 02:18:04 +02:00
```yaml
version:
includes:
# optional configurations (output, silent, method, run, etc.)
vars:
env: # followed or replaced by dotenv
tasks:
```
2019-10-27 22:28:12 +02:00
2024-02-22 02:18:04 +02:00
## Use two spaces for indentation
2019-10-27 22:28:12 +02:00
This is the most common convention for YAML files, and Task follows it.
```yaml
# bad
tasks:
foo:
cmds:
- echo 'foo'
# good
tasks:
foo:
cmds:
- echo 'foo'
```
2024-02-22 02:18:04 +02:00
## Separate the main sections with empty lines
2019-10-27 22:28:12 +02:00
```yaml
# bad
version: '3'
2019-10-27 22:28:12 +02:00
includes:
docker: ./docker/Taskfile.yml
output: prefixed
vars:
FOO: bar
env:
BAR: baz
tasks:
# ...
# good
version: '3'
2019-10-27 22:28:12 +02:00
includes:
docker: ./docker/Taskfile.yml
output: prefixed
vars:
FOO: bar
env:
BAR: baz
tasks:
# ...
```
2024-02-22 02:18:04 +02:00
## Separate tasks with empty lines
2019-10-27 22:28:12 +02:00
```yaml
# bad
version: '3'
2019-10-27 22:28:12 +02:00
tasks:
foo:
cmds:
- echo 'foo'
bar:
cmds:
- echo 'bar'
baz:
cmds:
- echo 'baz'
# good
version: '3'
2019-10-27 22:28:12 +02:00
tasks:
foo:
cmds:
- echo 'foo'
bar:
cmds:
- echo 'bar'
baz:
cmds:
- echo 'baz'
```
2024-02-22 02:18:04 +02:00
## Use only uppercase letters for variable names
2019-10-27 22:28:12 +02:00
```yaml
# bad
version: '3'
2019-10-27 22:28:12 +02:00
vars:
binary_name: myapp
tasks:
build:
cmds:
- go build -o {{.binary_name}} .
# good
version: '3'
2019-10-27 22:28:12 +02:00
vars:
BINARY_NAME: myapp
tasks:
build:
cmds:
- go build -o {{.BINARY_NAME}} .
```
2024-02-22 02:18:04 +02:00
## Avoid using whitespace when templating variables
2019-10-27 22:28:12 +02:00
```yaml
# bad
version: '3'
2019-10-27 22:28:12 +02:00
tasks:
greet:
cmds:
- echo '{{ .MESSAGE }}'
# good
version: '3'
2019-10-27 22:28:12 +02:00
tasks:
greet:
cmds:
- echo '{{.MESSAGE}}'
```
2024-02-22 02:18:04 +02:00
This convention is also commonly used in templates for the Go programming
language.
2019-10-27 22:28:12 +02:00
2024-02-22 02:18:04 +02:00
## Use kebab case for task names
2019-10-27 22:28:12 +02:00
```yaml
# bad
version: '3'
2019-10-27 22:28:12 +02:00
tasks:
do_something_fancy:
cmds:
- echo 'Do something'
# good
version: '3'
2019-10-27 22:28:12 +02:00
tasks:
do-something-fancy:
cmds:
- echo 'Do something'
```
2024-02-22 02:18:04 +02:00
## Use a colon to separate the task namespace and name
2019-10-27 22:28:12 +02:00
```yaml
# good
version: '3'
2019-10-27 22:28:12 +02:00
tasks:
docker:build:
cmds:
- docker ...
docker:run:
cmds:
- docker-compose ...
```
This is also done automatically when using included Taskfiles.
2024-02-22 02:18:04 +02:00
## Prefer using external scripts instead of multi-line commands
```yaml
# bad
version: '3'
tasks:
build:
cmds:
- |
for i in $(seq 1 10); do
echo $i
echo "some other complex logic"
done'
# good
version: '3'
tasks:
build:
cmds:
- ./scripts/my_complex_script.sh
```