editFile-1.7.0
4 minute read
KOSI Plugin editFile Version 1.7.0
Summary
The editFile plugin allows automated modifications to YAML and plain text files by supporting three main operations: add, overwrite, and delete. It enables precise targeting by allowing users to specify YAML keys or line numbers for text files.
Keys
| Key | Required | Description |
|---|---|---|
operation |
Yes | Defines the type of modification: add, overwrite, or delete. |
filePath |
Yes | Absolute path to the file being edited. |
fileType |
Yes | File type: yaml or text. |
key |
Required for fileType yaml |
Specifies the YAML key to be modified (dot-separated path format). |
line |
Required for fileType text |
Line number for the operation (1-based indexing). |
value |
Required for operation add and overwrite |
The content to insert or replace. Escaped linebreaks (\\n) and tabs (\\t) are supported in text files. |
Notes:
- For YAML files, only the
keyfield is used to identify the node.- For text files, only the
linefield is required to specify the line position.- For
valuein text: Double escaping (\\n,\\t) is required because the configuration parser interprets strings literally.- If using
overwriteoperation, ensure the file has enough lines, otherwise you may get an IndexOutOfRange error.
Examples
YAML File Examples
Sample YAML File
Shown as
/root/KosiPlugin/config.yamlin the examples below
spec:
clusterMaster:
existingText: this is an example
toBeOverwritten: this will be replaced
Example 1 - Add a new value
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";
files =
{
input = "template.yaml";
}
install
{
editFile
(
operation = "add";
fileType = "yaml";
filePath = "/root/KosiPlugin/config.yaml";
key = "spec.clusterMaster.newEntry";
value = "new value added";
);
}
Expected Result:
spec:
clusterMaster:
existingText: this is an example
toBeOverwritten: this will be replaced
newEntry: new value added
Example 2 - Overwrite an existing value
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";
files =
{
input = "template.yaml";
}
install
{
editFile
(
operation = "overwrite";
fileType = "yaml";
filePath = "/root/KosiPlugin/config.yaml";
key = "spec.clusterMaster.toBeOverwritten";
value = "this text has been replaced";
);
}
Expected Result:
spec:
clusterMaster:
existingText: this is an example
toBeOverwritten: this text has been replaced
Example 3 - Delete a YAML key
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";
files =
{
input = "template.yaml";
}
install
{
editFile
(
operation = "delete";
fileType = "yaml";
filePath = "/root/KosiPlugin/config.yaml";
key = "spec.clusterMaster.toBeOverwritten";
);
}
Expected Result:
spec:
clusterMaster:
existingText: this is an example
Text File Examples
Sample Text File
Shown as
/root/KosiPlugin/todo.txtin the examples below
To-do List:
- Buy groceries
- Clean house
- Pay bills
Example 1 - Add a new line
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";
files =
{
input = "template.yaml";
}
install
{
editFile
(
operation = "add";
fileType = "text";
filePath = "/root/KosiPlugin/todo.txt";
line = "'4'";
value = " - Call the bank";
);
}
Expected Result:
To-do List:
- Buy groceries
- Clean house
- Pay bills
- Call the bank
Example 2 - Overwrite an existing line
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";
files =
{
input = "template.yaml";
}
install
{
editFile
(
operation = "overwrite";
fileType = "text";
filePath = "/root/KosiPlugin/todo.txt";
line = "'3'";
value = " - Pay insurance";
);
}
Expected Result:
To-do List:
- Buy groceries
- Clean house
- Pay insurance
Example 3 - Delete a line
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";
files =
{
input = "template.yaml";
}
install
{
editFile
(
operation = "delete";
fileType = "text";
filePath = "/root/KosiPlugin/todo.txt";
line = "'2'";
);
}
Expected Result:
To-do List:
- Clean house
- Pay bills
Advanced Text Example - Multiline Overwrite
languageversion = "1.0.0";
apiversion = "kubernative/kubeops/sina/user/v4";
name = "kosi-example-packagev3";
description = "kosi-example-package";
version = "0.1.0";
docs = "docs.tgz";
logo = "logo.png";
files =
{
input = "template.yaml";
}
install
{
editFile
(
operation = "overwrite";
fileType = "text";
filePath = "/root/KosiPlugin/script.txt";
line = "'3'";
value = "Cluster Configuration:\\n clusterName: exampleCluster\\n hosts:\\n nodes:\\n - 192.168.79.130";
);
}
Expected Behavior:
- The plugin replaces lines 3 to 7 with the specified text.
- The target file must have at least 5 lines from line 3 onward. Otherwise, an
IndexOutOfRangeerror will be thrown.
Hint: Always ensure the file has enough lines when using multiline
valuewithoverwriteon text files.
Escape Rule: When using special characters invalue, escape them as\\n(newline),\\t(tab) in text files.