blob: 0c5fa723d8dc0364776589e7af834dbf936c6666 [file] [log] [blame]
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +00001#include "test-tool.h"
2#include "parse-options.h"
3#include "bundle-uri.h"
Elijah Newrena6dc3d32023-03-21 06:25:53 +00004#include "gettext.h"
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +00005#include "strbuf.h"
6#include "string-list.h"
Ævar Arnfjörð Bjarmason70b9c102022-12-22 15:14:12 +00007#include "transport.h"
Ævar Arnfjörð Bjarmason70b9c102022-12-22 15:14:12 +00008#include "remote.h"
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +00009
Derrick Stolee738e5242022-10-12 12:52:33 +000010enum input_mode {
11 KEY_VALUE_PAIRS,
12 CONFIG_FILE,
13};
14
15static int cmd__bundle_uri_parse(int argc, const char **argv, enum input_mode mode)
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +000016{
17 const char *key_value_usage[] = {
18 "test-tool bundle-uri parse-key-values <input>",
19 NULL
20 };
Derrick Stolee738e5242022-10-12 12:52:33 +000021 const char *config_usage[] = {
22 "test-tool bundle-uri parse-config <input>",
23 NULL
24 };
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +000025 const char **usage = key_value_usage;
26 struct option options[] = {
27 OPT_END(),
28 };
29 struct strbuf sb = STRBUF_INIT;
30 struct bundle_list list;
31 int err = 0;
32 FILE *fp;
33
Derrick Stolee738e5242022-10-12 12:52:33 +000034 if (mode == CONFIG_FILE)
35 usage = config_usage;
36
37 argc = parse_options(argc, argv, NULL, options, usage,
38 PARSE_OPT_STOP_AT_NON_OPTION);
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +000039
40 init_bundle_list(&list);
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +000041
Derrick Stoleeebc39472022-12-22 15:14:15 +000042 list.baseURI = xstrdup("<uri>");
43
Derrick Stolee738e5242022-10-12 12:52:33 +000044 switch (mode) {
45 case KEY_VALUE_PAIRS:
46 if (argc != 1)
47 goto usage;
48 fp = fopen(argv[0], "r");
49 if (!fp)
50 die("failed to open '%s'", argv[0]);
51 while (strbuf_getline(&sb, fp) != EOF) {
52 if (bundle_uri_parse_line(&list, sb.buf))
53 err = error("bad line: '%s'", sb.buf);
54 }
55 fclose(fp);
56 break;
57
58 case CONFIG_FILE:
59 if (argc != 1)
60 goto usage;
61 err = bundle_uri_parse_config_format("<uri>", argv[0], &list);
62 break;
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +000063 }
64 strbuf_release(&sb);
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +000065
66 print_bundle_list(stdout, &list);
67
68 clear_bundle_list(&list);
69
70 return !!err;
71
72usage:
73 usage_with_options(usage, options);
74}
75
Ævar Arnfjörð Bjarmason70b9c102022-12-22 15:14:12 +000076static int cmd_ls_remote(int argc, const char **argv)
77{
Ævar Arnfjörð Bjarmason70b9c102022-12-22 15:14:12 +000078 const char *dest;
79 struct remote *remote;
80 struct transport *transport;
81 int status = 0;
82
83 dest = argc > 1 ? argv[1] : NULL;
84
85 remote = remote_get(dest);
86 if (!remote) {
87 if (dest)
88 die(_("bad repository '%s'"), dest);
89 die(_("no remote configured to get bundle URIs from"));
90 }
Ævar Arnfjörð Bjarmason70b9c102022-12-22 15:14:12 +000091
92 transport = transport_get(remote, NULL);
Ævar Arnfjörð Bjarmason70b9c102022-12-22 15:14:12 +000093 if (transport_get_remote_bundle_uri(transport) < 0) {
94 error(_("could not get the bundle-uri list"));
95 status = 1;
96 goto cleanup;
97 }
98
99 print_bundle_list(stdout, transport->bundles);
100
101cleanup:
102 if (transport_disconnect(transport))
103 return 1;
104 return status;
105}
106
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +0000107int cmd__bundle_uri(int argc, const char **argv)
108{
109 const char *usage[] = {
110 "test-tool bundle-uri <subcommand> [<options>]",
111 NULL
112 };
113 struct option options[] = {
114 OPT_END(),
115 };
116
117 argc = parse_options(argc, argv, NULL, options, usage,
118 PARSE_OPT_STOP_AT_NON_OPTION |
119 PARSE_OPT_KEEP_ARGV0);
120 if (argc == 1)
121 goto usage;
122
123 if (!strcmp(argv[1], "parse-key-values"))
Derrick Stolee738e5242022-10-12 12:52:33 +0000124 return cmd__bundle_uri_parse(argc - 1, argv + 1, KEY_VALUE_PAIRS);
125 if (!strcmp(argv[1], "parse-config"))
126 return cmd__bundle_uri_parse(argc - 1, argv + 1, CONFIG_FILE);
Ævar Arnfjörð Bjarmason70b9c102022-12-22 15:14:12 +0000127 if (!strcmp(argv[1], "ls-remote"))
128 return cmd_ls_remote(argc - 1, argv + 1);
Ævar Arnfjörð Bjarmasond796ced2022-10-12 12:52:32 +0000129 error("there is no test-tool bundle-uri tool '%s'", argv[1]);
130
131usage:
132 usage_with_options(usage, options);
133}