forked from agorise/c-ipfs
First stab at ipfs init
This commit is contained in:
parent
fcda3e83ce
commit
689408ffe9
20 changed files with 342 additions and 0 deletions
9
Makefile
Normal file
9
Makefile
Normal file
|
@ -0,0 +1,9 @@
|
|||
all:
|
||||
cd repo; make all;
|
||||
cd cmd; make all;
|
||||
cd test; make all;
|
||||
|
||||
clean:
|
||||
cd repo; make clean;
|
||||
cd cmd; make clean;
|
||||
cd test; make clean;
|
5
cmd/Makefile
Normal file
5
cmd/Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
all:
|
||||
cd ipfs; make all;
|
||||
|
||||
clean:
|
||||
cd ipfs; make clean;
|
16
cmd/ipfs/Makefile
Normal file
16
cmd/ipfs/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
CC = gcc
|
||||
CFLAGS = -O0
|
||||
LFLAGS =
|
||||
DEPS = init.h
|
||||
OBJS = init.o main.o ../../commands/argument.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
ipfs: $(OBJS)
|
||||
$(CC) -o $@ $^ $(LFLAGS)
|
||||
|
||||
all: ipfs
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
15
cmd/ipfs/init.c
Normal file
15
cmd/ipfs/init.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "init.h"
|
||||
|
||||
int get_init_command(struct command* cmd) {
|
||||
// help text
|
||||
cmd->help_text.tagline = "Initializes IPFS config file.";
|
||||
cmd->help_text.short_description = "\nInitializes IPFS configuration files and generates a new keypair.\n\nipfs uses a repository in the local file system. By default, the repo is\nlocated at ~/.ipfs. To change the repo location, set the $IPFS_PATH\nenvironment variable.:\n\n export IPFS_PATH=/path/to/ipfsrepo";
|
||||
// arguments
|
||||
// initialize an array of arguments with size of 1
|
||||
struct argument argument;
|
||||
init_string_argument(&argument, "default-config", 0, 0, "Initialize with the given configuration");
|
||||
argument.enable_stdin = 1;
|
||||
struct argument* array[1] = { &argument };
|
||||
cmd->arguments = array;
|
||||
return 0;
|
||||
}
|
11
cmd/ipfs/init.h
Normal file
11
cmd/ipfs/init.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
* Initialize an IPFS repository
|
||||
*/
|
||||
#ifndef __CMD_IPFS_INIT_H__
|
||||
#define __CMD_IPFS_INIT_H__
|
||||
|
||||
#include "../../commands/command.h"
|
||||
|
||||
int get_init_command(struct command* command);
|
||||
|
||||
#endif
|
BIN
cmd/ipfs/ipfs
Executable file
BIN
cmd/ipfs/ipfs
Executable file
Binary file not shown.
32
cmd/ipfs/main.c
Normal file
32
cmd/ipfs/main.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void print_help(int longHelp, FILE* outStream) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* command line interface to IPFS.
|
||||
* Steps:
|
||||
* 1) parse the command line
|
||||
* 2) if user requests help, display then exit
|
||||
* 3) run the command
|
||||
* 4) output the response
|
||||
* 5) if anything fails, print the error, maybe with help
|
||||
*/
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
if (argc == 2) {
|
||||
if (strncmp(argv[1], "help", 4) == 0) {
|
||||
print_help(1, stdout);
|
||||
exit(0);
|
||||
} else if (strncmp(argv[1], "--version", 9) == 0) {
|
||||
argv[1] = "version";
|
||||
}
|
||||
} // end of help
|
||||
|
||||
// parse command line into an invocation
|
||||
// exit
|
||||
exit(0);
|
||||
}
|
21
commands/argument.c
Normal file
21
commands/argument.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "argument.h"
|
||||
|
||||
int init_argument(struct argument* argument, char* name, int required, int variadic, char* description) {
|
||||
argument->name = name;
|
||||
argument->required = required;
|
||||
argument->variadic = variadic;
|
||||
argument->description = description;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init_string_argument(struct argument* argument, char* name, int required, int variadic, char* description) {
|
||||
init_argument(argument, name, required, variadic, description);
|
||||
argument->type = string;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init_file_argument(struct argument* argument, char* name, int required, int variadic, char* description) {
|
||||
init_argument(argument, name, required, variadic, description);
|
||||
argument->type = file;
|
||||
return 0;
|
||||
}
|
21
commands/argument.h
Normal file
21
commands/argument.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef __COMMANDS_ARGUMENT_H__
|
||||
#define __COMMANDS_ARGUMENT_H__
|
||||
|
||||
enum argument_type { string, file };
|
||||
|
||||
struct argument {
|
||||
char* name;
|
||||
enum argument_type type;
|
||||
int required;
|
||||
int variadic;
|
||||
int supports_stdin;
|
||||
int recursive;
|
||||
char* description;
|
||||
int enable_stdin;
|
||||
};
|
||||
|
||||
int init_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);
|
||||
|
||||
#endif
|
40
commands/command.h
Normal file
40
commands/command.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/***
|
||||
* The structures to commands
|
||||
*/
|
||||
|
||||
#ifndef __COMMANDS_COMMAND_H__
|
||||
#define __COMMANDS_COMMAND_H__
|
||||
|
||||
#include "argument.h"
|
||||
|
||||
struct help_text {
|
||||
char* tagline;
|
||||
char* short_description;
|
||||
char** synopsis_options_values;
|
||||
|
||||
// optional
|
||||
char* usage;
|
||||
char* long_description;
|
||||
char* options;
|
||||
char* subcommands;
|
||||
char* synopsis;
|
||||
};
|
||||
|
||||
struct command {
|
||||
//struct option* options;
|
||||
struct argument** arguments;
|
||||
//int (*pre_run)(struct request*);
|
||||
//int (*run)(struct request*);
|
||||
//int (*post_run)(struct request*);
|
||||
//struct marshaller** marshallers;
|
||||
struct help_text help_text;
|
||||
|
||||
// a boolean to determine if this is an external
|
||||
// binary.
|
||||
int external;
|
||||
|
||||
//struct type return_type;
|
||||
char** subcommands;
|
||||
};
|
||||
|
||||
#endif // command.h
|
12
repo/Makefile
Normal file
12
repo/Makefile
Normal file
|
@ -0,0 +1,12 @@
|
|||
CC = gcc
|
||||
CFLAGS = -O0
|
||||
DEPS = repo.h
|
||||
OBJS = repo.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
all: $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
22
repo/config/config.h
Normal file
22
repo/config/config.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef __CONFIG_H__
|
||||
#define __CONFIG_H__
|
||||
|
||||
#include "datastore.h"
|
||||
|
||||
struct config {
|
||||
//struct identity identity;
|
||||
struct datastore datastore;
|
||||
//struct address* addresses;
|
||||
//struct mount* mounts;
|
||||
//struct discovery discovery;
|
||||
//struct ipns ipns;
|
||||
//struct bootstrap* peer_addresses;
|
||||
//struct tour tour;
|
||||
//struct gateway gateway;
|
||||
//struct supernode_routing supernode_client_config;
|
||||
//struct api api;
|
||||
//struct swarm swarm;
|
||||
//struct reprovider reprovider;
|
||||
};
|
||||
|
||||
#endif
|
21
repo/config/datastore.h
Normal file
21
repo/config/datastore.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef __DATASTORE_H__
|
||||
#define __DATASTORE_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
const char* datastore_default_directory = "datastore";
|
||||
|
||||
struct datastore {
|
||||
char* type;
|
||||
char* path;
|
||||
char* storage_max;
|
||||
int64_t storage_gc_watermark;
|
||||
char* gc_period;
|
||||
|
||||
char* params;
|
||||
int no_sync;
|
||||
int hash_on_read;
|
||||
int bloom_filter_size;
|
||||
};
|
||||
|
||||
#endif
|
6
repo/repo.c
Normal file
6
repo/repo.c
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "repo.h"
|
||||
|
||||
int repo_get_config(struct config* config) {
|
||||
return 0;
|
||||
}
|
||||
|
26
repo/repo.h
Normal file
26
repo/repo.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef __REPO_H__
|
||||
#define __REPO_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "config/config.h"
|
||||
|
||||
/**
|
||||
* Get the config
|
||||
* @param config a place to put the buffer (must have been pre-allocated)
|
||||
* @returns 0 on error
|
||||
*/
|
||||
int repo_get_config(struct config* config);
|
||||
|
||||
/**
|
||||
* Retrieves the config
|
||||
* @param config a place to get the information
|
||||
* @returns 0 on error
|
||||
*/
|
||||
int repo_set_config(struct config* config);
|
||||
int repo_set_config_key(char* key, void* value);
|
||||
int repo_get_config_key(char* key, void* value);
|
||||
int repo_get_datastore(struct datastore* datastore);
|
||||
int repo_get_storage_usage(uint64_t* usage);
|
||||
|
||||
#endif // __REPO_H__
|
16
test/Makefile
Normal file
16
test/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
CC = gcc
|
||||
CFLAGS = -O0
|
||||
LFLAGS =
|
||||
DEPS = testit.h repo/test_repo.h cmd/ipfs/test_init.h
|
||||
OBJS = testit.o ../cmd/ipfs/init.o ../commands/argument.o
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
testit: $(OBJS)
|
||||
$(CC) -o $@ $^ $(LFLAGS)
|
||||
|
||||
all: testit
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
36
test/cmd/ipfs/test_init.h
Normal file
36
test/cmd/ipfs/test_init.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* Testing of cmd/ipfs/init
|
||||
*/
|
||||
#ifndef __TEST_INIT_H__
|
||||
#define __TEST_INIT_H__
|
||||
|
||||
#include "../../../cmd/ipfs/init.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int test_get_init_command() {
|
||||
struct command cmd = { 0 };
|
||||
|
||||
// make sure its empty
|
||||
if (cmd.help_text.tagline != NULL) {
|
||||
fprintf(stderr, "short description should be null\n");
|
||||
return 0;
|
||||
}
|
||||
// grab the stuff
|
||||
get_init_command(&cmd);
|
||||
|
||||
// make sure its right
|
||||
if (cmd.help_text.tagline == NULL) {
|
||||
fprintf(stderr, "short description is null\n");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(cmd.help_text.tagline, "Initializes IPFS config file.") != 0) {
|
||||
fprintf(stderr, "short description is not null\n");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
13
test/repo/test_repo.h
Normal file
13
test/repo/test_repo.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef __TEST_REPO_H__
|
||||
#define __TEST_REPO_H__
|
||||
|
||||
#include "../../repo/repo.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_config_repo() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
19
test/testit.c
Normal file
19
test/testit.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include "testit.h"
|
||||
#include "repo/test_repo.h"
|
||||
#include "cmd/ipfs/test_init.h"
|
||||
|
||||
int testit(const char* name, int (*func)(void)) {
|
||||
printf("Testing %s...\n", name);
|
||||
int retVal = func();
|
||||
if (retVal)
|
||||
printf("%s success!\n", name);
|
||||
else
|
||||
printf("** Uh oh! %s failed.**\n", name);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
testit("config_repo", test_config_repo);
|
||||
testit("get_init_command", test_get_init_command);
|
||||
return 1;
|
||||
}
|
1
test/testit.h
Normal file
1
test/testit.h
Normal file
|
@ -0,0 +1 @@
|
|||
int testit(const char* name, int (*func)(void));
|
Loading…
Reference in a new issue