-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (74 loc) · 2.31 KB
/
main.cpp
File metadata and controls
89 lines (74 loc) · 2.31 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
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include "cdocker.h"
#include "cphprunner.h"
#include "globals.h"
#include "utils.h"
extern "C" {
#include <libgen.h>
#include <limits.h>
#include <sys/stat.h>
}
void do_help(char *progName)
{
std::cout << "Usage: " << progName << " [OPTIONS]" << std::endl;
std::cout << "Docker runner." << std::endl;
std::cout << std::endl;
std::cout << "Mandatory arguments to long options are mandatory for short options too." << std::endl;
std::cout << " -h, --help this screen" << std::endl;
std::cout << " -v, --version api version only" << std::endl;
std::cout << std::endl;
std::cout << "{ \"author\": \"" AUTHOR "\", \"version\": \"" VERSION_API "\" };" << std::endl;
}
void handle_getopt(int argc, char **argv)
{
if (argc != 2)
return;
char resolved_path[PATH_MAX];
struct stat sb_original{};
struct stat sb_realpath{};
memset(resolved_path, 0, PATH_MAX);
if (lstat(argv[0], &sb_original) == -1) {
return;
};
if (!realpath(argv[0], resolved_path)) {
perror("realpath");
exit(EXIT_FAILURE);
}
if (stat(argv[0], &sb_realpath) == -1) {
return;
};
if (sb_original.st_ino != sb_realpath.st_ino) {
// show help only if is called directly
return;
}
// we dont need getopt for the 2 options here :)
if (strncmp(argv[1],"-h",2) == 0) {
do_help(argv[0]);
exit(EXIT_SUCCESS);
}
if (strncmp(argv[1],"-v",2) == 0) {
std::cout << VERSION_API << std::endl;
exit(EXIT_SUCCESS);
}
}
int main(int argc, char **argv)
{
handle_getopt(argc, argv);
std::vector<std::string> newargs;
std::string dockerHostname("-");
int selected_version_int = 0;
CDocker Docker;
CPHPRunner PHPRunner;
PHPRunner.setDefaultVersion();
PHPRunner.selectPHPVersion(argv[0]);
PHPRunner.buildPHParguments(argc, argv);
PHPRunner.getArgs(&newargs);
PHPRunner.getSelectedVersion(&dockerHostname);
selected_version_int = PHPRunner.getSelectedVersion();
load_environment_file();
return Docker.run(dockerHostname, selected_version_int, newargs);
}