editFile-1.7.0

KOSI Plugin editFile Version 1.7.0

Summary

The editFile Plugin enables modifications to YAML and text files by allowing users to add, overwrite, or delete specific content. It provides precise control over file edits by targeting YAML keys or specific line numbers in text files.

Keys

operation (required)

Defines the type of modification to be performed. This key accepts one of the following three modes:

Key Description
add Inserts a new value at the specified key (YAML) or line (text).
overwrite Replaces the value of a specified key (YAML) or line (text).
delete Removes a key (YAML) or deletes an entire line (text).

add

Adds a value to a specified key in a YAML file or a specific line in a text file.

  • For YAML files: You must specify the key where the new value should be added.
  • For text files: You must specify the line number where the new text should be inserted.

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

Replaces the existing value at a specified key (YAML) or line (text).

Note: add and overwrite work the same for yaml.

delete

Removes a key (YAML) or deletes an entire line (text).

  • For YAML files: The key and its associated value will be removed.
  • For text files: The specified line will be deleted, and subsequent lines will shift up.

filePath (required)

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

fileType (required)

Defines the type of file being edited.

  • Set to “yaml” for YAML files.
  • Set to “text” for plain text files.

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)

Defines the new value to be written into the file.

  • For text files, use \n to indicate a new line and \t for indentation.
  • For YAML files, this is the value assigned to the specified key.

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