-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-main.adb
148 lines (136 loc) · 4.1 KB
/
db-main.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
with Ada.Text_IO;
with Ada.Command_Line;
use Ada.Command_Line;
with Ada.Environment_Variables;
with DB.Repository;
package body DB.Main is
procedure Main is
begin
if Argument_Count < 1 then
Help;
Set_Exit_Status(Failure);
elsif Argument(1) = "--help" or Argument(1) = "-h" or
Argument(1) = "help" or Argument(1) = "/?" or
Argument(1) = "-?" then
Help;
elsif Argument(1) = "-l" or Argument(1) = "list" then
Parse_And_Run(Action_List);
elsif (Argument(1) = "-g" or Argument(1) = "get")
and not (Argument_Count < 2) then
Parse_And_Run(Action_Get);
else
Error_Help("Unknown command: " & Argument(1));
end if;
end Main;
procedure Help is
use Ada.Text_IO;
begin
Put_Line("Bupstash Extraction Tool 1.1.1, " &
"(c) 2021-2023 Ma_Sys.ma <info@masysma.net>");
New_Line;
Put_Line("USAGE " & Command_Name & " --help");
Put_Line(" Display help screen.");
New_Line;
Put_Line("USAGE " & Command_Name &
" -l|list [-k KEY] [-r REPO]");
Put_Line(" List backups.");
New_Line;
Put_Line("USAGE " & Command_Name &
" -g|get [-k KEY] [-r REPO] -i ID");
Put_Line(" Restore backup. Append `| tar -x`.");
New_Line;
Put_Line("Environment variable BUPSTASH_KEY can supply " &
"the value for KEY.");
Put_Line("Environment variable BUPSTASH_REPOSITORY can " &
"supply the value for REPO.");
New_Line;
Put_Line("Arguments take precedence over environment.");
end Help;
procedure Parse_And_Run(Action: in Run_Action) is
Parameter_Not_Set: constant String := "";
-- This recursive parsing allows us to avoid copying to a
-- bounded string. Rather, we keep the strings constant at all
-- times by recursing with the changed values.
procedure Parse_And_Run_Recursive(I: in Positive;
Key_File: in String;
Repo_Directory: in String;
Selected_ID: in String) is
AC: constant Integer := Argument_Count;
Has_Next_Arg: constant Boolean := I < AC;
No_More_Args: constant Boolean := I > AC;
begin
if Has_Next_Arg and then Argument(I)'Length = 2 then
case Argument(I)(2) is
when 'k' => Parse_And_Run_Recursive(I + 2,
Argument(I + 1),
Repo_Directory,
Selected_ID);
when 'r' => Parse_And_Run_Recursive(I + 2,
Key_File,
Argument(I + 1),
Selected_ID);
when 'i' => Parse_And_Run_Recursive(I + 2,
Key_File,
Repo_Directory,
Argument(I + 1));
when others => Error_Help("Unknown argument: " &
Argument(I));
end case;
elsif No_More_Args then
if Key_File = Parameter_Not_Set or
Repo_Directory =
Parameter_Not_Set then
Error_Help("KEY or REPO missing: KEY=" &
Key_File & ", REPO=" &
Repo_Directory);
return;
end if;
case Action is
when Action_List =>
Run_List(Key_File, Repo_Directory);
when Action_Get =>
if Selected_ID = Parameter_Not_Set then
Error_Help("Missing ID.");
else
Run_Get(Key_File,
Repo_Directory,
Selected_ID);
end if;
end case;
else
Error_Help("Unknown argument: " & Argument(I));
end if;
end Parse_And_Run_Recursive;
begin
Parse_And_Run_Recursive(
I => 2,
Key_File => Ada.Environment_Variables.Value(
"BUPSTASH_KEY", Parameter_Not_Set),
Repo_Directory => Ada.Environment_Variables.Value(
"BUPSTASH_REPOSITORY", Parameter_Not_Set),
Selected_ID => Parameter_Not_Set
);
end Parse_And_Run;
procedure Error_Help(Message: in String) is
Line: constant String := "ERROR: " & Message;
begin
Ada.Text_IO.Put_Line(Line);
Ada.Text_IO.New_Line;
Help;
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line(Line);
end Error_Help;
procedure Run_List(Key_File: in String; Repo_Directory: in String) is
Repository: constant DB.Repository.Repository :=
DB.Repository.Init(Key_File, Repo_Directory);
begin
Repository.Print_Info;
end Run_List;
procedure Run_Get(Key_File: in String; Repo_Directory: in String;
Selected_ID: in String) is
Repository: constant DB.Repository.Repository :=
DB.Repository.Init(Key_File, Repo_Directory);
begin
Repository.Restore(Selected_ID);
end Run_Get;
end DB.Main;