update_engine_client_android implement --merge

This flag corresponds to cleanupSuccessfulUpdate function
of IUpdateEngine. It waits until merge completes or fails.

Note that this doesn't initiate a merge if one has not been
started. To initiate a merge, use snapshotctl.

Test: use it without OTA, it exits immediately
Test: use it after applying update before reboot, it exits immediately
   (however, it cancels the update. See b/147819418)
Test: use it after rebooting into new update, it waits until merge
completes.

Bug: 138808328

Change-Id: Id61c21811f2c33397246035788d139b059208b7f
diff --git a/update_engine_client_android.cc b/update_engine_client_android.cc
index 7766f79..7d9bc3d 100644
--- a/update_engine_client_android.cc
+++ b/update_engine_client_android.cc
@@ -144,7 +144,10 @@
               false,
               "Follow status update changes until a final state is reached. "
               "Exit status is 0 if the update succeeded, and 1 otherwise.");
-
+  DEFINE_bool(merge,
+              false,
+              "Wait for previous update to merge. "
+              "Only available after rebooting to new slot.");
   // Boilerplate init commands.
   base::CommandLine::Init(argc_, argv_);
   brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client");
@@ -222,6 +225,16 @@
     return ExitWhenIdle(status);
   }
 
+  if (FLAGS_merge) {
+    int32_t ret = 0;
+    Status status = service_->cleanupSuccessfulUpdate(&ret);
+    if (status.isOk()) {
+      LOG(INFO) << "CleanupSuccessfulUpdate exits with "
+                << utils::ErrorCodeToString(static_cast<ErrorCode>(ret));
+    }
+    return ExitWhenIdle(status);
+  }
+
   if (FLAGS_follow) {
     // Register a callback object with the service.
     callback_ = new UECallback(this);