GCC Code Coverage Report


src/
File: src/service/service_cmd.c
Date: 2026-03-31 17:48:22
Lines:
34/35
97.1%
Functions:
2/2
100.0%
Branches:
15/16
93.8%

Line Branch Exec Source
1 #include <assert.h>
2 #include <errno.h>
3 #include <glib.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "cmd/cmd.h"
9 #include "service/service.h"
10
11 static error_t parse_opt(int key, char* arg, struct argp_state* state);
12
13 struct service_arguments {
14 struct global_arguments* global;
15 };
16
17 static struct argp_option options[] = {{NULL}};
18
19 static const char doc[] =
20 "COMMANDS:\n"
21 "\n"
22 " start Ensures the GitTor service is running\n"
23 " stop Ensures the GitTor service is not running\n"
24 " restart Stops and starts the GitTor service\n"
25 " status Prints the GitTor service status (up, down)\n"
26 "\n"
27 "OPTIONS:"
28 "\v";
29
30 static struct argp argp = {options, parse_opt, NULL, doc, NULL, NULL, NULL};
31
32 49 static error_t parse_opt(int key,
33 char* arg,
34 __attribute__((__unused__)) struct argp_state* state) {
35
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 39 times.
49 switch (key) {
36 10 case ARGP_KEY_ARG:
37
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (strcmp(arg, "start") == 0) {
38 1 return gittor_service_start();
39
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 } else if (strcmp(arg, "stop") == 0) {
40 3 return gittor_service_stop();
41
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 } else if (strcmp(arg, "restart") == 0) {
42 2 return gittor_service_restart();
43
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 } else if (strcmp(arg, "status") == 0) {
44 1 printf("%s\n", gittor_service_status());
45 1 return 0;
46
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 } else if (strcmp(arg, "ping") == 0) { // TODO(isaac): remove
47 2 return gittor_service_ping();
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (strcmp(arg, "run") == 0) { // Hidden command
49 return gittor_service_run(false);
50 } else {
51 1 argp_error(state, "%s is not a valid command", arg);
52 1 return ESRCH;
53 }
54 break;
55
56 39 default:
57 39 return ARGP_ERR_UNKNOWN;
58 }
59
60 return 0;
61 }
62
63 10 extern int gittor_service(struct argp_state* state) {
64 // Set defaults arguments
65 10 struct service_arguments args = {0};
66
67 // Change the arguments array for just service
68 10 int argc = state->argc - state->next + 1;
69 10 char** argv = &state->argv[state->next - 1];
70 10 args.global = state->input;
71
72 // Change the command name to gittor service
73 10 const char name[] = "service";
74 10 size_t argv0len = strlen(state->name) + sizeof(name) + 1;
75 10 char* argv0 = argv[0];
76 10 argv[0] = malloc(argv0len);
77
1/1
✓ Branch 1 taken 10 times.
10 g_snprintf(argv[0], argv0len, "%s %s", state->name, name);
78
79 // Parse arguments
80
1/1
✓ Branch 1 taken 10 times.
10 int err = argp_parse(&argp, argc, argv, ARGP_NO_EXIT, &argc, &args);
81
82 // Reset back to global
83 10 free(argv[0]);
84 10 argv[0] = argv0;
85 10 state->next += argc - 1;
86
87 10 return err;
88 }
89