# Multiline Strings with Starlark

**URL:** <https://drone.discourse.group/t/multiline-strings-with-starlark/10914>\
**Category:** Drone Support\
**Created:** [December 26, 2020, 11:34pm UTC](https://drone.discourse.group/t/multiline-strings-with-starlark/10914 "2020-12-26T23:34:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kavbrian](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/kavbrian/32/2468_2.png) [@kavbrian](https://drone.discourse.group/u/kavbrian)\
**Post date:** [December 26, 2020, 11:34pm UTC](https://drone.discourse.group/t/multiline-strings-with-starlark/10914/1 "2020-12-26T23:34:34Z")

</div>

Hello,

I’m converting my configuration from YAML to Starlark and I’ve encountered an issue with multiline strings.

In YAML I have the following pipeline step:

```auto
- name: check
  image: alpine
  pull: if-not-exists
  commands:
    - |
      if ! echo ${DRONE_COMMIT_MESSAGE} | grep -q ${DRONE_STAGE_NAME}; then
        echo ${DRONE_STAGE_NAME} build not required
        exit 78
      fi

```

…and I’m trying to write it in Starlark as:

```auto
return {
  'name': 'check',
  'image': 'alpine',
  'pull': 'if-not-exists',
  'commands': '''|
    if ! echo ${DRONE_COMMIT_MESSAGE} | grep -q ${DRONE_STAGE_NAME}; then
        echo ${DRONE_STAGE_NAME} build not required
        exit 78
    fi'''
}

```

This fails with a generic error when attempting to convert it to YAML:

```auto
$ drone starlark convert --stdout
yaml: unmarshal errors:
  line 1: cannot unmarshal !!str `|
    ...` into []string

```

I’ve tried various combinations of strings and escaping newlines, but none worked. If I run with `--format=false`, some output gets produced:

```auto
$ drone starlark convert --stdout --format=false
---
{"kind": "pipeline", "type": "docker", "name": "ctags", "platform": {"arch": "arm64", "os": "linux"}, "steps": [{"name": "submodule", "image": "plugins/git", "pull": "if-not-exists", "commands": ["git submodule update --init ${DRONE_STAGE_NAME%%-*}"]}, {"name": "check", "image": "alpine", "pull": "if-not-exists", "commands": "|\n if ! echo ${DRONE_COMMIT_MESSAGE} | grep -q ${DRONE_STAGE_NAME}; then\n echo ${DRONE_STAGE_NAME} build not required\n exit 78\n fi"}]}

```

Is this a bug, or am I writing the multiline command in Starlark wrong?

Thank you.

---

<div class="post-metadata">

**Author:** ![bradrydzewski](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/bradrydzewski/32/3513_2.png) [@bradrydzewski](https://drone.discourse.group/u/bradrydzewski)\
**Post date:** [December 27, 2020, 12:46am UTC](https://drone.discourse.group/t/multiline-strings-with-starlark/10914/2 "2020-12-27T00:46:30Z")

</div>

> [@Private: false repositories fails to clone](https://drone.discourse.group/t/private-false-repositories-fails-to-clone/8541/1):
>
> ```auto
> 'commands': '''|
> if ! echo ${DRONE_COMMIT_MESSAGE} | grep -q ${DRONE_STAGE_NAME}; then
> echo ${DRONE_STAGE_NAME} build not required
> exit 78
> fi'''
> 
> ```

I do see two things you can fix. First is that you do not need to prefix a multiple line string with `|` in starlark; this is only required when writing yaml. Second is the `commands` attribute is a string array; your example assigns a string where a string array is expected.

---

<div class="post-metadata">

**Author:** ![kavbrian](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/kavbrian/32/2468_2.png) [@kavbrian](https://drone.discourse.group/u/kavbrian)\
**Post date:** [December 27, 2020, 11:03am UTC](https://drone.discourse.group/t/multiline-strings-with-starlark/10914/3 "2020-12-27T11:03:06Z")

</div>

Thanks! That’s a very good point about `commands` being an array; I wrapped my string in list brackets and the error went away. The result I got was this:

```auto
commands:
  - "if ! echo ${DRONE_COMMIT_MESSAGE} | grep -q ${DRONE_STAGE_NAME}; then\n echo ${DRONE_STAGE_NAME} build not required\n exit 78\n fi"

```

I wasn’t sure about those literal `\n` inside the converted string, so in the end I put it all on one line, as in:

```auto
'commands': [
  'if ! echo ${DRONE_COMMIT_MESSAGE} | grep -q ${DRONE_STAGE_NAME}; then; ' +
    'echo ${DRONE_STAGE_NAME} build not required; ' +
    'exit 78; ' +
  'fi'
  ]

```

…which produces:

```auto
commands:
- if ! echo ${DRONE_COMMIT_MESSAGE} | grep -q ${DRONE_STAGE_NAME}; then; echo ${DRONE_STAGE_NAME} build not required; exit 78; fi

```

This seems good enough for now, and I plan to look into using the context object to generate these pipelines dynamically, so these `if` checks my end up being replaced completely.

Thanks again for your help!
