| 249 | } |
| 250 | |
| 251 | void |
| 252 | FileConfigCommand::config_set() |
| 253 | { |
| 254 | static const std::string CREATE_STR{"create"}; |
| 255 | static const std::string TYPE_STR{"type"}; |
| 256 | |
| 257 | auto filename = get_parsed_arguments()->get(COLD_STR).value(); // could be empty which means we should use the default file name |
| 258 | bool append = !get_parsed_arguments()->get(UPDATE_STR); |
| 259 | auto const &data = get_parsed_arguments()->get(SET_STR); |
| 260 | |
| 261 | std::string passed_tag; |
| 262 | if (auto p = get_parsed_arguments()->get(TYPE_STR); p) { |
| 263 | passed_tag = p.value(); |
| 264 | } |
| 265 | // Get the default records.yaml if nothing is passed. |
| 266 | fix_filename(filename); |
| 267 | if (filename.empty()) { |
| 268 | throw std::logic_error("Can't deduce the file path."); |
| 269 | } |
| 270 | |
| 271 | std::ios_base::openmode mode = std::ios::in | std::ios::out; |
| 272 | if (append) { |
| 273 | mode = std::ios::out | std::ios::app; |
| 274 | } |
| 275 | |
| 276 | std::fstream fs; |
| 277 | if (auto err = open_file(filename, fs, mode, true); !err.empty()) { |
| 278 | throw std::logic_error(err); |
| 279 | } |
| 280 | |
| 281 | std::string const &variable = amend_variable_name(data[0]); |
| 282 | try { |
| 283 | if (append) { |
| 284 | YAML::Emitter doc; // we will build the document again, either to append the |
| 285 | // new node or to modify the existing one. |
| 286 | doc << YAML::Comment(get_leading_comment()) << YAML::BeginDoc; |
| 287 | make_tree_node(variable, data[1], passed_tag, doc); |
| 288 | doc << YAML::Newline; |
| 289 | |
| 290 | fs.write(doc.c_str(), doc.size()); |
| 291 | fs.close(); |
| 292 | } else { |
| 293 | FlatYAMLAccessor::load(YAML::LoadAll(fs)); |
| 294 | fs.close(); |
| 295 | |
| 296 | auto new_node = find_or_create_node(variable); |
| 297 | |
| 298 | new_node = data[1]; |
| 299 | |
| 300 | if (!passed_tag.empty()) { |
| 301 | new_node.SetTag(get_tag(passed_tag)); |
| 302 | } |
| 303 | // try gain |
| 304 | if (auto err = open_file(filename, fs, std::ios::out | std::ios::trunc); !err.empty()) { |
| 305 | throw std::logic_error(err); |
| 306 | } |
| 307 | |
| 308 | YAML::Emitter doc; |
nothing calls this directly
no test coverage detected