CLI2: Generalize method to add context to support writeable context

This commit is contained in:
Tomas Babej 2021-01-28 01:09:43 -05:00
parent 5c3cf0f438
commit 5716f5abb7
2 changed files with 42 additions and 19 deletions

View file

@ -575,15 +575,13 @@ void CLI2::addModifications (const std::string& arg)
while (lex.token (lexeme, type)) while (lex.token (lexeme, type))
mods.push_back (lexeme); mods.push_back (lexeme);
// Determine at which argument index does the task command reside // Determine at which argument index does the task modification command
// reside
unsigned int cmdIndex = 0; unsigned int cmdIndex = 0;
for (; cmdIndex < _args.size(); ++cmdIndex) for (; cmdIndex < _args.size(); ++cmdIndex)
{ {
if (a._lextype == Lexer::Type::separator)
continue;
// Command found, stop iterating. // Command found, stop iterating.
if (a.hasTag ("CMD")) if (_args[cmdIndex].hasTag ("CMD"))
break; break;
} }
@ -597,7 +595,7 @@ void CLI2::addModifications (const std::string& arg)
// There are situations where a context filter is applied. This method // There are situations where a context filter is applied. This method
// determines whether one applies, and if so, applies it. Disqualifiers include: // determines whether one applies, and if so, applies it. Disqualifiers include:
// - filter contains ID or UUID // - filter contains ID or UUID
void CLI2::addContextFilter () void CLI2::addContext (bool readable, bool writeable)
{ {
// Recursion block. // Recursion block.
if (_context_added) if (_context_added)
@ -623,19 +621,44 @@ void CLI2::addContextFilter ()
} }
} }
// Apply context // Determine whether we're using readable or writeable context. Readable
// (filtering) takes precedence.
if (readable) {
Context::getContext ().debug ("Applying context: " + contextName); Context::getContext ().debug ("Applying context: " + contextName);
std::string contextFilter = Context::getContext ().config.get ("context." + contextName); std::string contextFilter = Context::getContext ().config.get ("context." + contextName + ".read");
if (contextFilter == "") if (contextFilter.empty ())
Context::getContext ().debug ("Context '" + contextName + "' not defined."); {
else Context::getContext ().debug ("Specific readable context for '" + contextName + "' not defined. Falling back on generic.");
contextFilter = Context::getContext ().config.get ("context." + contextName);
}
if (! contextFilter.empty ())
{ {
_context_added = true; _context_added = true;
addFilter (contextFilter); addFilter (contextFilter);
if (Context::getContext ().verbose ("context")) if (Context::getContext ().verbose ("context"))
Context::getContext ().footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName)); Context::getContext ().footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName));
} }
}
else if (writeable) {
Context::getContext ().debug ("Applying context: " + contextName);
std::string contextMods = Context::getContext ().config.get ("context." + contextName + ".write");
if (contextMods.empty ())
{
Context::getContext ().debug ("Specific writeable context for '" + contextName + "' not defined. Falling back on generic.");
contextMods = Context::getContext ().config.get ("context." + contextName);
}
if (! contextMods.empty ())
{
_context_added = true;
addModifications (contextMods);
if (Context::getContext ().verbose ("context"))
Context::getContext ().footnote (format ("Context '{1}' set. Use 'task context none' to remove.", contextName));
}
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -931,7 +954,7 @@ void CLI2::categorizeArgs ()
std::string command = getCommand (); std::string command = getCommand ();
Command* cmd = Context::getContext ().commands[command]; Command* cmd = Context::getContext ().commands[command];
if (cmd && cmd->uses_context ()) if (cmd && cmd->uses_context ())
addContextFilter (); addContext (cmd->accepts_filter (), cmd->accepts_modifications ());
bool changes = false; bool changes = false;
bool afterCommand = false; bool afterCommand = false;

View file

@ -74,7 +74,7 @@ public:
void analyze (); void analyze ();
void addFilter (const std::string& arg); void addFilter (const std::string& arg);
void addModifications (const std::string& arg); void addModifications (const std::string& arg);
void addContextFilter (); void addContext (bool readable, bool writeable);
void prepareFilter (); void prepareFilter ();
const std::vector <std::string> getWords (); const std::vector <std::string> getWords ();
bool canonicalize (std::string&, const std::string&, const std::string&) const; bool canonicalize (std::string&, const std::string&, const std::string&) const;