source: appstream-generator/src/app.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 4.0 KB
Line 
1/*
2 * Copyright (C) 2016 Matthias Klumpp <matthias@tenstral.net>
3 *
4 * Licensed under the GNU Lesser General Public License Version 3
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the license, or
9 * (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20import std.stdio;
21import std.path : getcwd, buildPath;
22import std.getopt;
23import std.string : format;
24import core.stdc.stdlib;
25
26import asgen.logging;
27import asgen.config;
28import asgen.engine;
29
30
31private immutable helpText =
32"Usage:
33  appstream-generator <subcommand> [OPTION...] - AppStream Generator.
34
35AppStream Metadata Generator
36
37Subcommands:
38  process SUITE      - Process new metadata for the given distribution suite.
39  cleanup            - Cleanup old metadata and media files.
40  remove-found SUITE - Drop all valid processed metadata and hints.
41  forget PKID        - Drop all information we have about this (partial) package-id.
42
43Help Options:
44  -h, --help       Show help options
45
46Application Options:
47  --version        Show the program version.
48  --verbose        Show extra debugging information.
49  --force          Force action.
50  -w|--workspace   Define the workspace location.";
51
52void main(string[] args)
53{
54    string command;
55    bool verbose;
56    bool showHelp;
57    bool showVersion;
58    bool forceAction;
59    string wdir = getcwd ();
60
61    // parse command-line options
62    try {
63        getopt (args,
64            "help|h", &showHelp,
65            "verbose", &verbose,
66            "version", &showVersion,
67            "force", &forceAction,
68            "workspace|w", &wdir);
69    } catch (Exception e) {
70        writeln ("Unable to parse parameters: ", e.msg);
71        exit (1);
72    }
73
74    if (showHelp) {
75        writeln (helpText);
76        return;
77    }
78
79    if (showVersion) {
80        writeln ("Generator version: ", asgen.config.generatorVersion);
81        return;
82    }
83
84    if (args.length < 2) {
85        writeln ("No subcommand specified!");
86        return;
87    }
88
89    auto conf = Config.get ();
90    try {
91        conf.loadFromFile (buildPath (wdir, "asgen-config.json"));
92    } catch (Exception e) {
93        writefln ("Unable to load configuration: %s", e.msg);
94        exit (4);
95    }
96    scope (exit) {
97        import std.file;
98        if (exists (conf.getTmpDir ()))
99            rmdirRecurse (conf.getTmpDir ());
100    }
101
102    // globally enable verbose mode, if requested
103    if (verbose) {
104        asgen.logging.setVerbose (true);
105    }
106
107    auto engine = new Engine ();
108    engine.forced = forceAction;
109
110    command = args[1];
111    switch (command) {
112        case "run":
113        case "process":
114            if (args.length != 3) {
115                writeln ("Invalid number of parameters: You need to specify a suite name.");
116                exit (1);
117            }
118            engine.run (args[2]);
119            break;
120        case "cleanup":
121            engine.runCleanup ();
122            break;
123        case "remove-found":
124            if (args.length != 3) {
125                writeln ("Invalid number of parameters: You need to specify a suite name.");
126                exit (1);
127            }
128            engine.removeHintsComponents (args[2]);
129            break;
130        case "forget":
131            if (args.length != 3) {
132                writeln ("Invalid number of parameters: You need to specify a package-id (partial IDs are allowed).");
133                exit (1);
134            }
135            engine.forgetPackage (args[2]);
136            break;
137        default:
138            writeln (format ("The command '%s' is unknown.", command));
139            exit (1);
140            break;
141    }
142}
Note: See TracBrowser for help on using the repository browser.