editFile-1.6.0

KOSI Plugin editFile Version 1.6.0

Summary

The editFile Plugin allows you to add, overwrite or delete specific parts of a yaml or text file.

Keys

operation (required)

This key accepts the three operating modes as its value:

  • add
  • overwrite
  • delete

add

Add a value to the specified key (yaml) or line (text).
For yaml files you have to specify the key and value you want to add to the file. For text files you have to specify the line and value you want to add to the file.

For example:

- editFile:
    operation: add
    fileType: text
    filePath: "/root/KosiPlugin/script.text"
    key: "spec.clusterMaster.toAdd"
    value: Hier steht ein Text

The key toAdd will be created / written into the file as a subtree of clusterMaster with the given value.

overwrite

Overwrite the text with the value on the specified key (yaml) or line (text)

Note: add and overwrite work the same for yaml.

delete

Delete the text of the specified key (yaml) or line (text). In both cases the whole line is removed.

filePath (required)

Set the absolute pathe to the file to be edited as the value of this key.

fileType (required)

If the file that you want to edit is a yaml file, set the value of this key to "yaml".
If the file is a text file, set the value of this key to "text".

key

This is a mandatory key for yaml files.

This key allows you to specify the key you want to add, overwrite or delete in your yaml file.
Key is based on JSON Path.

For example:

key: "spec.clusterName"

This example shows the value of key, if you want to edit the key clusterName with the parent spec.

Note: If you have a list the key would be as followd:

key: "spec.clusterName.list.[0].Name"

line

This is a mandatory key for text files.

Allows you to set the line number where the operation gets executed.

value (required)

Set the new value you want to write to the file as the value of this key. If you want to write a text/block you have to use \n at the end of each line and \t to indent line.

For example:

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/kosi/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
  input="template.yaml";
}

containers =
{
    example=["docker.io", "nginx", "latest"];
}

install
{
    editFile(operation="overwrite"; fileType="text";filePath="/root/KosiPlugin/script.txt";line="3";
    value="Hier steht ein Text \n
           \t clusterName: hier steht ein Name \n
           \t hostips: \n
           \t \t ips: \n
           \t \t \t - 192.168.79.130");
}

For yaml files, the whole string is set as the value for the given key.

For text files, when using add, the text will inserted beginning at line. For every newline in value a new line will be added to te file.
When using overwrite, the value will be split into multiple lines and every line in the input will replace one line in the old file starting at line.

Make sure that there are enough lines to overwrite

Examples for yaml files

example file

spec:
  clusterMaster:
    someText: this is an example
    toOverwrite: this will be gone

Example 1 - add new text

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/kosi/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
  input="template.yaml";
}

containers =
{
    example=["docker.io", "nginx", "latest"];
}

install
{
    editFile(operation="add"; fileType="text";filePath="/root/KosiPlugin/example.yaml";key="spec.clusterMaster.toAdd";
    value="this is ne text");
}

Result:

spec:
  clusterMaster:
    someText: this is an example
    toOverwrite: heir steht text
    toAdd: this is new text

Example 2 - overwrite text

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/kosi/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
  input="template.yaml";
}

containers =
{
    example=["docker.io", "nginx", "latest"];
}

install
{
    editFile(operation="overwrite"; fileType="yaml";filePath="/root/KosiPlugin/example.yaml";key="spec.clusterMaster.toOverwrite";value="this replaced the old text");
}

Result:

spec:
  clusterMaster:
    someText: this is an example
    toOverwrite: this replaced the old text

Example 3 - delete text

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/kosi/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
  input="template.yaml";
}

containers =
{
    example=["docker.io", "nginx", "latest"];
}

install
{
    editFile(operation="delete"; fileType="yaml";filePath="/root/KosiPlugin/example.yaml";key="spec.clusterMaster.toOverwrite");
}

Result:

spec:
  clusterMaster:
    someText: this is an example

Examples for text files

example file

bucket list:
 - item 1
 - item 2
 - item 3

Example 1 - add new text

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/kosi/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
  input="template.yaml";
}

containers =
{
    example=["docker.io", "nginx", "latest"];
}

install
{
    editFile(operation="add"; fileType="text";filePath="/root/KosiPlugin/example.txt";line='"5"';value='" - item 4"');
}

Result:

bucket list:
 - item 1
 - item 2
 - item 3
 - item 4

Example 2 - overwrite text

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/kosi/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
  input="template.yaml";
}

containers =
{
    example=["docker.io", "nginx", "latest"];
}

install
{
    editFile(operation="overwrite"; fileType="text";filePath="/root/KosiPlugin/example.txt";line="4";value=" - item 3");
}

Result:

bucket list:
 - item 1
 - item 2
 - new item 3

Example 3 - delete text

languageversion = "1.0.0";
apiversion = "kubernative/kubeops/kosi/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";

files =
{
  input="template.yaml";
}

containers =
{
    example=["docker.io", "nginx", "latest"];
}

install
{
    editFile(operation="delete"; fileType="text";filePath="/root/KosiPlugin/example.txt";line="3");
}

Result:

bucket list:
 - item 1
 - item 3