Skip to content

Commit

Permalink
Add a script to help managing embedded scripts externally
Browse files Browse the repository at this point in the history
The idea of this script is if you have inside your script something like this :

It will replace this #include with the content of python/script.py

This make it easier to write your tasks and use linters/code checkers from your
editor on your included script.
  • Loading branch information
chmouel committed May 26, 2021
1 parent fc29455 commit 9ae22bf
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/embedded-script-external/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Embed your script in external files

This is a small script that helps you manage your embedded [scripts
](https://github.com/tektoncd/pipeline/blob/main/docs/tasks.md#running-scripts-within-steps)
inside your Tekton tasks externally.

## Install

You need the [ruamel.yaml](https://yaml.readthedocs.io/en/latest/) library, it
should be availabe in fedora/debian default repositories or you can simply use
pip to install it :

```shell
pip3 install ruamel.yaml
```

## Usage

If for example you have a task like this :

```yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: git-clone
spec:
steps:
- name: step1
script: "#include script.sh"
```
The script will see it and includes the `script.sh` in place of your "#include script.sh"

## CAVEAT

It doesn't support embedded `TaskSpec` or `PipelineSpec` yet but there is no
reason this cannot be supported.
69 changes: 69 additions & 0 deletions test/embedded-script-external/embedded-script-external.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# Author: Chmouel Boudjnah <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import io
import os.path
import re
import sys
import typing

from ruamel.yaml import YAML

REGEXP = r"^#include\s*([^$]*)"


def replace(filename: str) -> typing.List:
yaml = YAML()
docs = yaml.load_all(open(filename))
rets = []
for doc in docs:
if 'spec' not in doc and 'tasks' not in doc['spec']:
continue
for task in doc['spec']['steps']:
if 'script' not in task:
continue
if not task['script'].startswith("#include "):
continue
match = re.match(REGEXP, task['script'])
if not match:
continue
filename = match[1].strip()
if not os.path.exists(filename):
sys.stderr.write(
f"WARNING: we could not find a file called: {filename} in task: {doc['metadata']['name']} step: {task['name']}"
)
continue
task['script'] = open(filename).read()
output = io.StringIO()
yaml.dump(doc, output)
rets.append(output.getvalue())
return rets


def parse_args():
parser = argparse.ArgumentParser(
description="Manage your embedded Tekton script task externally")
parser.add_argument("yaml_file", help="Yaml file to parse")
return parser.parse_args()


if __name__ == "__main__":
args = parse_args()
replaced = replace(args.yaml_file)
for doc in replaced:
if not doc or not doc.strip():
continue
print("---")
print(doc)

0 comments on commit 9ae22bf

Please sign in to comment.