Browse Source

klish: Implement -q option

Serj Kalichev 2 years ago
parent
commit
281b426ace
3 changed files with 39 additions and 11 deletions
  1. 14 8
      bin/klish/klish.c
  2. 22 3
      bin/klish/opts.c
  3. 3 0
      bin/klish/private.h

+ 14 - 8
bin/klish/klish.c

@@ -66,14 +66,16 @@ int main(int argc, char **argv)
 		while ((line = faux_list_each(&iter))) {
 			faux_error_t *error = faux_error_new();
 			int retcode = -1;
-
-			if (ktp_session_req_cmd(ktp, line, &retcode, error)) {
-				fprintf(stderr, "Retcode: %d\n", retcode);
-			} else {
+			bool_t rc = BOOL_FALSE;
+			if (!opts->quiet)
+				fprintf(stderr, "%s\n", line);
+			rc = ktp_session_req_cmd(ktp, line, &retcode, error);
+			if (!rc || (faux_error_len(error) > 0)) {
 				fprintf(stderr, "Error:\n");
 				faux_error_fshow(error, stderr);
 			}
-
+			if (rc)
+				fprintf(stderr, "Retcode: %d\n", retcode);
 			faux_error_free(error);
 		}
 
@@ -88,12 +90,16 @@ int main(int argc, char **argv)
 			while ((line = faux_file_getline(fd))) {
 				faux_error_t *error = faux_error_new();
 				int retcode = -1;
-				if (ktp_session_req_cmd(ktp, line, &retcode, error)) {
-					fprintf(stderr, "Retcode: %d\n", retcode);
-				} else {
+				bool_t rc = BOOL_FALSE;
+				if (!opts->quiet)
+					fprintf(stderr, "%s\n", line);
+				rc = ktp_session_req_cmd(ktp, line, &retcode, error);
+				if (!rc || (faux_error_len(error) > 0)) {
 					fprintf(stderr, "Error:\n");
 					faux_error_fshow(error, stderr);
 				}
+				if (rc)
+					fprintf(stderr, "Retcode: %d\n", retcode);
 				faux_error_free(error);
 				faux_str_free(line);
 			}

+ 22 - 3
bin/klish/opts.c

@@ -30,6 +30,9 @@ struct options *opts_init(void)
 
 	// Initialize
 	opts->verbose = BOOL_FALSE;
+	opts->stop_on_error = BOOL_FALSE;
+	opts->dry_run = BOOL_FALSE;
+	opts->quiet = BOOL_FALSE;
 	opts->unix_socket_path = faux_str_dup(KLISH_DEFAULT_UNIX_SOCKET_PATH);
 
 	// Don't free command list because elements are the pointers to
@@ -61,12 +64,15 @@ void opts_free(struct options *opts)
  */
 int opts_parse(int argc, char *argv[], struct options *opts)
 {
-	static const char *shortopts = "hvS:c:";
+	static const char *shortopts = "hvS:c:edq";
 	static const struct option longopts[] = {
 		{"socket",		1, NULL, 'S'},
 		{"help",		0, NULL, 'h'},
 		{"verbose",		0, NULL, 'v'},
 		{"command",		1, NULL, 'c'},
+		{"stop-on-error",	0, NULL, 'e'},
+		{"dry-run",		0, NULL, 'd'},
+		{"quiet",		0, NULL, 'q'},
 		{NULL,			0, NULL, 0}
 	};
 
@@ -85,6 +91,15 @@ int opts_parse(int argc, char *argv[], struct options *opts)
 		case 'v':
 			opts->verbose = BOOL_TRUE;
 			break;
+		case 'e':
+			opts->stop_on_error = BOOL_TRUE;
+			break;
+		case 'd':
+			opts->dry_run = BOOL_TRUE;
+			break;
+		case 'q':
+			opts->quiet = BOOL_TRUE;
+			break;
 		case 'h':
 			help(0, argv[0]);
 			_exit(0);
@@ -140,12 +155,16 @@ void help(int status, const char *argv0)
 			name);
 	} else {
 		printf("Version : %s\n", VERSION);
-		printf("Usage   : %s [options]\n", name);
+		printf("Usage   : %s [options] [filename] ... [filename]\n", name);
 		printf("Klish client\n");
 		printf("Options :\n");
 		printf("\t-S <path>, --socket=<path> UNIX socket path.\n");
 		printf("\t-h, --help Print this help.\n");
 		printf("\t-v, --verbose Be verbose.\n");
-		printf("\t-c <line>, --command=<line> Command to execute.\n");
+		printf("\t-c <line>, --command=<line> Command to execute.\n"
+			"\t\tMultiple options are allowed.\n");
+		printf("\t-e, --stop-on-error Stop script execution on error.\n");
+		printf("\t-q, --quiet Disable echo while executing commands\n\t\tfrom the file stream.\n");
+		printf("\t-d, --dry-run Don't actually execute ACTION scripts.\n");
 	}
 }

+ 3 - 0
bin/klish/private.h

@@ -11,6 +11,9 @@
 struct options {
 	bool_t verbose;
 	char *unix_socket_path;
+	bool_t stop_on_error;
+	bool_t dry_run;
+	bool_t quiet;
 	faux_list_t *commands;
 	faux_list_t *files;
 };