ada_keystore_c8fa1d94/tools/akt-configs.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
-----------------------------------------------------------------------
--  akt-configs -- Configuration
--  Copyright (C) 2019, 2021, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  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.
-----------------------------------------------------------------------
with Ada.Environment_Variables;
with Ada.Directories;
with Ada.Strings.Unbounded;
with Interfaces.C.Strings;
with Util.Files;
with Util.Log.Loggers;
with Util.Systems.Os;
with Util.Beans.Objects;
with Util.Properties;
package body AKT.Configs is

   use Ada.Strings.Unbounded;
   package UBO renames Util.Beans.Objects;

   --  The logger
   Log   : constant Util.Log.Loggers.Logger := Util.Log.Loggers.Create ("AKT.Configs");

   function Get_Default_Path return String;

   Cfg      : Util.Properties.Manager;
   Cfg_Path : Ada.Strings.Unbounded.Unbounded_String;

   --  ------------------------------
   --  Get the default configuration path.
   --  ------------------------------
   function Get_Default_Path return String is
   begin
      if not Ada.Environment_Variables.Exists ("HOME") then
         return "akt.properties";
      end if;
      declare
         Home : constant String := Ada.Environment_Variables.Value ("HOME");
      begin
         return Util.Files.Compose (Home, ".config/akt/akt.properties");
      end;
   end Get_Default_Path;

   --  ------------------------------
   --  Get the directory which contains the named keys.
   --  ------------------------------
   function Get_Directory_Key_Path return String is
      Val : constant UBO.Object := Cfg.Get_Value (NAMED_KEY_DIR);
   begin
      if not UBO.Is_Null (Val) then
         return UBO.To_String (Val);
      end if;
      if not Ada.Environment_Variables.Exists ("HOME") then
         return "";
      end if;
      declare
         Home : constant String := Ada.Environment_Variables.Value ("HOME");
      begin
         return Util.Files.Compose (Home, ".config/akt/keys");
      end;
   end Get_Directory_Key_Path;

   --  ------------------------------
   --  Initialize the configuration.
   --  ------------------------------
   procedure Initialize (Path : in String) is
      Def_Path : constant String := Get_Default_Path;
   begin
      if Path'Length > 0 and then Ada.Directories.Exists (Path) then
         Log.Info ("Loading configuration {0}", Path);

         Cfg.Load_Properties (Path);
         Cfg_Path := Ada.Strings.Unbounded.To_Unbounded_String (Path);
      elsif Ada.Directories.Exists (Def_Path) then
         Log.Info ("Loading user global configuration {0}", Def_Path);

         Cfg.Load_Properties (Def_Path);
         Cfg_Path := Ada.Strings.Unbounded.To_Unbounded_String (Def_Path);
      end if;

      if Path'Length > 0 then
         Cfg_Path := Ada.Strings.Unbounded.To_Unbounded_String (Path);
      end if;
   end Initialize;

   --  ------------------------------
   --  Save the configuration.
   --  ------------------------------
   procedure Save is
      Path : constant String
        := (if Length (Cfg_Path) = 0 then Get_Default_Path else To_String (Cfg_Path));
      Dir  : constant String := Ada.Directories.Containing_Directory (Path);
      P    : Interfaces.C.Strings.chars_ptr;
   begin
      Log.Info ("Saving configuration {0}", Path);

      if not Ada.Directories.Exists (Path) then
         Ada.Directories.Create_Path (Dir);
         P := Interfaces.C.Strings.New_String (Dir);
         if Util.Systems.Os.Sys_Chmod (P, 8#0700#) /= 0 then
            Log.Error (-("cannot set the permission of {0}"), Dir);
         end if;
         Interfaces.C.Strings.Free (P);
      end if;
      Cfg.Save_Properties (Path);

      --  Set the permission on the file to allow only the user to read/write that file.
      P := Interfaces.C.Strings.New_String (Path);
      if Util.Systems.Os.Sys_Chmod (P, 8#0600#) /= 0 then
         Log.Error (-("cannot set the permission of {0}"), Path);
      end if;
      Interfaces.C.Strings.Free (P);
   end Save;

   --  ------------------------------
   --  Get the configuration parameter.
   --  ------------------------------
   function Get (Name : in String) return String is
   begin
      return Cfg.Get (Name);
   end Get;

   --  ------------------------------
   --  Set the configuration parameter.
   --  ------------------------------
   procedure Set (Name  : in String;
                  Value : in String) is
   begin
      Cfg.Set (Name, Value);
   end Set;

   --  ------------------------------
   --  Returns true if the configuration parameter is defined.
   --  ------------------------------
   function Exists (Name : in String) return Boolean is
   begin
      return Cfg.Exists (Name);
   end Exists;

end AKT.Configs;