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 | -----------------------------------------------------------------------
-- akt-commands-store -- Store content read from standard input in keystore
-- Copyright (C) 2019, 2021, 2022, 2023 Stephane Carrez
-- Written by Stephane Carrez (Stephane.Carrez@gmail.com)
-- SPDX-License-Identifier: Apache-2.0
-----------------------------------------------------------------------
with Ada.Directories;
with Ada.Streams.Stream_IO;
with Util.Systems.Os;
with Util.Streams.Raw;
with Util.Streams.Files;
with Keystore.Tools;
package body AKT.Commands.Store is
use Ada.Directories;
-- ------------------------------
-- Insert a new value in the keystore.
-- ------------------------------
overriding
procedure Execute (Command : in out Command_Type;
Name : in String;
Args : in Argument_List'Class;
Context : in out Context_Type) is
pragma Unreferenced (Name);
function Accept_File (Ent : in Keystore.Tools.Directory_Entry_Type) return Boolean;
procedure Insert_File (Name : in String);
procedure Insert_Directory (Name : in String);
procedure Insert_Standard_Input (Name : in String);
function Accept_File (Ent : in Keystore.Tools.Directory_Entry_Type) return Boolean is
begin
Context.Console.Notice (N_INFO, Ada.Directories.Full_Name (Ent));
return True;
end Accept_File;
procedure Insert_File (Name : in String) is
File : Util.Streams.Files.File_Stream;
begin
File.Open (Mode => Ada.Streams.Stream_IO.In_File,
Name => Name);
Context.Wallet.Set (Name => Ada.Directories.Simple_Name (Name),
Kind => Keystore.T_FILE,
Input => File);
end Insert_File;
procedure Insert_Directory (Name : in String) is
begin
Keystore.Tools.Store (Wallet => Context.Wallet,
Path => Name,
Prefix => Ada.Directories.Simple_Name (Name) & '/',
Pattern => "*",
Filter => Accept_File'Access);
end Insert_Directory;
procedure Insert_Standard_Input (Name : in String) is
Input : Util.Streams.Raw.Raw_Stream;
begin
Input.Initialize (File => Util.Systems.Os.STDIN_FILENO);
Context.Wallet.Set (Name => Name,
Kind => Keystore.T_BINARY,
Input => Input);
end Insert_Standard_Input;
begin
-- Open keystore with workers because we expect possibly big data.
Context.Open_Keystore (Args, Use_Worker => True);
if Command.Use_Stdin then
if Context.First_Arg > Args.Get_Count then
AKT.Commands.Log.Error (-("missing name to store the standard input"));
raise Error;
end if;
Insert_Standard_Input (Args.Get_Argument (Context.First_Arg));
else
for I in Context.First_Arg .. Args.Get_Count loop
declare
Name : constant String := Args.Get_Argument (I);
begin
if not Ada.Directories.Exists (Name) then
AKT.Commands.Log.Error (-("'{0}' does not exist"), Name);
raise Error;
elsif Ada.Directories.Kind (Name) = Ada.Directories.Ordinary_File then
Insert_File (Name);
elsif Ada.Directories.Kind (Name) = Ada.Directories.Directory then
Insert_Directory (Name);
else
AKT.Commands.Log.Error (-("'{0}' is not a regular file nor a directory"), Name);
raise Error;
end if;
end;
end loop;
end if;
end Execute;
-- ------------------------------
-- Setup the command before parsing the arguments and executing it.
-- ------------------------------
overriding
procedure Setup (Command : in out Command_Type;
Config : in out GNAT.Command_Line.Command_Line_Configuration;
Context : in out Context_Type) is
package GC renames GNAT.Command_Line;
begin
Drivers.Command_Type (Command).Setup (Config, Context);
GC.Define_Switch (Config => Config,
Output => Command.Use_Stdin'Access,
Switch => "--",
Help => -("Use the standard input to read the content"));
end Setup;
end AKT.Commands.Store;
|