T
Task3w ago
ibragdzh

Parentheses in commands

I understood I should escape () parentheses, but how do I do it? Backslashes did not work, task says: task: Failed to parse Taskfile.yml: yaml: line 16: found unknown escape character
5 Replies
andreynering
andreynering2w ago
@ibragdzh Can you paste that part of your Taskfile? It an YAML parse error, so it looks like simply wrapping your command into quotes would work.
ibragdzh
ibragdzhOP2w ago
Sure, here it is:
version: '3'

dotenv: ['.env']

vars:
MIGRATION_FILE: "
package migrations

import (
\"github.com/rs/zerolog/log\"
migrate \"github.com/xakep666/mongo-migrate\"
\"go.mongodb.org/mongo-driver/mongo\"
)

//nolint:gochecknoinits // used for migrations
func init() {
err := migrate.Register(
func(db *mongo.Database) error {
return nil
},
func(db *mongo.Database) error {
return nil
},
)
if err != nil {
log.Err(err).Msg(\"$${TIMESTAMP}_$${NAME} migration error\")
}
}
"

tasks:
migrate/create:
cmds:
- "read -p \"enter name: \" NAME; \
TIMESTAMP=$$(date +%s); \
FILENAME=\"migrations/$${TIMESTAMP}_$${NAME}.go\"; \
mkdir -p internal/persistence/migrations; \
echo \"{{.MIGRATION_FILE}}\" > $$FILENAME; \
echo \"Migrate file $$FILENAME created!\""
version: '3'

dotenv: ['.env']

vars:
MIGRATION_FILE: "
package migrations

import (
\"github.com/rs/zerolog/log\"
migrate \"github.com/xakep666/mongo-migrate\"
\"go.mongodb.org/mongo-driver/mongo\"
)

//nolint:gochecknoinits // used for migrations
func init() {
err := migrate.Register(
func(db *mongo.Database) error {
return nil
},
func(db *mongo.Database) error {
return nil
},
)
if err != nil {
log.Err(err).Msg(\"$${TIMESTAMP}_$${NAME} migration error\")
}
}
"

tasks:
migrate/create:
cmds:
- "read -p \"enter name: \" NAME; \
TIMESTAMP=$$(date +%s); \
FILENAME=\"migrations/$${TIMESTAMP}_$${NAME}.go\"; \
mkdir -p internal/persistence/migrations; \
echo \"{{.MIGRATION_FILE}}\" > $$FILENAME; \
echo \"Migrate file $$FILENAME created!\""
I'm migrating from Makefile and this command was working as expected with make, but I don't yet know how do I adapt it to Taskfile just mentioning, the answer is message below
andreynering
andreynering2w ago
@ibragdzh This page has some explanation on how to declare multi-line string in YAML: https://yaml-multiline.info/ Using | is probably what you want.
YAML Multiline Strings
Find the right syntax for your YAML multiline strings.
ibragdzh
ibragdzhOP2w ago
Thanks. Updated this multiline var, now returned to the another error saying no parentheses:
$ task migrate/create
task: [migrate/create] read -p "enter name: " NAME; TIMESTAMP=$$(date +%s); FILENAME="migrations/$${TIMESTAMP}_$${NAME}.go"; mkdir -p internal/persistence/migrations; echo "package migrations

import \(
\"github.com/rs/zerolog/log\"
migrate \"github.com/xakep666/mongo-migrate\"
\"go.mongodb.org/mongo-driver/mongo\"
\)

//nolint:gochecknoinits // used for migrations
func init\(\) {
err := migrate.Register\(
func\(db *mongo.Database\) error {
return nil
},
func\(db *mongo.Database\) error {
return nil
},
\)
if err != nil {
log.Err\(err\).Msg\(\"$${TIMESTAMP}_$${NAME} migration error\"\)
}
}
" > $$FILENAME; echo "Migrate file $$FILENAME created!"
task: Failed to run task "migrate/create": 1:43: a command can only contain words and redirects; encountered (
$ task migrate/create
task: [migrate/create] read -p "enter name: " NAME; TIMESTAMP=$$(date +%s); FILENAME="migrations/$${TIMESTAMP}_$${NAME}.go"; mkdir -p internal/persistence/migrations; echo "package migrations

import \(
\"github.com/rs/zerolog/log\"
migrate \"github.com/xakep666/mongo-migrate\"
\"go.mongodb.org/mongo-driver/mongo\"
\)

//nolint:gochecknoinits // used for migrations
func init\(\) {
err := migrate.Register\(
func\(db *mongo.Database\) error {
return nil
},
func\(db *mongo.Database\) error {
return nil
},
\)
if err != nil {
log.Err\(err\).Msg\(\"$${TIMESTAMP}_$${NAME} migration error\"\)
}
}
" > $$FILENAME; echo "Migrate file $$FILENAME created!"
task: Failed to run task "migrate/create": 1:43: a command can only contain words and redirects; encountered (
Trying both escaped and non-escaped parentheses didn't work for me
andreynering
andreynering2w ago
Try to change echo \"{{.MIGRATION_FILE}}\" > $$FILENAME; to echo {{shellQuote .MIGRATION_FILE}} > $$FILENAME;
Want results from more Discord servers?
Add your server