T
Task2mo 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
7 Replies
andreynering
andreynering2mo 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
ibragdzhOP2mo 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
andreynering2mo 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
ibragdzhOP2mo 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
andreynering2mo ago
Try to change echo \"{{.MIGRATION_FILE}}\" > $$FILENAME; to echo {{shellQuote .MIGRATION_FILE}} > $$FILENAME;
ibragdzh
ibragdzhOP4w ago
Unfortunately,
$ 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\n\nimport (\n \\"github.com/rs/zerolog/log\\"\n migrate \\"github.com/xakep666/mongo-migrate\\"\n \\"go.mongodb.org/mongo-driver/mongo\\"\n)\n\n//nolint:gochecknoinits // used for migrations\nfunc init() {\n err := migrate.Register(\n func(db *mongo.Database) error {\n return nil\n },\n func(db *mongo.Database) error {\n return nil\n },\n )\n if err != nil {\n log.Err(err).Msg(\\"$${TIMESTAMP}_$${NAME} migration error\\")\n }\n}\n' > $$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\n\nimport (\n \\"github.com/rs/zerolog/log\\"\n migrate \\"github.com/xakep666/mongo-migrate\\"\n \\"go.mongodb.org/mongo-driver/mongo\\"\n)\n\n//nolint:gochecknoinits // used for migrations\nfunc init() {\n err := migrate.Register(\n func(db *mongo.Database) error {\n return nil\n },\n func(db *mongo.Database) error {\n return nil\n },\n )\n if err != nil {\n log.Err(err).Msg(\\"$${TIMESTAMP}_$${NAME} migration error\\")\n }\n}\n' > $$FILENAME; echo "Migrate file $$FILENAME created!"
task: Failed to run task "migrate/create": 1:43: a command can only contain words and redirects; encountered (
andreynering
andreynering4w ago
Looks like there's a leading $ in front of that string. I think it shouldn't be there.
Want results from more Discord servers?
Add your server