This splits most of the main logic from the cli logic so that they can be used independently, in particular for if we ever libify mgmt.