<commit_before>/*
 * Copyright (C) SUSE LINUX GmbH 2012-2015, all rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>

#include "ccan/list/list.h"
#include "lib/exml.h"
#include "lib/op.h"
#include "lib/azure_mgmt_req.h"
#include "lib/azure_blob_req.h"
#include "lib/azure_fs_req.h"
#include "lib/s3_req.h"
#include "lib/conn.h"
#include "lib/azure_ssl.h"
#include "lib/file/file_api.h"
#include "cli_common.h"
#include "cli_sign.h"
#include "cli_util.h"
#include "cli_create.h"

void
cli_create_args_free(struct cli_args *cli_args)
{
	free(cli_args->path);
	if ((cli_args->type == CLI_TYPE_AZURE)
	 || (cli_args->type == CLI_TYPE_AFS)) {
		free(cli_args->az.blob_acc);
		free(cli_args->az.ctnr_name);
	} else if (cli_args->type == CLI_TYPE_S3) {
		free(cli_args->s3.bkt_name);
	}
	free(cli_args->create.label);
	free(cli_args->create.desc);
	free(cli_args->create.affin_grp);
	free(cli_args->create.location);
}

static int
cli_create_args_validate_az(struct cli_args *cli_args)
{
	if (cli_args->az.blob_acc == NULL) {
		cli_args_usage(cli_args->progname, cli_args->flags,
			       "Create must include an <account> argument");
		return -EINVAL;
	}

	if (cli_args->az.ctnr_name != NULL) {
		/* container creation */
		if ((cli_args->create.label != NULL)
		 || (cli_args->create.desc != NULL)
		 || (cli_args->create.location != NULL)
		 || (cli_args->create.affin_grp != NULL)) {
			cli_args_usage(cli_args->progname, cli_args->flags,
				       "container creation does take "
				       "-l, -d, -A or -L arguments");
			return -EINVAL;
		}
		cli_args->create.type = CLI_CMD_CREATE_CTNR;
		return 0;
	}

	cli_args->create.type = CLI_CMD_CREATE_ACC;
	if (cli_args->create.label == NULL) {
		cli_args_usage(cli_args->progname, cli_args->flags,
			       "Account creation requires a <label> argument");
		return -EINVAL;
	}
	if ((cli_args->create.location == NULL)
	 && (cli_args->create.affin_grp == NULL)) {
		cli_args_usage(cli_args->progname, cli_args->flags,
			       "Create must specify either a <location> or "
			       "<affinity group>");
		return -EINVAL;
	}

	return 0;
}

static int
cli_create_args_validate_s3(struct cli_args *cli_args)
{
	if (cli_args->s3.bkt_name == NULL) {
		cli_args_usage(cli_args->progname, cli_args->flags,
			       "Create must include a <bucket> argument");
		return -EINVAL;
	}

	return 0;
}

int
cli_create_args_parse(int argc,
		      char * const *argv,
		      struct cli_args *cli_args)
{
	int opt;
	int ret;
	extern char *optarg;
	extern int optind;
	/* reset index to start scanning again */
	optind = 1;

	memset(&cli_args->create, 0, sizeof(cli_args->create));
	while ((opt = getopt(argc, argv, "l:d:A:L:s")) != -1) {
		switch (opt) {
		case 'l':
			cli_args->create.label = strdup(optarg);
			if (cli_args->create.label == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		case 'd':
			cli_args->create.desc = strdup(optarg);
			if (cli_args->create.desc == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		case 'A':
			cli_args->create.affin_grp = strdup(optarg);
			if (cli_args->create.affin_grp == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		case 'L':
			cli_args->create.location = strdup(optarg);
			if (cli_args->create.location == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		default: /* '?' */
			cli_args_usage(cli_args->progname, cli_args->flags,
				       "invalid create argument");
			ret = -EINVAL;
			goto err_args_free;
			break;
		}
	}

	/* path is parsed by libfile on open */
	cli_args->path = strdup(argv[optind]);
	if (cli_args->path == NULL) {
		ret = -ENOMEM;
		goto err_args_free;
	}

	/* TODO can be removed. all path parsing now done by libfile */
	if ((cli_args->type == CLI_TYPE_AZURE)
	 || (cli_args->type == CLI_TYPE_AFS)) {
		ret = cli_args_path_parse(cli_args->progname, cli_args->flags,
					  argv[optind],
					  &cli_args->az.blob_acc,
					  &cli_args->az.ctnr_name, NULL);
		if (ret < 0)
			goto err_args_free;
		ret = cli_create_args_validate_az(cli_args);
	} else if (cli_args->type == CLI_TYPE_S3) {
		ret = cli_args_path_parse(cli_args->progname, cli_args->flags,
					  argv[optind],
					  &cli_args->s3.bkt_name,
					  NULL, NULL);
		if (ret < 0)
			goto err_args_free;
		ret = cli_create_args_validate_s3(cli_args);
	} else {
		ret = -ENOTSUP;
	}
	if (ret < 0)
		goto err_args_free;

	cli_args->cmd = CLI_CMD_CREATE;
	return 0;<commit_msg>cli/create: remove path parsing and validation<commit_after>/*
 * Copyright (C) SUSE LINUX GmbH 2012-2015, all rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>

#include "ccan/list/list.h"
#include "lib/exml.h"
#include "lib/op.h"
#include "lib/azure_mgmt_req.h"
#include "lib/azure_blob_req.h"
#include "lib/azure_fs_req.h"
#include "lib/s3_req.h"
#include "lib/conn.h"
#include "lib/azure_ssl.h"
#include "lib/file/file_api.h"
#include "cli_common.h"
#include "cli_sign.h"
#include "cli_util.h"
#include "cli_create.h"

void
cli_create_args_free(struct cli_args *cli_args)
{
	free(cli_args->path);
	free(cli_args->create.label);
	free(cli_args->create.desc);
	free(cli_args->create.affin_grp);
	free(cli_args->create.location);
}

int
cli_create_args_parse(int argc,
		      char * const *argv,
		      struct cli_args *cli_args)
{
	int opt;
	int ret;
	extern char *optarg;
	extern int optind;
	/* reset index to start scanning again */
	optind = 1;
	cli_args->path = NULL;

	memset(&cli_args->create, 0, sizeof(cli_args->create));
	while ((opt = getopt(argc, argv, "l:d:A:L:s")) != -1) {
		switch (opt) {
		case 'l':
			cli_args->create.label = strdup(optarg);
			if (cli_args->create.label == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		case 'd':
			cli_args->create.desc = strdup(optarg);
			if (cli_args->create.desc == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		case 'A':
			cli_args->create.affin_grp = strdup(optarg);
			if (cli_args->create.affin_grp == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		case 'L':
			cli_args->create.location = strdup(optarg);
			if (cli_args->create.location == NULL) {
				ret = -ENOMEM;
				goto err_args_free;
			}
			break;
		default: /* '?' */
			cli_args_usage(cli_args->progname, cli_args->flags,
				       "invalid create argument");
			ret = -EINVAL;
			goto err_args_free;
			break;
		}
	}

	/* path is parsed by libfile on open */
	cli_args->path = strdup(argv[optind]);
	if (cli_args->path == NULL) {
		ret = -ENOMEM;
		goto err_args_free;
	}

	if ((cli_args->type != CLI_TYPE_AZURE)
	 && (cli_args->type != CLI_TYPE_AFS)
	 && (cli_args->type == CLI_TYPE_S3)) {
		ret = -ENOTSUP;
		goto err_args_free;
	}

	cli_args->cmd = CLI_CMD_CREATE;
	return 0;
{'fraction_non_alphanumeric': 0.1423258120505827, 'fraction_numerical': 0.005702950657079098, 'mean_word_length': 3.620274914089347, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2714114', 'n_tokens_mistral': 3363, 'n_tokens_neox': 3032, 'n_words': 718}