Refactor common parts from cmd to wrapper

This is needed to allow daemon reuse these parts and be plugged in
clearly.

Change-Id: I01e020ae6f185faa446c715cf9a6b0c082260e13