Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 1 | #include "aos/aos_cli_utils.h" |
| 2 | |
| 3 | #include <sys/stat.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <iostream> |
| 8 | |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 9 | DEFINE_string(config, "./aos_config.json", "File path of aos configuration"); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 10 | |
| 11 | DEFINE_bool( |
| 12 | _bash_autocomplete, false, |
| 13 | "Internal use: Outputs channel list for use with autocomplete script."); |
| 14 | DEFINE_string(_bash_autocomplete_word, "", |
| 15 | "Internal use: Current word being autocompleted"); |
| 16 | |
| 17 | DEFINE_bool(all, false, |
| 18 | "If true, print out the channels for all nodes, not just the " |
| 19 | "channels which are visible on this node."); |
| 20 | |
| 21 | namespace aos { |
| 22 | namespace { |
| 23 | |
| 24 | bool EndsWith(std::string_view str, std::string_view ending) { |
| 25 | const std::size_t offset = str.size() - ending.size(); |
| 26 | return str.size() >= ending.size() && |
| 27 | std::equal(str.begin() + offset, str.end(), ending.begin(), |
| 28 | ending.end()); |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | bool CliUtilInfo::Initialize( |
| 34 | int *argc, char ***argv, |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 35 | std::function<bool(const aos::Channel *)> channel_filter, |
| 36 | bool expect_args) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 37 | // Don't generate failure output if the config doesn't exist while attempting |
| 38 | // to autocomplete. |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame^] | 39 | if (FLAGS__bash_autocomplete && |
| 40 | (!(EndsWith(FLAGS_config, ".json") || EndsWith(FLAGS_config, ".bfbs")))) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 41 | std::cout << "COMPREPLY=()"; |
| 42 | return true; |
| 43 | } |
| 44 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame^] | 45 | config = aos::configuration::MaybeReadConfig(FLAGS_config); |
| 46 | if (FLAGS__bash_autocomplete && !config.has_value()) { |
| 47 | std::cout << "COMPREPLY=()"; |
| 48 | return true; |
| 49 | } |
| 50 | CHECK(config.has_value()) << "Could not read config. See above errors."; |
| 51 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 52 | event_loop.emplace(&config->message()); |
| 53 | event_loop->SkipTimingReport(); |
| 54 | event_loop->SkipAosLog(); |
| 55 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 56 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 57 | event_loop->configuration()->channels(); |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 58 | |
| 59 | do { |
| 60 | std::string channel_name; |
| 61 | std::string message_type; |
| 62 | if (*argc > 1) { |
| 63 | channel_name = (*argv)[1]; |
| 64 | ShiftArgs(argc, argv); |
| 65 | } |
| 66 | if (*argc > 1) { |
| 67 | message_type = (*argv)[1]; |
| 68 | ShiftArgs(argc, argv); |
| 69 | } |
| 70 | |
| 71 | if (FLAGS__bash_autocomplete) { |
| 72 | Autocomplete(channel_name, message_type, channel_filter); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | if (channel_name.empty() && message_type.empty()) { |
| 77 | std::cout << "Channels:\n"; |
| 78 | for (const aos::Channel *channel : *channels) { |
| 79 | if (FLAGS_all || channel_filter(channel)) { |
| 80 | std::cout << channel->name()->c_str() << ' ' |
| 81 | << channel->type()->c_str() << '\n'; |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | std::vector<const aos::Channel *> found_channels_now; |
| 88 | bool found_exact = false; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 89 | for (const aos::Channel *channel : *channels) { |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 90 | if (!FLAGS_all && !channel_filter(channel)) { |
| 91 | continue; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 92 | } |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 93 | if (channel->name()->c_str() != channel_name) { |
| 94 | continue; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 95 | } |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 96 | if (channel->type()->string_view() == message_type) { |
| 97 | if (!found_exact) { |
| 98 | found_channels_now.clear(); |
| 99 | found_exact = true; |
| 100 | } |
| 101 | } else if (!found_exact && channel->type()->string_view().find( |
| 102 | message_type) != std::string_view::npos) { |
| 103 | } else { |
| 104 | continue; |
| 105 | } |
| 106 | found_channels_now.push_back(channel); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 107 | } |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 108 | |
Austin Schuh | d30a5ca | 2021-07-31 20:49:35 -0700 | [diff] [blame] | 109 | if (found_channels_now.empty()) { |
| 110 | LOG(FATAL) |
| 111 | << "Could not find any channels with the given name and type for " |
| 112 | << channel_name << " " << message_type; |
| 113 | } else if (found_channels_now.size() > 1 && !message_type.empty()) { |
| 114 | LOG(FATAL) << "Multiple channels found with same type for " |
| 115 | << channel_name << " " << message_type; |
| 116 | } |
| 117 | for (const aos::Channel *channel : found_channels_now) { |
| 118 | found_channels.push_back(channel); |
| 119 | } |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 120 | } while (expect_args && *argc > 1); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | void CliUtilInfo::Autocomplete( |
| 126 | std::string_view channel_name, std::string_view message_type, |
| 127 | std::function<bool(const aos::Channel *)> channel_filter) { |
| 128 | const aos::Configuration *const config_msg = event_loop->configuration(); |
| 129 | const bool unique_match = |
| 130 | std::count_if(config_msg->channels()->begin(), |
| 131 | config_msg->channels()->end(), |
| 132 | [channel_name, message_type](const aos::Channel *channel) { |
| 133 | return channel->name()->string_view() == channel_name && |
| 134 | channel->type()->string_view() == message_type; |
| 135 | }) == 1; |
| 136 | |
| 137 | const bool editing_message = |
| 138 | !channel_name.empty() && FLAGS__bash_autocomplete_word == message_type; |
| 139 | const bool editing_channel = |
| 140 | !editing_message && FLAGS__bash_autocomplete_word == channel_name; |
| 141 | |
| 142 | std::cout << "COMPREPLY=("; |
| 143 | |
| 144 | // If we have a unique match, don't provide any suggestions. Otherwise, check |
| 145 | // that were're editing one of the two positional arguments. |
| 146 | if (!unique_match && (editing_message || editing_channel)) { |
| 147 | for (const aos::Channel *channel : *config_msg->channels()) { |
| 148 | if (FLAGS_all || channel_filter(channel)) { |
| 149 | // Suggest only message types if the message type argument is being |
| 150 | // entered. |
| 151 | if (editing_message) { |
| 152 | // Then, filter for only channel names that match exactly and types |
| 153 | // that begin with message_type. |
| 154 | if (channel->name()->string_view() == channel_name && |
| 155 | channel->type()->string_view().find(message_type) == 0) { |
| 156 | std::cout << '\'' << channel->type()->c_str() << "' "; |
| 157 | } |
| 158 | } else if (channel->name()->string_view().find(channel_name) == 0) { |
| 159 | // If the message type empty, then return full autocomplete. |
| 160 | // Otherwise, since the message type is poulated yet not being edited, |
| 161 | // the user must be editing the channel name alone, in which case only |
| 162 | // suggest channel names, not pairs. |
| 163 | if (message_type.empty()) { |
| 164 | std::cout << '\'' << channel->name()->c_str() << ' ' |
| 165 | << channel->type()->c_str() << "' "; |
| 166 | } else { |
| 167 | std::cout << '\'' << channel->name()->c_str() << "' "; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | std::cout << ')'; |
| 174 | } |
| 175 | |
| 176 | } // namespace aos |