forked from jackburton79/inventory-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (83 loc) · 2.18 KB
/
main.cpp
File metadata and controls
99 lines (83 loc) · 2.18 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
/*
* main.cpp
*
* Created on: 11/lug/2013
* Author: stefano
*/
#include "Agent.h"
#include "Configuration.h"
#include <cstdlib>
#include <cstring>
#include <getopt.h>
#include <iostream>
extern const char* __progname;
static
struct option sLongOptions[] = {
{ "conf", required_argument, 0, 'c' },
{ "server", required_argument, 0, 's' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
static void
PrintHelpAndExit()
{
std::cout << __progname << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << "-h [--help] : Print usage" << std::endl;
std::cout << "-c [--conf] : Specify configuration file" << std::endl;
std::cout << "-s [--server] : Specify OCSInventory server url" << std::endl;
std::cout << "If no server is specified, either via the -s option or via the" << std::endl;
std::cout << "configuration file (option -c), the program will write a local" << std::endl;
std::cout << "inventory in the current working directory." << std::endl;
std::cout << "Examples:" << std::endl;
std::cout << " " << __progname;
std::cout << " --conf /etc/ocsinventory-ng.conf" << std::endl;
std::cout << " " << __progname;
std::cout << " --server http://ocsinventory-ng/ocsinventory" << std::endl;
::exit(0);
}
int
main(int argc, char **argv)
{
char* configFile = NULL;
char* serverUrl = NULL;
int optIndex = 0;
int c = 0;
while ((c = ::getopt_long(argc, argv, "c:s:h",
sLongOptions, &optIndex)) != -1) {
switch (c) {
case 'c':
configFile = optarg;
break;
case 's':
serverUrl = optarg;
break;
case 'h':
PrintHelpAndExit();
break;
}
}
if (configFile != NULL)
Configuration::Get()->Load(configFile);
else if (serverUrl)
Configuration::Get()->SetServer(serverUrl);
try {
Agent agent;
agent.Run();
} catch (std::string& errorString) {
std::cerr << errorString << std::endl;
return 1;
} catch (const char* string) {
std::cerr << string << std::endl;
return 1;
} catch (int error) {
std::cerr << strerror(error) << std::endl;
return 1;
} catch (...) {
std::cerr << "Unhandled exception." << std::endl;
return 1;
}
if (configFile != NULL)
Configuration::Get()->Save();
return 0;
}