| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/credentials.h> |
| 7 |
#include <aws/common/command_line_parser.h> |
| 8 |
#include <aws/common/condition_variable.h> |
| 9 |
#include <aws/common/mutex.h> |
| 10 |
#include <aws/common/zero.h> |
| 11 |
#include <aws/io/channel_bootstrap.h> |
| 12 |
#include <aws/io/event_loop.h> |
| 13 |
#include <aws/io/logging.h> |
| 14 |
#include <aws/io/uri.h> |
| 15 |
|
| 16 |
#include "app_ctx.h" |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
int s3_ls_main(int argc, char *const argv[], const char *command_name, void *user_data); |
| 21 |
int s3_cp_main(int argc, char *const argv[], const char *command_name, void *user_data); |
| 22 |
int s3_compute_platform_info_main(int argc, char *const argv[], const char *command_name, void *user_data); |
| 23 |
|
| 24 |
static struct aws_cli_subcommand_dispatch s_dispatch_table[] = { |
| 25 |
{ |
| 26 |
.command_name = "ls", |
| 27 |
.subcommand_fn = s3_ls_main, |
| 28 |
}, |
| 29 |
{ |
| 30 |
.command_name = "cp", |
| 31 |
.subcommand_fn = s3_cp_main, |
| 32 |
}, |
| 33 |
{ |
| 34 |
.command_name = "platform-info", |
| 35 |
.subcommand_fn = s3_compute_platform_info_main, |
| 36 |
}, |
| 37 |
}; |
| 38 |
|
| 39 |
static void s_usage(int exit_code) { |
| 40 |
|
| 41 |
FILE *output = exit_code == 0 ? stdout : stderr; |
| 42 |
fprintf(output, "usage: s3 <command> <options>\n"); |
| 43 |
fprintf(output, " available commands:\n"); |
| 44 |
|
| 45 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_dispatch_table); ++i) { |
| 46 |
fprintf(output, " %s\n", s_dispatch_table[i].command_name); |
| 47 |
} |
| 48 |
|
| 49 |
fflush(output); |
| 50 |
exit(exit_code); |
| 51 |
} |
| 52 |
|
| 53 |
static void s_setup_logger(struct app_ctx *app_ctx) { |
| 54 |
struct aws_logger_standard_options logger_options = { |
| 55 |
.level = app_ctx->log_level, |
| 56 |
.file = stderr, |
| 57 |
}; |
| 58 |
|
| 59 |
aws_logger_init_standard(&app_ctx->logger, app_ctx->allocator, &logger_options); |
| 60 |
aws_logger_set(&app_ctx->logger); |
| 61 |
} |
| 62 |
|
| 63 |
static struct aws_cli_option s_long_options[] = { |
| 64 |
{"region", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'r'}, |
| 65 |
{"verbose", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'v'}, |
| 66 |
{"help", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'h'}, |
| 67 |
|
| 68 |
{NULL, AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 0}, |
| 69 |
}; |
| 70 |
|
| 71 |
static void s_parse_app_ctx(int argc, char *const argv[], struct app_ctx *app_ctx) { |
| 72 |
|
| 73 |
while (true) { |
| 74 |
int option_index = 0; |
| 75 |
int c = aws_cli_getopt_long(argc, argv, "r:v:h", s_long_options, &option_index); |
| 76 |
if (c == -1) { |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
switch (c) { |
| 81 |
case 0: |
| 82 |
|
| 83 |
break; |
| 84 |
case 'r': |
| 85 |
app_ctx->region = aws_cli_optarg; |
| 86 |
break; |
| 87 |
case 'v': |
| 88 |
if (!strcmp(aws_cli_optarg, "TRACE")) { |
| 89 |
app_ctx->log_level = AWS_LL_TRACE; |
| 90 |
} else if (!strcmp(aws_cli_optarg, "INFO")) { |
| 91 |
app_ctx->log_level = AWS_LL_INFO; |
| 92 |
} else if (!strcmp(aws_cli_optarg, "DEBUG")) { |
| 93 |
app_ctx->log_level = AWS_LL_DEBUG; |
| 94 |
} else if (!strcmp(aws_cli_optarg, "ERROR")) { |
| 95 |
app_ctx->log_level = AWS_LL_ERROR; |
| 96 |
} else { |
| 97 |
fprintf(stderr, "unsupported log level %s.\n", aws_cli_optarg); |
| 98 |
s_usage(1); |
| 99 |
} |
| 100 |
break; |
| 101 |
case 'h': |
| 102 |
app_ctx->help_requested = true; |
| 103 |
break; |
| 104 |
default: |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (!app_ctx->help_requested) { |
| 110 |
if (app_ctx->log_level != AWS_LOG_LEVEL_NONE) { |
| 111 |
s_setup_logger(app_ctx); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
aws_cli_reset_state(); |
| 117 |
|
| 118 |
|
| 119 |
aws_s3_init_default_signing_config( |
| 120 |
&app_ctx->signing_config, aws_byte_cursor_from_c_str(app_ctx->region), app_ctx->credentials_provider); |
| 121 |
app_ctx->signing_config.flags.use_double_uri_encode = false; |
| 122 |
|
| 123 |
|
| 124 |
struct aws_s3_client_config client_config; |
| 125 |
AWS_ZERO_STRUCT(client_config); |
| 126 |
client_config.client_bootstrap = app_ctx->client_bootstrap; |
| 127 |
client_config.region = aws_byte_cursor_from_c_str(app_ctx->region); |
| 128 |
client_config.signing_config = &app_ctx->signing_config; |
| 129 |
app_ctx->client = aws_s3_client_new(app_ctx->allocator, &client_config); |
| 130 |
} |
| 131 |
|
| 132 |
int main(int argc, char *argv[]) { |
| 133 |
|
| 134 |
struct aws_allocator *allocator = aws_default_allocator(); |
| 135 |
aws_s3_library_init(allocator); |
| 136 |
|
| 137 |
struct app_ctx app_ctx; |
| 138 |
AWS_ZERO_STRUCT(app_ctx); |
| 139 |
app_ctx.allocator = allocator; |
| 140 |
app_ctx.c_var = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT; |
| 141 |
aws_mutex_init(&app_ctx.mutex); |
| 142 |
|
| 143 |
|
| 144 |
struct aws_event_loop_group *event_loop_group = aws_event_loop_group_new_default(allocator, 0, NULL); |
| 145 |
|
| 146 |
|
| 147 |
struct aws_host_resolver_default_options resolver_options = { |
| 148 |
.el_group = event_loop_group, |
| 149 |
.max_entries = 8, |
| 150 |
}; |
| 151 |
struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 152 |
|
| 153 |
|
| 154 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 155 |
.event_loop_group = event_loop_group, |
| 156 |
.host_resolver = resolver, |
| 157 |
}; |
| 158 |
app_ctx.client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 159 |
if (app_ctx.client_bootstrap == NULL) { |
| 160 |
printf("ERROR initializing client bootstrap\n"); |
| 161 |
return -1; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
struct aws_credentials_provider_chain_default_options credentials_provider_options; |
| 166 |
AWS_ZERO_STRUCT(credentials_provider_options); |
| 167 |
credentials_provider_options.bootstrap = app_ctx.client_bootstrap; |
| 168 |
app_ctx.credentials_provider = aws_credentials_provider_new_chain_default(allocator, &credentials_provider_options); |
| 169 |
|
| 170 |
s_parse_app_ctx(argc, argv, &app_ctx); |
| 171 |
int dispatch_return_code = |
| 172 |
aws_cli_dispatch_on_subcommand(argc, argv, s_dispatch_table, AWS_ARRAY_SIZE(s_dispatch_table), &app_ctx); |
| 173 |
|
| 174 |
if (dispatch_return_code && |
| 175 |
(aws_last_error() == AWS_ERROR_INVALID_ARGUMENT || aws_last_error() == AWS_ERROR_UNIMPLEMENTED)) { |
| 176 |
s_usage(app_ctx.help_requested == true ? 0 : 1); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
aws_s3_client_release(app_ctx.client); |
| 181 |
aws_credentials_provider_release(app_ctx.credentials_provider); |
| 182 |
aws_client_bootstrap_release(app_ctx.client_bootstrap); |
| 183 |
aws_host_resolver_release(resolver); |
| 184 |
aws_event_loop_group_release(event_loop_group); |
| 185 |
aws_mutex_clean_up(&app_ctx.mutex); |
| 186 |
aws_s3_library_clean_up(); |
| 187 |
|
| 188 |
return dispatch_return_code; |
| 189 |
} |