Don't print an error message for empty courses
[nci.git] / nci-delete-assignment-group.c
blob34a3168ca0547cf8d9eb74d5e146660a0b717821
1 /*
2 * Copyright (c) 2016-2019 S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
7 * in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include <curl/curl.h>
27 #include <yajl_parse.h>
29 #include "macros.h"
30 #include "util.h"
32 int
33 main(int argc, char **argv)
35 int ret = EINVAL;
36 char *url_base = 0;
37 char *auth_token = 0;
38 char *course_id = 0;
39 char *group_id = 0;
40 char *target_arg = 0;
41 long long target_ll = 0;
42 char *target = 0;
43 uint_fast8_t purge_arg = 0;
44 size_t len = 0;
45 char *built_uri = 0;
46 struct curl_httppost *post = 0;
47 struct curl_httppost *postend = 0;
48 char *deleted_id = 0;
49 int opt = 0;
51 setlocale(LC_ALL, "");
53 while ((opt = getopt(argc, argv, "c:g:t:p")) != -1) {
54 switch (opt) {
55 case 'c':
56 course_id = optarg;
57 break;
58 case 'g':
59 group_id = optarg;
60 break;
61 case 't':
62 target_arg = optarg;
63 break;
64 case 'p':
65 purge_arg = 1;
66 break;
67 default:
68 break;
72 if (!course_id) {
73 ret = EINVAL;
74 fprintf(stderr, "course-id is mandatory\n");
75 goto cleanup;
78 if (!target_arg &&
79 !purge_arg) {
80 ret = EINVAL;
81 fprintf(stderr, "one of -t, -p is mandatory\n");
82 goto cleanup;
83 } else if (target_arg &&
84 purge_arg) {
85 fprintf(stderr, "both -t and -p provided, ignoring -p\n");
86 purge_arg = 0;
89 curl_global_init(CURL_GLOBAL_DEFAULT);
91 if (!(url_base = get_url_base())) {
92 ret = ENOENT;
94 /* Error should have already been printed */
95 goto cleanup;
98 if (!(auth_token = get_auth_token())) {
99 ret = ENOENT;
101 /* Error should have already been printed */
102 goto cleanup;
105 if (target_arg) {
106 target_ll = strtoll(target_arg, 0, 0);
107 len = snprintf(0, 0, "%lld", target_ll);
109 if (len + 1 < len) {
110 ret = errno = EOVERFLOW;
111 perror(L(""));
112 goto cleanup;
115 if (!(target = malloc(len + 1))) {
116 ret = errno;
117 perror(L("malloc"));
118 goto cleanup;
121 sprintf(target, "%lld", target_ll);
123 if (curl_formadd(&post, &postend, CURLFORM_PTRNAME,
124 "move_assignments_to", CURLFORM_PTRCONTENTS,
125 target,
126 CURLFORM_END)) {
127 ret = errno;
128 perror(L("curl_formadd"));
129 goto cleanup;
133 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignment_groups/%s",
134 url_base, course_id, group_id);
136 if (len + 1 < len) {
137 ret = errno = EOVERFLOW;
138 perror(L(""));
139 goto cleanup;
142 if (!(built_uri = malloc(len + 1))) {
143 ret = errno;
144 perror(L("malloc"));
145 goto cleanup;
148 sprintf(built_uri, "%s/api/v1/courses/%s/assignment_groups/%s",
149 url_base, course_id, group_id);
151 if ((ret = send_and_id_scan(built_uri, auth_token, post, "DELETE",
152 &deleted_id))) {
153 goto cleanup;
156 if (deleted_id) {
157 printf("%s\n", deleted_id);
160 ret = 0;
161 cleanup:
162 free(deleted_id);
163 curl_formfree(post);
164 free(built_uri);
165 free(target);
166 free(url_base);
167 free(auth_token);
168 curl_global_cleanup();
170 return ret;