| 1 |
with Ada.Text_IO; |
| 2 |
with Ada.Directories; |
| 3 |
|
| 4 |
with AAA.Strings; |
| 5 |
with AAA.Directories; |
| 6 |
|
| 7 |
with CLIC.Config.Load; |
| 8 |
|
| 9 |
with Simple_Logging; |
| 10 |
|
| 11 |
with TOML; |
| 12 |
|
| 13 |
package body CLIC.Config.Edit is |
| 14 |
|
| 15 |
package Trace renames Simple_Logging; |
| 16 |
|
| 17 |
use TOML; |
| 18 |
|
| 19 |
procedure Write_Config_File (Table : TOML_Value; Path : String) |
| 20 |
with Pre => Table.Kind = TOML_Table; |
| 21 |
|
| 22 |
function Remove_From_Table (Table : TOML_Value; |
| 23 |
Key : Config_Key) |
| 24 |
return Boolean |
| 25 |
with Pre => Table.Kind = TOML_Table; |
| 26 |
|
| 27 |
function Add_In_Table (Table : TOML_Value; |
| 28 |
Key : Config_Key; |
| 29 |
Val : TOML_Value) |
| 30 |
return Boolean |
| 31 |
with Pre => Table.Kind = TOML_Table; |
| 32 |
|
| 33 |
----------------------- |
| 34 |
-- Write_Config_File -- |
| 35 |
----------------------- |
| 36 |
|
| 37 |
procedure Write_Config_File (Table : TOML_Value; Path : String) is |
| 38 |
use Ada.Text_IO; |
| 39 |
use Ada.Directories; |
| 40 |
File : File_Type; |
| 41 |
begin |
| 42 |
|
| 43 |
-- Create the directory for the config file, in case it doesn't exists |
| 44 |
Create_Path (Containing_Directory (Path)); |
| 45 |
|
| 46 |
Create (File, Out_File, Path); |
| 47 |
Trace.Debug ("Write config: '" & TOML.Dump_As_String (Table) & "'"); |
| 48 |
Put (File, TOML.Dump_As_String (Table)); |
| 49 |
Close (File); |
| 50 |
end Write_Config_File; |
| 51 |
|
| 52 |
----------------------- |
| 53 |
-- Remove_From_Table -- |
| 54 |
----------------------- |
| 55 |
|
| 56 |
function Remove_From_Table (Table : TOML_Value; |
| 57 |
Key : Config_Key) |
| 58 |
return Boolean |
| 59 |
is |
| 60 |
use AAA.Strings; |
| 61 |
|
| 62 |
Id : constant String := Split (Key, '.', Raises => False); |
| 63 |
Leaf : constant Boolean := Id = Key; |
| 64 |
begin |
| 65 |
if not Table.Has (Id) then |
| 66 |
-- The key doesn't exist |
| 67 |
Trace.Error ("Configuration key not defined"); |
| 68 |
return False; |
| 69 |
end if; |
| 70 |
|
| 71 |
if Leaf then |
| 72 |
Table.Unset (Id); |
| 73 |
return True; |
| 74 |
else |
| 75 |
declare |
| 76 |
Sub : constant TOML_Value := Table.Get (Id); |
| 77 |
begin |
| 78 |
if Sub.Kind = TOML_Table then |
| 79 |
return Remove_From_Table (Sub, Split (Key, '.', Tail)); |
| 80 |
else |
| 81 |
Trace.Error ("Configuration key not defined"); |
| 82 |
return False; |
| 83 |
end if; |
| 84 |
end; |
| 85 |
end if; |
| 86 |
end Remove_From_Table; |
| 87 |
|
| 88 |
------------------ |
| 89 |
-- Add_In_Table -- |
| 90 |
------------------ |
| 91 |
|
| 92 |
function Add_In_Table (Table : TOML_Value; |
| 93 |
Key : Config_Key; |
| 94 |
Val : TOML_Value) |
| 95 |
return Boolean |
| 96 |
is |
| 97 |
use AAA.Strings; |
| 98 |
Id : constant String := Split (Key, '.', Raises => False); |
| 99 |
Leaf : constant Boolean := Id = Key; |
| 100 |
begin |
| 101 |
if Leaf then |
| 102 |
Table.Set (Id, Val); |
| 103 |
return True; |
| 104 |
end if; |
| 105 |
|
| 106 |
if not Table.Has (Id) then |
| 107 |
-- The subkey doesn't exist, create a table for it |
| 108 |
Table.Set (Id, Create_Table); |
| 109 |
end if; |
| 110 |
|
| 111 |
declare |
| 112 |
Sub : constant TOML_Value := Table.Get (Id); |
| 113 |
begin |
| 114 |
if Sub.Kind = TOML_Table then |
| 115 |
return Add_In_Table (Sub, Split (Key, '.', Tail), Val); |
| 116 |
else |
| 117 |
Trace.Error ("Configuration key already defined"); |
| 118 |
return False; |
| 119 |
end if; |
| 120 |
end; |
| 121 |
end Add_In_Table; |
| 122 |
|
| 123 |
----------- |
| 124 |
-- Unset -- |
| 125 |
----------- |
| 126 |
|
| 127 |
function Unset (Path : String; Key : Config_Key) return Boolean is |
| 128 |
use AAA.Directories; |
| 129 |
|
| 130 |
Tmp : Replacer := New_Replacement (File => Path, |
| 131 |
Backup => False, |
| 132 |
Allow_No_Original => True); |
| 133 |
|
| 134 |
Table : constant TOML_Value := Load.Load_TOML_File (Tmp.Editable_Name); |
| 135 |
begin |
| 136 |
|
| 137 |
if Table.Is_Null then |
| 138 |
-- The configuration file doesn't exist or is not valid |
| 139 |
Trace.Error ("configuration file doesn't exist or is not valid"); |
| 140 |
return False; |
| 141 |
end if; |
| 142 |
|
| 143 |
if not Remove_From_Table (Table, Key) then |
| 144 |
return False; |
| 145 |
end if; |
| 146 |
|
| 147 |
Write_Config_File (Table, Tmp.Editable_Name); |
| 148 |
|
| 149 |
Tmp.Replace; |
| 150 |
|
| 151 |
return True; |
| 152 |
end Unset; |
| 153 |
|
| 154 |
--------- |
| 155 |
-- Set -- |
| 156 |
--------- |
| 157 |
|
| 158 |
function Set (Path : String; |
| 159 |
Key : Config_Key; |
| 160 |
Value : String; |
| 161 |
Check : Check_Import := null) |
| 162 |
return Boolean |
| 163 |
is |
| 164 |
use AAA.Directories; |
| 165 |
|
| 166 |
Tmp : Replacer := New_Replacement (File => Path, |
| 167 |
Backup => False, |
| 168 |
Allow_No_Original => True); |
| 169 |
|
| 170 |
Table : TOML_Value := Load.Load_TOML_File (Tmp.Editable_Name); |
| 171 |
|
| 172 |
To_Add : constant TOML_Value := To_TOML_Value (Value); |
| 173 |
begin |
| 174 |
if To_Add.Is_Null then |
| 175 |
Trace.Error ("Invalid configuration value: '" & Value & "'"); |
| 176 |
return False; |
| 177 |
end if; |
| 178 |
|
| 179 |
if Check /= null and then not Check (Key, To_Add) then |
| 180 |
return False; |
| 181 |
end if; |
| 182 |
|
| 183 |
if Table.Is_Null then |
| 184 |
-- The configuration file doesn't exist or is not valid. Create an |
| 185 |
-- empty table. |
| 186 |
Table := TOML.Create_Table; |
| 187 |
end if; |
| 188 |
|
| 189 |
if not Add_In_Table (Table, Key, To_Add) then |
| 190 |
return False; |
| 191 |
end if; |
| 192 |
|
| 193 |
Write_Config_File (Table, Tmp.Editable_Name); |
| 194 |
|
| 195 |
Tmp.Replace; |
| 196 |
|
| 197 |
return True; |
| 198 |
end Set; |
| 199 |
|
| 200 |
end CLIC.Config.Edit; |