-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.d
80 lines (68 loc) · 1.98 KB
/
update.d
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
/+dub.sdl:
name "update"
dependency "mimeapps" path="../"
+/
import std.stdio;
import std.getopt;
import std.string;
import std.path;
import mimeapps;
import isfreedesktop;
int assocFormatError()
{
stderr.writeln("Association must be given in form mimetype:desktopId");
return 1;
}
int main(string[] args)
{
string fileName;
string[] toAdd;
string[] toRemove;
string[] toSetDefault;
bool forced;
getopt(args, "file", "Input file to update", &fileName,
"add", "Add association", &toAdd,
"remove", "Remove association", &toRemove,
"default", "Set default application", &toSetDefault,
"force", "Force changing current user override mimeapps.list", &forced
);
if (fileName.empty) {
stderr.writeln("No input file given");
return 1;
}
if (toAdd.empty && toRemove.empty && toSetDefault.empty) {
stderr.writeln("No update operations given");
return 1;
}
AssociationUpdateQuery query;
foreach(str; toRemove) {
auto splitted = str.findSplit(":");
if (splitted[1].empty) {
return assocFormatError();
}
query.removeAssociation(splitted[0], splitted[2]);
}
foreach(str; toAdd) {
auto splitted = str.findSplit(":");
if (splitted[1].empty) {
return assocFormatError();
}
query.addAssociation(splitted[0], splitted[2]);
}
foreach(str; toSetDefault) {
auto splitted = str.findSplit(":");
if (splitted[1].empty) {
return assocFormatError();
}
query.setDefaultApplication(splitted[0], splitted[2]);
}
static if (isFreedesktop) {
auto mimeAppsLists = mimeAppsListPaths();
if (mimeAppsLists.canFind(buildNormalizedPath(fileName)) && !forced) {
stderr.writeln("Cowardly refusing to update a system file. Make a copy in other path.");
return 1;
}
}
updateAssociations(fileName, query);
return 0;
}