MCPcopy Create free account
hub / github.com/GothenburgBitFactory/taskwarrior / setConfigVariable

Method setConfigVariable

src/commands/CmdConfig.cpp:56–104  ·  view source on GitHub ↗

///////////////////////////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

54
55////////////////////////////////////////////////////////////////////////////////
56bool CmdConfig::setConfigVariable(const std::string& name, const std::string& value,
57 bool confirmation /* = false */) {
58 // Read .taskrc (or equivalent)
59 std::vector<std::string> contents;
60 File::read(Context::getContext().config.file(), contents);
61
62 auto found = false;
63 auto change = false;
64
65 for (auto& line : contents) {
66 // Get l-trimmed version of the line
67 auto trimmed_line = trim(line, " ");
68
69 // If there is a comment on the line, it must follow the pattern.
70 auto comment = line.find('#');
71 auto pos = trimmed_line.find(name + '=');
72
73 // TODO: Use std::regex here
74 if (pos == 0) {
75 found = true;
76 if (!confirmation ||
77 confirm(format("Are you sure you want to change the value of '{1}' from '{2}' to '{3}'?",
78 name, Context::getContext().config.get(name), value))) {
79 auto new_line = line.substr(0, pos + name.length() + 1) + json::encode(value);
80
81 // Preserve the comment
82 if (comment != std::string::npos) new_line += " " + line.substr(comment);
83
84 // Rewrite the line
85 line = new_line;
86 change = true;
87 }
88 }
89 }
90
91 // Not found, so append instead.
92 if (!found &&
93 (!confirmation ||
94 confirm(format("Are you sure you want to add '{1}' with a value of '{2}'?", name, value)))) {
95 contents.push_back(name + '=' + json::encode(value));
96 change = true;
97 }
98
99 if (change)
100 if (!File::write(Context::getContext().config.file(), contents))
101 throw format("Could not write to '{1}'.", Context::getContext().config.file());
102
103 return change;
104}
105
106////////////////////////////////////////////////////////////////////////////////
107int CmdConfig::unsetConfigVariable(const std::string& name, bool confirmation /* = false */) {

Callers

nothing calls this directly

Calls 2

findMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected