building the init command struct

This commit is contained in:
jmjatlanta 2016-10-26 21:13:32 -05:00
parent 1d324cf391
commit a38c3a8253
6 changed files with 65 additions and 16 deletions

View file

@ -7,9 +7,16 @@ int get_init_command(struct command* cmd) {
// arguments // arguments
// initialize an array of arguments with size of 1 // initialize an array of arguments with size of 1
struct argument argument; struct argument argument;
init_string_argument(&argument, "default-config", 0, 0, "Initialize with the given configuration"); int retVal = init_string_argument(&argument, "default-config", 0, 0, "Initialize with the given configuration");
argument.enable_stdin = 1; argument.enable_stdin = 1;
struct argument* array[1] = { &argument }; struct argument* array[] = { &argument };
cmd->arguments = array; cmd->arguments = array;
cmd->argument_count = 1;
return retVal;
}
int uninit_command(struct command* cmd) {
for(int i = 0; i < cmd->argument_count; i++)
uninit_argument(cmd->arguments[i]);
return 0; return 0;
} }

View file

@ -6,6 +6,18 @@
#include "../../commands/command.h" #include "../../commands/command.h"
/***
* Returns a command structure customized for the init command
* @param command the struct to fill
* @returns 0 on failure, otherwise 1
*/
int get_init_command(struct command* command); int get_init_command(struct command* command);
/***
* Uninitializes all the dynamic memory caused by get_init_command
* @param command the struct
* @returns 0 on failure, otherwise 1
*/
int uninit_command(struct command* command);
#endif #endif

View file

@ -1,21 +1,35 @@
#include <stdlib.h>
#include <string.h>
#include "argument.h" #include "argument.h"
int uninit_argument(struct argument* argument) {
free(argument->name);
return 1;
}
int init_argument(struct argument* argument, char* name, int required, int variadic, char* description) { int init_argument(struct argument* argument, char* name, int required, int variadic, char* description) {
argument->name = name == NULL ? NULL : strdup(name);
if (argument->name == NULL)
return 0;
strncpy(argument->name, name, strlen(name));
argument->name = name; argument->name = name;
argument->required = required; argument->required = required;
argument->variadic = variadic; argument->variadic = variadic;
argument->description = description; argument->description = description;
return 0; return 1;
} }
int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description) { int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description) {
init_argument(argument, name, required, variadic, description); int retVal = init_argument(argument, name, required, variadic, description);
if (retVal)
argument->type = string; argument->type = string;
return 0; return retVal;
} }
int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description) { int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description) {
init_argument(argument, name, required, variadic, description); int retVal = init_argument(argument, name, required, variadic, description);
if (retVal)
argument->type = file; argument->type = file;
return 0; return retVal;
} }

View file

@ -15,6 +15,7 @@ struct argument {
}; };
int init_argument(struct argument* argument, char* name, int required, int variadic, char* description); int init_argument(struct argument* argument, char* name, int required, int variadic, char* description);
int uninit_argument(struct argument* argument);
int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description); int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description);
int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description); int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description);

View file

@ -23,6 +23,7 @@ struct help_text {
struct command { struct command {
//struct option* options; //struct option* options;
struct argument** arguments; struct argument** arguments;
int argument_count;
//int (*pre_run)(struct request*); //int (*pre_run)(struct request*);
//int (*run)(struct request*); //int (*run)(struct request*);
//int (*post_run)(struct request*); //int (*post_run)(struct request*);

View file

@ -5,31 +5,45 @@
#define __TEST_INIT_H__ #define __TEST_INIT_H__
#include "../../../cmd/ipfs/init.h" #include "../../../cmd/ipfs/init.h"
#include "../../../commands/argument.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int test_get_init_command() { int test_get_init_command() {
struct command cmd = { 0 }; struct command cmd = { 0 };
int retVal = 1;
// make sure its empty // make sure its empty
if (cmd.help_text.tagline != NULL) { if (cmd.help_text.tagline != NULL) {
fprintf(stderr, "short description should be null\n"); fprintf(stderr, "short description should be null\n");
return 0; return 0;
} }
// grab the stuff // grab the stuff
get_init_command(&cmd); retVal = get_init_command(&cmd);
if (!retVal) {
fprintf(stderr, "Function call to get_init_command not successful. Return was %d\n", retVal);
return retVal;
}
// make sure its right // make sure its right
if (cmd.help_text.tagline == NULL) { if (cmd.help_text.tagline == NULL) {
fprintf(stderr, "short description is null\n"); fprintf(stderr, "short description is null\n");
return 0; retVal = 0;
} } else if (strcmp(cmd.help_text.tagline, "Initializes IPFS config file.") != 0) {
if (strcmp(cmd.help_text.tagline, "Initializes IPFS config file.") != 0) {
fprintf(stderr, "short description is not null\n"); fprintf(stderr, "short description is not null\n");
return 0; retVal = 0;
} else if (cmd.argument_count != 1) {
fprintf(stderr, "argument count should be 1");
retVal = 0;
} else {
struct argument arg1 = *(cmd.arguments[0]);
if (strncmp(arg1.name, "default-config", 14) != 0) {
fprintf(stderr, "arg1 wrong name. Expected %s but got %s\n", "default_config", arg1.name);
retVal = 0;
} }
return 1; }
uninit_command(&cmd);
return retVal;
} }
#endif #endif