c-ipfs/namesys/name.c

96 lines
2.7 KiB
C
Raw Normal View History

/**
* Handles the command line options for "ipfs name"
*/
#include <stdlib.h>
#include "libp2p/utils/logger.h"
#include "libp2p/utils/vector.h"
#include "ipfs/core/ipfs_node.h"
#include "ipfs/cmd/cli.h"
2017-09-21 22:10:41 +00:00
#include "ipfs/core/http_request.h"
/***
* Publish IPNS name
*/
int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
2017-09-21 22:10:41 +00:00
// call api.
char* response = NULL;
struct HttpRequest* request = ipfs_core_http_request_new();
if (request == NULL)
return 0;
request->command = "name";
request->sub_command = "publish";
libp2p_utils_vector_add(request->arguments, name);
size_t response_size = 0;
int retVal = ipfs_core_http_request_post(local_node, request, &response, &response_size, "", 0);
if (response != NULL && response_size > 0) {
2017-10-04 14:36:38 +00:00
fwrite(response, 1, response_size, stdout);
2017-09-21 22:10:41 +00:00
free(response);
2017-09-25 17:07:16 +00:00
}
2017-09-21 22:10:41 +00:00
ipfs_core_http_request_free(request);
return retVal;
}
int ipfs_name_resolve(struct IpfsNode* local_node, char* name) {
// ask api
char* response = NULL;
struct HttpRequest* request = ipfs_core_http_request_new();
if (request == NULL)
return 0;
request->command = "name";
request->sub_command = "resolve";
libp2p_utils_vector_add(request->arguments, name);
size_t response_size = 0;
int retVal = ipfs_core_http_request_post(local_node, request, &response, &response_size, "", 0);
if (response != NULL && response_size > 0) {
2017-10-04 14:36:38 +00:00
fwrite(response, 1, response_size, stdout);
free(response);
2017-09-25 17:07:16 +00:00
}
ipfs_core_http_request_free(request);
return retVal;
}
/**
* We received a cli command "ipfs name". Do the right thing.
* @param argc number of arguments on the command line
* @param argv actual command line arguments
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_name(struct CliArguments* args) {
int retVal = 0;
struct IpfsNode* client_node = NULL;
if (args->argc < (args->verb_index + 2)) {
libp2p_logger_error("name", "Not enough command line arguments. Should be \"name resolve\" or \"name publish\".\n");
goto exit;
}
char* which = args->argv[args->verb_index+1];
char* path = args->argv[args->verb_index+2];
// make sure API is running
if (!ipfs_node_offline_new(args->config_dir, &client_node)) {
libp2p_logger_error("name", "Unable to create offline node.\n");
goto exit;
}
if (client_node->mode != MODE_API_AVAILABLE) {
libp2p_logger_error("name", "API must be running.\n");
goto exit;
}
// determine what we're doing
if (strcmp(which, "publish") == 0) {
retVal = ipfs_name_publish(client_node, path);
} else if (strcmp(which, "resolve") == 0) {
retVal = ipfs_name_resolve(client_node, path);
} else {
libp2p_logger_error("name", "Nothing found on command line. Should be \"name resolve\" or \"name publish\".\n");
goto exit;
}
exit:
// shut everything down
2017-09-25 13:55:42 +00:00
ipfs_node_free(client_node);
return retVal;
}