25 #include "serialize.h" 28 void parseArguments(
MonkState* state,
int argc,
char** argv,
int* fileOptInd) {
30 static struct option long_options[] = {{
"config", required_argument, 0,
'c'},
31 {
"userID", required_argument, 0,
'u'},
32 {
"groupID", required_argument, 0,
'g'},
33 {
"scheduler_start", no_argument, 0,
'S'},
34 {
"jobId", required_argument, 0,
'j'},
37 while ((c = getopt_long(argc, argv,
"VvJhIs:k:c:", long_options, &option_index)) != -1) {
46 state->scanMode = MODE_SCHEDULER;
53 printf(AGENT_NAME
" version " VERSION_S
" r(" COMMIT_HASH_S
")\n");
55 printf(AGENT_NAME
" (no version available)\n");
63 state->scanMode = MODE_EXPORT_KOWLEDGEBASE;
64 state->knowledgebaseFile = optarg;
67 state->scanMode = MODE_CLI_OFFLINE;
68 state->knowledgebaseFile = optarg;
71 state->ignoreFilesWithMimeType =
true;
76 "\nAs CLI tool using the licenses from the FOSSology database:\n");
77 printf(
" %s [options] file [file [...]]\n", argv[0]);
79 " -h :: help (print this message), then exit.\n" 80 " -c config :: specify the directory for the system configuration.\n" 81 " -v :: verbose output.\n" 82 " -J :: JSON output.\n" 83 " file :: scan file and print licenses detected within it.\n" 84 " -V :: print the version info, then exit.\n" 85 "\nSave knowledgebase to knowledgebaseFile for offline usage without db:\n");
86 printf(
" %s [options] -s knowledgebaseFile\n", argv[0]);
88 " -c config :: specify the directory for the system configuration.\n" 89 "\nUse previously saved knowledgebaseFile for offline usage without db.\n");
90 printf(
" %s -k knowledgebaseFile [options] file [file [...]]\n", argv[0]);
92 " -J :: JSON output.\n" 93 " file :: scan file and print licenses detected within it.\n" 94 "\nThe following should only be called by the FOSSology scheduler:\n");
95 printf(
" %s --scheduler_start [options]\n", argv[0]);
97 " -c config :: specify the directory for the system configuration.\n" 98 " --userID i :: the id of the user that created the job\n" 99 " --groupID i :: the id of the group of the user that created the job\n" 100 " --jobID i :: the id of the job\n");
105 *fileOptInd = optind;
106 if (optind < argc && state->scanMode != MODE_CLI_OFFLINE) {
107 state->scanMode = MODE_CLI;
109 if((state->scanMode == MODE_CLI_OFFLINE ||
110 state->scanMode == MODE_EXPORT_KOWLEDGEBASE) &&
111 state->knowledgebaseFile == NULL) {
112 fprintf( stderr,
"necessary path to knowledgebase file not provided\n");
117 int main(
int argc,
char** argv) {
120 MonkState stateStore = { .dbManager = NULL,
124 .knowledgebaseFile = NULL,
126 .ignoreFilesWithMimeType =
false,
129 parseArguments(state, argc, argv, &fileOptInd);
130 int wasSuccessful = 1;
132 if (state->scanMode == 0) {
137 if (state->scanMode != MODE_CLI_OFFLINE) {
140 fileOptInd = fileOptInd - oldArgc + argc;
142 PGresult* licensesResult = queryAllLicenses(state->dbManager);
143 licenses = extractLicenses(state->dbManager, licensesResult, MIN_ADJACENT_MATCHES, MAX_LEADING_DIFF);
144 PQclear(licensesResult);
146 licenses = deserializeFromFile(state->knowledgebaseFile, MIN_ADJACENT_MATCHES, MAX_LEADING_DIFF);
149 if (state->scanMode == MODE_SCHEDULER) {
150 wasSuccessful = handleSchedulerMode(state, licenses);
151 scheduler_disconnect(state, ! wasSuccessful);
152 }
else if (state->scanMode == MODE_CLI ||
153 state->scanMode == MODE_CLI_OFFLINE) {
155 if (state->dbManager != NULL) {
156 scheduler_disconnect(state, 0);
158 state->dbManager = NULL;
160 wasSuccessful = handleCliMode(state, licenses, argc, argv, fileOptInd);
161 }
else if (state->scanMode == MODE_EXPORT_KOWLEDGEBASE) {
162 printf(
"Write knowledgebase to %s\n", state->knowledgebaseFile);
163 wasSuccessful = serializeToFile(licenses, state->knowledgebaseFile);
166 licenses_free(licenses);
168 return ! wasSuccessful;
void bail(int exitval)
Disconnect with scheduler returning an error code and exit.
void fo_scheduler_connect_dbMan(int *argc, char **argv, fo_dbManager **dbManager)
Make a connection from an agent to the scheduler and create a DB manager as well. ...