Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
195 lines (172 loc) · 5.24 KB
/
Copy pathProgram.cs
File metadata and controls
195 lines (172 loc) · 5.24 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using SQLiteEncrypted.ConsoleJournal;
using SQLitePCL;
return Run(args);
static int Run(string[] arguments)
{
try
{
if (arguments.Length == 0 || arguments[0] is "help" or "--help" or "-h")
{
PrintUsage();
return arguments.Length == 0 ? 1 : 0;
}
var activationKey = RequireEnvironment("SQLITE_ENCRYPTED_ACTIVATION_KEY");
var databaseKey = RequireEnvironment("SQLITE_ENCRYPTED_DATABASE_KEY");
var databasePath = Environment.GetEnvironmentVariable(
"SQLITE_ENCRYPTED_DATABASE_PATH");
if (string.IsNullOrWhiteSpace(databasePath))
{
databasePath = Path.GetFullPath("journal.db");
}
raw.SetProvider(new SQLite3Provider_e_sqlite3());
raw.FreezeProvider();
using var database = EncryptedDatabase.Open(
databasePath, activationKey, databaseKey);
var journal = new JournalRepository(database);
return arguments[0] switch
{
"add" => Add(journal, arguments),
"list" => List(journal, arguments),
"show" => Show(journal, arguments),
"search" => Search(journal, arguments),
"update" => Update(journal, arguments),
"delete" => Delete(journal, arguments),
_ => Unknown(arguments[0])
};
}
catch (Exception failure)
{
Console.Error.WriteLine($"Error: {failure.Message}");
return 1;
}
}
static int Add(JournalRepository journal, string[] arguments)
{
RequireArgumentCount(arguments, 3, "add <title> <body>");
var entry = journal.Add(RequireText(arguments[1], "title"),
RequireText(arguments[2], "body"));
Console.WriteLine($"Added journal entry {entry.Id}.");
return 0;
}
static int List(JournalRepository journal, string[] arguments)
{
RequireArgumentCount(arguments, 1, "list");
PrintEntries(journal.List());
return 0;
}
static int Search(JournalRepository journal, string[] arguments)
{
RequireArgumentCount(arguments, 2, "search <text>");
PrintEntries(journal.List(RequireText(arguments[1], "search text")));
return 0;
}
static int Show(JournalRepository journal, string[] arguments)
{
RequireArgumentCount(arguments, 2, "show <id>");
var id = ParseId(arguments[1]);
var entry = journal.Get(id);
if (entry is null)
{
Console.Error.WriteLine($"Journal entry {id} was not found.");
return 2;
}
PrintEntry(entry, includeBody: true);
return 0;
}
static int Update(JournalRepository journal, string[] arguments)
{
RequireArgumentCount(arguments, 4, "update <id> <title> <body>");
var id = ParseId(arguments[1]);
if (!journal.Update(id, RequireText(arguments[2], "title"),
RequireText(arguments[3], "body")))
{
Console.Error.WriteLine($"Journal entry {id} was not found.");
return 2;
}
Console.WriteLine($"Updated journal entry {id}.");
return 0;
}
static int Delete(JournalRepository journal, string[] arguments)
{
RequireArgumentCount(arguments, 2, "delete <id>");
var id = ParseId(arguments[1]);
if (!journal.Delete(id))
{
Console.Error.WriteLine($"Journal entry {id} was not found.");
return 2;
}
Console.WriteLine($"Deleted journal entry {id}.");
return 0;
}
static void PrintEntries(IReadOnlyList<JournalEntry> entries)
{
if (entries.Count == 0)
{
Console.WriteLine("No journal entries found.");
return;
}
foreach (var entry in entries)
{
PrintEntry(entry, includeBody: false);
}
}
static void PrintEntry(JournalEntry entry, bool includeBody)
{
Console.WriteLine($"{entry.Id,4} {entry.Title} [{entry.UpdatedAt}]");
if (includeBody)
{
Console.WriteLine(entry.Body);
}
}
static string RequireEnvironment(string name)
{
var value = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidOperationException($"{name} is required.");
}
return value;
}
static string RequireText(string value, string name)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException($"{name} must not be empty.");
}
return value.Trim();
}
static long ParseId(string value) =>
long.TryParse(value, out var id) && id > 0
? id
: throw new ArgumentException($"Invalid journal entry ID: {value}");
static void RequireArgumentCount(string[] arguments, int expected, string usage)
{
if (arguments.Length != expected)
{
throw new ArgumentException($"Usage: journal {usage}");
}
}
static int Unknown(string command)
{
Console.Error.WriteLine($"Unknown command: {command}");
PrintUsage();
return 1;
}
static void PrintUsage()
{
Console.WriteLine("""
SQLite.Encrypted raw API journal
Usage:
journal add <title> <body>
journal list
journal show <id>
journal search <text>
journal update <id> <title> <body>
journal delete <id>
Required environment variables:
SQLITE_ENCRYPTED_ACTIVATION_KEY
SQLITE_ENCRYPTED_DATABASE_KEY
Optional environment variable:
SQLITE_ENCRYPTED_DATABASE_PATH (defaults to ./journal.db)
""");
}