commands: code refactoring

create a general function getExecCommand to aoid repeating the
same code